How to resolve Unneccessary Stubbing exception

前端 未结 13 1452
无人共我
无人共我 2020-12-24 03:57

My Code is as below,

@RunWith(MockitoJUnitRunner.class)
public class MyClass {

    private static final String code =\"Test\";

    @Mock
     private MyCl         


        
13条回答
  •  清酒与你
    2020-12-24 04:35

    I had UnnecessaryStubbingException when I tried to use the when methods on a Spy object. Mockito.lenient() silenced the exception but the test results were not correct.

    In case of Spy objects, one has to call the methods directly.

    @ExtendWith(MockitoExtension.class)
    @RunWith(JUnitPlatform.class)
    class ArithmTest {
    
        @Spy
        private Arithm arithm;
    
        @Test
        void testAddition() {
    
            int res = arithm.add(2, 5);
    
            // doReturn(7).when(arithm).add(2, 5);
            assertEquals(res, 7);
        }
    }
    

提交回复
热议问题