Best Mockito code snippet using org.mockitousage.strictness.StrictnessPerStubbingTest.unnecessary_stubbing
Source:StrictnessPerStubbingTest.java
...153 // and stubbing works, too:154 Assert.assertEquals("1", mock.simpleMethod("1"));155 }156 @Test157 public void unnecessary_stubbing() {158 // when159 Mockito.when(mock.simpleMethod("1")).thenReturn("1");160 Mockito.lenient().when(mock.differentMethod("2")).thenReturn("2");161 // then unnecessary stubbing flags method only on the strict stubbing:162 // good enough to prove that we're flagging just one unnecessary stubbing:163 // TODO 792: this assertion is duplicated with StrictnessPerMockTest164 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {165 @Override166 public void call() throws Throwable {167 mockito.finishMocking();168 }169 }).isInstanceOf(UnnecessaryStubbingException.class).hasMessageContaining("1. -> ").isNot(TestBase.hasMessageContaining("2. ->"));170 }171 @Test172 public void unnecessary_stubbing_with_doReturn() {173 // when174 Mockito.lenient().doReturn("2").when(mock).differentMethod("2");175 // then no exception is thrown:176 mockito.finishMocking();177 }178 @Test179 public void verify_no_more_invocations() {180 // when181 Mockito.when(mock.simpleMethod("1")).thenReturn("1");182 Mockito.lenient().when(mock.differentMethod("2")).thenReturn("2");183 // and:184 mock.simpleMethod("1");185 mock.differentMethod("200");// <- different arg186 // then 'verifyNoMoreInteractions' flags the lenient stubbing (called with different arg)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!