Powermockito doNothing for method with arguments

前端 未结 4 1546
小鲜肉
小鲜肉 2020-12-31 05:43

I\'ve developed an application in Java and I\'m trying to create unit tests using Powermockito (I should add that I\'m new to unit testing).

I have a class called Re

4条回答
  •  鱼传尺愫
    2020-12-31 06:08

    You can find a fully functional example below. Since you didn't post the complete example, I can only assume that you did not annotate the test class with @RunWith or @PrepareForTest because the rest seems fine.

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({Resource.class})
    public class MockingTest{
    
        @Test
        public void shouldMockVoidStaticMethod() throws Exception {
            PowerMockito.spy(Resource.class);
            PowerMockito.doNothing().when(Resource.class, "readResources", Mockito.any(String.class));
    
            //no exception heeeeere!
            Resource.readResources("whatever");
    
            PowerMockito.verifyStatic();
            Resource.readResources("whatever");
        }
    
    }
    
    class Resource {
        public static void readResources(String someArgument) {
            throw new UnsupportedOperationException("meh!");
        }
    }
    

提交回复
热议问题