Mockito provides mocks and stubs corresponding to it. all the responses of mocks will return the preconfigured stubs else if the stub is not found then will use Nice Mock functionality and return null.
Mockito provides a way to delegate all the calls of a mock to another mock. This feature especially useful for the spy or mock which are difficult to mock.
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.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.AdditionalAnswers;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
@Test
void saveAppleMockTest() {
AppleService appleServiceOriginal = mock(AppleService.class);
when(appleServiceOriginal.saveApple("Macintosh")).thenReturn("i love apple");
/* all the invocations of appleServiceMock will be deligate to appleServiceOriginal */
AppleService appleServiceMock = mock(AppleService.class, AdditionalAnswers.delegatesTo(appleServiceOriginal));
String apple = appleServiceMock.saveApple("Macintosh");
System.out.println("apple = " + apple);
assertEquals("i love apple", apple);
verify(appleServiceOriginal).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()
}
Additional Answers DelegatesTo for Different Type
DelegatesTo method is not limited to deligate the calls to same method, it can call different type or class as well. The only requirement is that another class must have a matching method or else Mockito will throw exception.
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;
public class AppleServiceOriginal {
public String saveApple(String apple) {
String appleString = "i love " + apple + " apple";
return appleString;
}
}
package com.example.mokito3.sujan;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.AdditionalAnswers;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
@Test
void saveAppleMockTest() {
AppleServiceOriginal appleServiceOriginal = mock(AppleServiceOriginal.class);
when(appleServiceOriginal.saveApple("Macintosh")).thenReturn("i love apple");
/* all the invocations of mock AppleServiceMock class will be delegate To to AppleServiceOriginal class */
AppleService appleServiceMock = mock(AppleService.class, AdditionalAnswers.delegatesTo(appleServiceOriginal));
String apple = appleServiceMock.saveApple("Macintosh");
System.out.println("apple = " + apple);
assertEquals("i love apple", apple);
verify(appleServiceOriginal).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()
}