powermock

Mock java.lang.Runtime with PowerMockito

 ̄綄美尐妖づ 提交于 2019-12-07 04:11:54
问题 want to write an unittest for a method like public static void startProgram() { process = Runtime.getRuntime().exec(command, null, file); } I don't want to inject the runtime object for some reasons, so I wanted to stub the getRuntime method that it returns a Runtime mock... I tried it this way: @RunWith(PowerMockRunner.class) @PrepareForTest(Runtime.class) public class ProgramTest { @Test public void testStartProgram() { Runtime mockedRuntime = PowerMockito.mock(Runtime.class); PowerMockito

How to mock super class method using Mockito or anyother relavent java framework

不想你离开。 提交于 2019-12-07 03:58:48
问题 My Scenario is as below class SuperClass{ public void run(){ System.out.println("I am running in Super class"); } } class ChildClass extends SuperClass{ public void childRunner(){ System.out.println("Step 1"); System.out.println("Step 2"); **run();** System.out.println("Last Step"); } } Now I want to mock the childRunner() method of ChildClass and since this method internally calls the super class method, i need some help/piece of code on how to mock the run() method which is present in the

Mocking a final method with PowerMock + EasyMock

回眸只為那壹抹淺笑 提交于 2019-12-07 02:50:22
问题 I'm trying to mock a call to the final method ResourceBundle.getString() . With PowerMock 1.4.12 and EasyMock 3.1, the call is not being mocked; instead, the "real" method is called. My test class: @RunWith(PowerMockRunner.class) @PrepareForTest(ResourceBundle.class) public class TestSuite { @Before public void setUp() throws Exception { ResourceBundle resourceBundleMock = PowerMock.createNiceMock(ResourceBundle.class); expect(resourceBundleMock.getString(BundleConstants.QUEUE)).andReturn(

NoClassDefFoundError: org/hamcrest/Matchers using PowerMock-OSGi

一个人想着一个人 提交于 2019-12-06 15:32:45
I get an NoClassDefFoundError for org.hamcrest.Matchers, when i run my Test as OSGi PlugIn test, but when i run it as plain JUnit test everthing works as expected. I am using the OSGi version of PowerMock and have all neccessary dependencies in my launch config. What i am doing wrong? It seems like the Testrunner doesnt see the class, for some reason. Edit: I created a reduced sample project and figured out that the Problem only appear when i use @PrepareForTest(XXX.class) in my class declaration. java.lang.NoClassDefFoundError: org/hamcrest/Matchers at eu.gemtec.commons.util.assertion.Assert

PowerMock学习(十一)之Mock private methods的使用

不打扰是莪最后的温柔 提交于 2019-12-06 15:20:14
Mock private methods 就是mock私有方法啦,学到这不难发现,我们其实大部分都是通过反射去完成单元测试的,但是在实际中,某个类中的私有方法,个人不建议使用反射来测试,因为有时候会覆盖单元测试中的方法,那么下面我们就来举个例子,来使用mock来模拟私有方法的测试。 模拟场景 假设我们按照人名去查找一个学生是否存在 isExist() ,如果存在就返回true,否则false,同时调用这个类中的私有的查找方法返回学生个数,如果存在返回1,不存在返回0 业务逻辑部分代码比较简单,具体代码如下: package com.rongrong.powermock.mockprivate; /** * @author rongrong * @version 1.0 * @description: * @date 2019/12/5 21:48 */ public class StudentPrivateService { public boolean isExist(String userName) { int count = checkExist(userName); if (count > 0) { return true; } else { throw new NullPointerException(); } } private int checkExist

您如何断言在JUnit 4测试中引发了某种异常?

徘徊边缘 提交于 2019-12-06 13:02:23
如何惯用JUnit4来测试某些代码引发异常? 虽然我当然可以做这样的事情: @Test public void testFooThrowsIndexOutOfBoundsException() { boolean thrown = false; try { foo.doStuff(); } catch (IndexOutOfBoundsException e) { thrown = true; } assertTrue(thrown); } 我记得在这种情况下,有一个批注或一个Assert.xyz或 一些 不那么杂乱无章的 东西 ,更是JUnit的精髓。 #1楼 我在这里尝试了许多方法,但是它们要么很复杂,要么不完全符合我的要求。 实际上,可以很简单地编写一个辅助方法: public class ExceptionAssertions { public static void assertException(BlastContainer blastContainer ) { boolean caughtException = false; try { blastContainer.test(); } catch( Exception e ) { caughtException = true; } if( !caughtException ) { throw new

use mockito to stub final method [duplicate]

こ雲淡風輕ζ 提交于 2019-12-06 12:46:29
This question already has answers here : Final method mocking (7 answers) Closed 5 years ago . I need to use a mock who has a final method. So i use powermock but it does not work class B { public final int nb() { return 4; } } @RunWith(PowerMockRunner.class) @PrepareForTest(B.class) public class Exemple extends TestCase { @Test public void test() { B b = PowerMockito.mock(B.class); PowerMockito.when(b.nb()).thenReturn(5); final int actualState = b.nb(); assertEquals(5, actualState); } } if someone has a solution, thank you in advance Your example is fine, should work without any problem.

PowerMock - Mocking static system class throws IllegalStateException

自作多情 提交于 2019-12-06 12:33:00
I have the following code public class A{ public void createFile() { File tempXmlFile = null; String extension = ".xml"; String name = "someName"; try { tempXmlFile = File.createTempFile(name, extension); if (tempXmlFile.exists()) { tempXmlFile.delete(); } } catch (IOException e) { System.out.println(e.getStackTrace()); } } } @RunWith(PowerMockRunner.class) @PrepareForTest(A.class) public class testA extends TestCase{ private A classUnderTest; @Override @Before public void setUp() { classUnderTest = PowerMock.createMock(A.class); //the class is more complex in my case and I have to mock it }

Missing jacoco.exec file when using jacoco offline instrumentation with Powermock

末鹿安然 提交于 2019-12-06 11:47:31
问题 Despite apparently this post showed a solution to using powermock and jacoco, I haven't been able to make it work in a pretty simple project (available on GitHub). In my case, the test executes correctly but the jacoco.exec file is missing so jacoco doesn't check coverage. Test class: @RunWith(PowerMockRunner.class) @PrepareOnlyThisForTest(Util.class) @PowerMockIgnore("org.jacoco.agent.rt.*") public class UtilTest { @Test public void testSay() throws Exception { PowerMockito.mockStatic(Util

PowerMock + Robolectric + Dagger2

大兔子大兔子 提交于 2019-12-06 08:48:01
I test custom view class which contain: android ui elements some logic static methods callings dagger2 dependencies So i use next tools for testing Robolectric for UI elements mocking unit tests for logic testing PowerMock for static methods mocking Robolectric + PowerMock integration problem is known and solution is known - https://github.com/robolectric/robolectric/wiki/Using-PowerMock But with this solution dagger2 dependencies fail. Attention to code. My custom view: public class ProgressTextView extends TextView { private String defaultText; private int fileSize; private String