Powermockito doNothing for method with arguments

前端 未结 4 1541
小鲜肉
小鲜肉 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!");
        }
    }
    
    0 讨论(0)
  • 2020-12-31 06:11

    Maybe i can't undestand your question, but i believe it's necessary specify what must do the method, so if you don't specify thenReturn or thenThrow or whatever powerMockito doesn't know what have to do when read your real code, for example:

    REAL CODE:

                IPager pag;
            IPagerData<List<IDeute>> dpag;
            pag = new PagerImpl();
            pag.setFiles(nombrefilesPaginador);
            pag.setInici(1);
            dpag = gptService.obtenirDeutes(idSubjecte, idEns, tipusDeute, periode, pag);
    

    Testing real code by mockito:

            IPager pag = new PagerImpl();
            pag.setInici(1);
            pag.setFiles(0);
            when(serveiGpt.obtenirDeutes(eq(331225L),
             eq(IConstantsIdentificadors.ID_ENS_BASE), 
             Matchers.any(ETipusDeute.class),
             Matchers.any(EPeriodeDeute.class), 
             eq(pag)))
            .thenThrow(new NullPointerException(" Null!"));
    

    If haven't specify the return my test will be fail. I hope it helps.

    0 讨论(0)
  • Why go through so much trouble just so that your method does not do anything. Just calling PowerMockito.mockStatic(Resource.class) should replace all static methods in your class with default stubs which basically mean they do nothing.

    Unless you do want to change the behavior of your method to actually do something just calling PowerMockito.mockStatic(Resource.class) should suffice. Ofcourse this also means all static methods in the class are stubbed which you need to consider.

    0 讨论(0)
  • 2020-12-31 06:22

    If doNothing() isn't working you can hack it a bit using the PowerMockito.doAnswer(). This lets you mock into void methods that are supposed to do something, like setting values, etc. If doNothing() doesn't work, using a blank doAnswer() should work fine.

    Example:

    PowerMockito.doAnswer(new org.mockito.stubbing.Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return null; //does nothing
        }
    }).when(mockObject).methodYouWantToDoNothing(args);
    
    0 讨论(0)
提交回复
热议问题