Giriş
Şu satırı dahil ederiz
import org.mockito.Mockito;import org.mockito.junit.jupiter.MockitoExtension;
Örneği ilk burada gördüm. Aslında bu çözümler sadece UnnecessaryStubbingException fırlatılmasını engelliyor. Testleri halen temizlemekte fayda var. Verilen hata şöyle
Unnecessary stubbings detected.Clean & maintainable test code requires zero unnecessary code.Following stubbings are unnecessary (click to navigate to relevant line of code):1. -> at pl.nsn.railway.grkeepalive.MyTest.testLenient(MyTest.java:41)2. -> at pl.nsn.railway.grkeepalive.MyTest.testLenient(MyTest.java:42)Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
Örnek - JUnit 4
Şöyle yaparız. Böylece artık UnnecessaryStubbingException dikkate alınmaz
@RunWith(MockitoJUnitRunner.Silent.class) public class FooTest { ... }
Örnek - JUnit 5
@MockitoSettings(strictness = Strictness.LENIENT) kullanırız. Böylece artık UnnecessaryStubbingException dikkate alınmaz
@ExtendWith(MockitoExtension.class)@MockitoSettings(strictness = Strictness.LENIENT)public class MyTest {public class MyFrameworkRuntimeProvider {String getContainer(String name) {return name;}String getComponentHandle() {return null;}}//@Mock//MyFrameworkRuntimeProvider mock;@Testpublic void testLenient(){MyFrameworkRuntimeProvider mock = Mockito.mock(MyFrameworkRuntimeProvider.class);Mockito.when(mock.getContainer("1")).thenReturn("1");Mockito.when(mock.getContainer("2")).thenReturn("2"); //not calledMockito.when(mock.getComponentHandle()).thenReturn(null);//not calledmock.getContainer("1");Mockito.verify(mock,Mockito.atLeast(1)).getContainer(Mockito.anyString());}}
Hiç yorum yok:
Yorum Gönder