Strictness#STRICT_STUBS
is one of the most benificial features provided by Mockito. Strictness#LENIENT
was the default behaviour of Mockito 1.x but from Mockito 3.x. onwards Strictness#STRICT_STUBS
will be the default behaviour.
Strictness#STRICT_STUBS
make sure clean code, unnecessary and unused mock and stubbing are avoided, reduces code duplication, improves debuggability hence overall increases the performance and productivity, but some times we are not sure at invocation time which parameter will be used hence need to stub with multiple inputs. Strictness#STRICT_STUBS
will not allow this and throw UnnecessaryStubbingException
.
Mockito provides a way to add lenient stubs with Strictness#STRICT_STUBS.
lenient().when().thenReturn()
Mockito allows those unused stubbed which are maked with lenient
package com.example.mokito3.sujan;
public class AppleService {
public String saveApple(String apple) {
String appleString = "i love " + apple + " apple";
return appleString;
}
}
package com.example.mokito3.sujan;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
public class AppleServiceTest {
@Mock
private AppleService appleService;
@Test
void saveAppleWithStaticMockTest() {
AppleService appleService = mock(AppleService.class);
lenient().when(appleService.saveApple("Fuji")).thenReturn("i eat Fuji apple");
when(appleService.saveApple("Macintosh")).thenReturn("i eat Macintosh apple");
String apple = appleService.saveApple("Macintosh");
Assertions.assertEquals("i eat Macintosh apple", apple);
verify(appleService).saveApple("Macintosh");
}
@Test
void saveAppleWithAnnotationMockTest() {
lenient().when(appleService.saveApple("Fuji")).thenReturn("i eat Fuji apple");
when(appleService.saveApple("Macintosh")).thenReturn("i eat Macintosh apple");
String apple = appleService.saveApple("Macintosh");
Assertions.assertEquals("i eat Macintosh apple", apple);
verify(appleService).saveApple("Macintosh");
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories { jcenter() }
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
useJUnitPlatform()
}
MockSettings#lenient
if we need all the stubs of a mock instance to be lenient then marking it with lenient
is redundant, For these types of scenarios Mockito Provides MockSettings
interface.
all the mocks created by
@Mock(lenient = true)
or
AppleService appleService = mock(AppleService.class, withSettings().lenient());
will be considered as lenient.
package com.example.mokito3.sujan;
public class AppleService {
public String saveApple(String apple) {
String appleString = "i love " + apple + " apple";
return appleString;
}
}
package com.example.mokito3.sujan;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
public class AppleServiceTest {
@Mock(lenient = true)
private AppleService appleService;
@Test
void saveAppleWithStaticMockTest() {
AppleService appleService = mock(AppleService.class, withSettings().lenient());
when(appleService.saveApple("Fuji")).thenReturn("i eat Fuji apple");
when(appleService.saveApple("Macintosh")).thenReturn("i eat Macintosh apple");
String apple = appleService.saveApple("Macintosh");
Assertions.assertEquals("i eat Macintosh apple", apple);
verify(appleService).saveApple("Macintosh");
}
@Test
void saveAppleWithAnnotationMockTest() {
when(appleService.saveApple("Fuji")).thenReturn("i eat Fuji apple");
when(appleService.saveApple("Macintosh")).thenReturn("i eat Macintosh apple");
String apple = appleService.saveApple("Macintosh");
Assertions.assertEquals("i eat Macintosh apple", apple);
verify(appleService).saveApple("Macintosh");
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories { jcenter() }
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
useJUnitPlatform()
}