powermock

PowerMock Mockito: how to mock all static methods?

隐身守侯 提交于 2019-12-04 03:40:55
问题 Do we need to mock all static methods of a class when using PowerMock (with Mockito)? I mean, suppose we have: class MockMe { public static MockMe getInstance(){ //return new Instance via complex process; } public static List<X> anotherStaticMethod(){ // does xyz } } My question, if I need to mock getInstance method, is it necessary to mock "anotherStaticMethod" as well? PowerMock version:1.3, Mockito version:1.8 回答1: No you can use partial mocking using spy in PowerMockito. Or you can use

Is there away to mock UUID in Mockito without using powermock?

醉酒当歌 提交于 2019-12-04 02:21:44
问题 I want to mock an object that has a uuid value but I don't want to install powermock. 回答1: Your easiest way to achieve this will be to wrap up your UUID generation. Suppose you have a class using UUID.randomUUID public Clazz MyClazz{ public void doSomething(){ UUID uuid = UUID.randomUUID(); } } The UUID geneartion is completely tied to the JDK implementation. A solution would to be wrap the UUID generation that could be replaced at test time with a different dependency. Spring has an

PowerMock: How to unmock a method?

二次信任 提交于 2019-12-03 23:51:55
I have a static method that is mocked using PowerMock to throw an exception. (It deletes files.) Unfortunately, during my @After (after-each-test) method, I need to call this method without the mocks. How can I umock a method? I don't see an equivalent to Mockito.reset() . [ Ref: mockito : how to unmock a method? ] Example: @RunWith(PowerMockRunner.class) @PrepareForTest(PathUtils.class) // Important: This class has a static method we want to mock. public class CleaningServiceImplTest2 extends TestBase { public static final File testDirPath = new File(CleaningServiceImplTest2.class

Mockito / PowerMocktio doNothing for none void method

て烟熏妆下的殇ゞ 提交于 2019-12-03 23:33:32
I need a method which returns something to do nothing when invoked during testing, the class instance which owns the method is implemented as a spy. I am aware the doNothing() method only works with void methods. Is there a way to get the same behaviour with a method that returns something? Thank you! Use when(spy.myMethod()).thenReturn(null) . This will prevent the spy from invoking the wrapped instance. You have to tell Mockito what to return for a method that returns something. The default behavior for a mock is to return null . The default behavior for a spy is to call the wrapped object.

Powermock mockstatic Cannot subclass final class

为君一笑 提交于 2019-12-03 23:26:08
I am trying to mock a final class PowerMockito.mockStatic(TestFinalClass.class); It is working from my eclipse when I run a single junit and add javaagent to my VM arguments -javaagent:{path}/powermock-module-javaagent-1.6.4.jar But when I try to run all test cases from command line using maven build command I am still getting "Cannot subclass final class" Below is my snippet from pom.xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4</version> <configuration> <argLine>-javaagent:{path}/powermock-module-javaagent-1.6.4.jar<

Powermock verify private static method call in non static method

谁说胖子不能爱 提交于 2019-12-03 17:05:39
Dear stackoverflow comrades, I again have a problem in getting a specific PowerMock / Mockito case to work. The issue is, that I need to verify the call of a private static method, which is called from a public non-static method . A similar example I posted previously on How to suppress and verify private static method calls? This is my code: class Factory { public String factorObject() throws Exception { String s = "Hello Mary Lou"; checkString(s); return s; } private static void checkString(String s) throws Exception { throw new Exception(); } } And this is my testclass: @RunWith

How can I test a method which invoke protected (unwanted) methods of parent class?

早过忘川 提交于 2019-12-03 16:38:15
I'm stuck in a very weird case. I have some specific code which I need to test. Here it is: public class A { /* * The real method of real class is so big that I just don't want to test it. * That's why I use throwing an exception. */ protected void method(Integer result) { throw new RuntimeException("Oops!"); } protected <T> T generifiedMethod(String s, T type) { throw new RuntimeException("Oops!"); } protected void mainMethod(Integer value) { throw new RuntimeException("Oops!"); } } I also have a child class: public class B extends A { @Override protected void mainMethod(Integer value) { if

Which Maven artifacts should I use to import PowerMock?

 ̄綄美尐妖づ 提交于 2019-12-03 16:25:25
问题 What jars do I need to add to my pom.xml to get PowerMock working with Mockito? I have the following dependencies: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-support<

How to mock non static methods using PowerMock

╄→гoц情女王★ 提交于 2019-12-03 14:26:50
I am trying to mock an inner method call of my test method My class looks like this public class App { public Student getStudent() { MyDAO dao = new MyDAO(); return dao.getStudentDetails();//getStudentDetails is a public //non-static method in the DAO class } When I write the junit for the method getStudent(), is there a way in PowerMock to mock the line dao.getStudentDetails(); or make the App class use a mock dao object during junit execution instead of the actual dao call which connects to the DB? You can use the whenNew() method from PowerMock (see https://github.com/powermock/powermock

Testing code which calls native methods

冷暖自知 提交于 2019-12-03 12:10:52
I have a class like this: public final class Foo { public native int getBar(); public String toString() { return "Bar: " + getBar(); } } Please note that getBar() is implemented with JNI and that the class is final . I want to write a junit test to test the toString() method. For this I need to mock the getBar() method and then run the original toString() method to check the output. My first thought was that this must be impossible but then I found PowerMock which supports testing final classes and native methods according to the feature list. But so far I had no success with it. The best