How to resolve Unneccessary Stubbing exception

前端 未结 13 1451
无人共我
无人共我 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 05:01

    Silent is not a solution. You need fix your mock in your test. See official documentation here.

    Unnecessary stubs are stubbed method calls that were never realized during test execution (see also MockitoHint), example:

    //code under test:
     ...
     String result = translator.translate("one")
     ...
    
     //test:
     ...
     when(translator.translate("one")).thenReturn("jeden"); // <- stubbing realized during code execution
     when(translator.translate("two")).thenReturn("dwa"); // <- stubbing never realized
     ...
    

    Notice that one of the stubbed methods were never realized in the code under test, during test execution. The stray stubbing might be an oversight of the developer, the artifact of copy-paste or the effect not understanding the test/code. Either way, the developer ends up with unnecessary test code. In order to keep the codebase clean & maintainable it is necessary to remove unnecessary code. Otherwise tests are harder to read and reason about.

    To find out more about detecting unused stubbings see MockitoHint.

提交回复
热议问题