Add mock-comparisons module (#453)
* Add new module for mocks comparison. * Add sources for testing. * Changes on testCase. * Enter some tests for mockito. * More tests for Mockito. * Even more tests. * Add the rest of the mocking libraries. * Javadoc on test. * Test bare bones for EasyMock. * Fist kind of test and setup. * Add tests using EasyMock with a change on LoginService. * Create LoginControllerTest.java * Test setup * [JMockit] No method called test. * [JMockit] Two methods called test. * [JMockit] One method called test. * [JMockit] Exception mock test * [JMockit] Mocked object to pass around test. * [JMockit] Custom matcher test. * [JMockit] Partial mocking test. * [JMockit] Fix with IDE. * Not stubs. Mocks. MOCKS!!! * Remove unnecesary import.
This commit is contained in:
parent
b9ddf5f8be
commit
9a915901af
7
mock-comparisons/README.md
Normal file
7
mock-comparisons/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
=========
|
||||||
|
|
||||||
|
## Mock comparison realated tutorials
|
||||||
|
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Mockito vs EasyMock vs JMockit](http://www.baeldung.com/mockito-vs-easymock-vs-jmockit)
|
105
mock-comparisons/pom.xml
Normal file
105
mock-comparisons/pom.xml
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>org.baeldung</groupId>
|
||||||
|
<artifactId>mock-comparisons</artifactId>
|
||||||
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<name>mockito</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- utils -->
|
||||||
|
|
||||||
|
<!-- web -->
|
||||||
|
|
||||||
|
<!-- test scoped -->
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<version>${mockito.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.easymock</groupId>
|
||||||
|
<artifactId>easymock</artifactId>
|
||||||
|
<version>${easymock.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jmockit</groupId>
|
||||||
|
<artifactId>jmockit</artifactId>
|
||||||
|
<version>${jmockit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>mock-comparisons</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.7</source>
|
||||||
|
<target>1.7</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
</plugins>
|
||||||
|
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- Spring -->
|
||||||
|
|
||||||
|
<!-- persistence -->
|
||||||
|
|
||||||
|
<!-- logging -->
|
||||||
|
|
||||||
|
<!-- various -->
|
||||||
|
|
||||||
|
<!-- util -->
|
||||||
|
|
||||||
|
<!-- testing -->
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
<mockito.version>1.10.19</mockito.version>
|
||||||
|
<easymock.version>3.4</easymock.version>
|
||||||
|
<jmockit.version>1.24</jmockit.version>
|
||||||
|
|
||||||
|
<!-- maven plugins -->
|
||||||
|
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
|
||||||
|
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||||
|
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
|
||||||
|
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
||||||
|
<cargo-maven2-plugin.version>1.4.14</cargo-maven2-plugin.version>
|
||||||
|
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,29 @@
|
|||||||
|
package org.baeldung.mocks.testCase;
|
||||||
|
|
||||||
|
public class LoginController {
|
||||||
|
|
||||||
|
public LoginService loginService;
|
||||||
|
|
||||||
|
public String login(UserForm userForm){
|
||||||
|
if(null == userForm){
|
||||||
|
return "ERROR";
|
||||||
|
}else{
|
||||||
|
boolean logged;
|
||||||
|
|
||||||
|
try {
|
||||||
|
logged = loginService.login(userForm);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "ERROR";
|
||||||
|
}
|
||||||
|
|
||||||
|
if(logged){
|
||||||
|
loginService.setCurrentUser(userForm.getUsername());
|
||||||
|
return "OK";
|
||||||
|
}else{
|
||||||
|
return "KO";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// standard setters and getters
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package org.baeldung.mocks.testCase;
|
||||||
|
|
||||||
|
public class LoginDao {
|
||||||
|
|
||||||
|
public int login(UserForm userForm){
|
||||||
|
//actual call to a third party library
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package org.baeldung.mocks.testCase;
|
||||||
|
|
||||||
|
public class LoginService {
|
||||||
|
|
||||||
|
private LoginDao loginDao;
|
||||||
|
|
||||||
|
private String currentUser;
|
||||||
|
|
||||||
|
public boolean login(UserForm userForm) {
|
||||||
|
assert null != userForm;
|
||||||
|
|
||||||
|
int loginResults = loginDao.login(userForm);
|
||||||
|
|
||||||
|
switch (loginResults){
|
||||||
|
case 1:
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentUser(String username) {
|
||||||
|
if(null != username){
|
||||||
|
this.currentUser = username;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLoginDao(LoginDao loginDao) {
|
||||||
|
this.loginDao = loginDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
// standard setters and getters
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package org.baeldung.mocks.testCase;
|
||||||
|
|
||||||
|
public class UserForm {
|
||||||
|
|
||||||
|
// public access modifiers as only for testing
|
||||||
|
|
||||||
|
public String password;
|
||||||
|
|
||||||
|
public String username;
|
||||||
|
|
||||||
|
public String getUsername(){
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,147 @@
|
|||||||
|
package org.baeldung.mocks.easymock;
|
||||||
|
|
||||||
|
import org.baeldung.mocks.testCase.LoginController;
|
||||||
|
import org.baeldung.mocks.testCase.LoginDao;
|
||||||
|
import org.baeldung.mocks.testCase.LoginService;
|
||||||
|
import org.baeldung.mocks.testCase.UserForm;
|
||||||
|
import org.easymock.*;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Test for LoginController using EasyMock.</p>
|
||||||
|
* Created by Alvaro on 12/06/2016.
|
||||||
|
*/
|
||||||
|
@RunWith(EasyMockRunner.class)
|
||||||
|
public class LoginControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private LoginDao loginDao;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private LoginService loginService;
|
||||||
|
|
||||||
|
@TestSubject
|
||||||
|
private LoginController loginController = new LoginController();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertThatNoMethodHasBeenCalled() {
|
||||||
|
EasyMock.replay(loginService);
|
||||||
|
loginController.login(null);
|
||||||
|
|
||||||
|
// no method called
|
||||||
|
EasyMock.verify(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertTwoMethodsHaveBeenCalled() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
EasyMock.expect(loginService.login(userForm)).andReturn(true);
|
||||||
|
loginService.setCurrentUser("foo");
|
||||||
|
EasyMock.replay(loginService);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
EasyMock.verify(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertOnlyOneMethodHasBeenCalled() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
EasyMock.expect(loginService.login(userForm)).andReturn(false);
|
||||||
|
EasyMock.replay(loginService);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("KO", login);
|
||||||
|
EasyMock.verify(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mockExceptionThrowing() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
EasyMock.expect(loginService.login(userForm)).andThrow(new IllegalArgumentException());
|
||||||
|
EasyMock.replay(loginService);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("ERROR", login);
|
||||||
|
EasyMock.verify(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mockAnObjectToPassAround() {
|
||||||
|
UserForm userForm = EasyMock.mock(UserForm.class);
|
||||||
|
EasyMock.expect(userForm.getUsername()).andReturn("foo");
|
||||||
|
EasyMock.expect(loginService.login(userForm)).andReturn(true);
|
||||||
|
loginService.setCurrentUser("foo");
|
||||||
|
EasyMock.replay(userForm);
|
||||||
|
EasyMock.replay(loginService);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
EasyMock.verify(userForm);
|
||||||
|
EasyMock.verify(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void argumentMatching() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
// default matcher
|
||||||
|
EasyMock.expect(loginService.login(EasyMock.isA(UserForm.class))).andReturn(true);
|
||||||
|
// complex matcher
|
||||||
|
loginService.setCurrentUser(specificArgumentMatching("foo"));
|
||||||
|
EasyMock.replay(loginService);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
EasyMock.verify(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String specificArgumentMatching(final String expected) {
|
||||||
|
EasyMock.reportMatcher(new IArgumentMatcher() {
|
||||||
|
@Override
|
||||||
|
public boolean matches(Object argument) {
|
||||||
|
return argument instanceof String && ((String) argument).startsWith(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void appendTo(StringBuffer buffer) {
|
||||||
|
//NOOP
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void partialMocking() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
// use partial mock
|
||||||
|
LoginService loginServicePartial = EasyMock.partialMockBuilder(LoginService.class).
|
||||||
|
addMockedMethod("setCurrentUser").createMock();
|
||||||
|
loginServicePartial.setCurrentUser("foo");
|
||||||
|
// let service's login use implementation so let's mock DAO call
|
||||||
|
EasyMock.expect(loginDao.login(userForm)).andReturn(1);
|
||||||
|
|
||||||
|
loginServicePartial.setLoginDao(loginDao);
|
||||||
|
loginController.loginService = loginServicePartial;
|
||||||
|
|
||||||
|
EasyMock.replay(loginDao);
|
||||||
|
EasyMock.replay(loginServicePartial);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
// verify mocked call
|
||||||
|
EasyMock.verify(loginServicePartial);
|
||||||
|
EasyMock.verify(loginDao);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,148 @@
|
|||||||
|
package org.baeldung.mocks.jmockit;
|
||||||
|
|
||||||
|
import mockit.*;
|
||||||
|
import mockit.integration.junit4.JMockit;
|
||||||
|
import org.baeldung.mocks.testCase.LoginController;
|
||||||
|
import org.baeldung.mocks.testCase.LoginDao;
|
||||||
|
import org.baeldung.mocks.testCase.LoginService;
|
||||||
|
import org.baeldung.mocks.testCase.UserForm;
|
||||||
|
import org.hamcrest.BaseMatcher;
|
||||||
|
import org.hamcrest.Description;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Test for LoginController using JMockit.</p>
|
||||||
|
* Created by Alvaro on 12/06/2016.
|
||||||
|
*/
|
||||||
|
@RunWith(JMockit.class)
|
||||||
|
public class LoginControllerTest {
|
||||||
|
|
||||||
|
@Injectable
|
||||||
|
private LoginDao loginDao;
|
||||||
|
|
||||||
|
@Injectable
|
||||||
|
private LoginService loginService;
|
||||||
|
|
||||||
|
@Tested
|
||||||
|
private LoginController loginController;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertThatNoMethodHasBeenCalled() {
|
||||||
|
loginController.login(null);
|
||||||
|
// no method called
|
||||||
|
new FullVerifications(loginService) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertTwoMethodsHaveBeenCalled() {
|
||||||
|
final UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
new Expectations() {{
|
||||||
|
loginService.login(userForm); result = true;
|
||||||
|
loginService.setCurrentUser("foo");
|
||||||
|
}};
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
new FullVerifications(loginService) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertOnlyOneMethodHasBeenCalled() {
|
||||||
|
final UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
new Expectations() {{
|
||||||
|
loginService.login(userForm); result = false;
|
||||||
|
// no expectation for setCurrentUser
|
||||||
|
}};
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("KO", login);
|
||||||
|
new FullVerifications(loginService) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mockExceptionThrowing() {
|
||||||
|
final UserForm userForm = new UserForm();
|
||||||
|
new Expectations() {{
|
||||||
|
loginService.login(userForm); result = new IllegalArgumentException();
|
||||||
|
// no expectation for setCurrentUser
|
||||||
|
}};
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("ERROR", login);
|
||||||
|
new FullVerifications(loginService) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mockAnObjectToPassAround(@Mocked final UserForm userForm) {
|
||||||
|
new Expectations() {{
|
||||||
|
userForm.getUsername(); result = "foo";
|
||||||
|
loginService.login(userForm); result = true;
|
||||||
|
loginService.setCurrentUser("foo");
|
||||||
|
}};
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
new FullVerifications(loginService) {};
|
||||||
|
new FullVerifications(userForm) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void argumentMatching() {
|
||||||
|
final UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
// default matcher
|
||||||
|
new Expectations() {{
|
||||||
|
loginService.login((UserForm) any);
|
||||||
|
result = true;
|
||||||
|
// complex matcher
|
||||||
|
loginService.setCurrentUser(withArgThat(new BaseMatcher<String>() {
|
||||||
|
@Override
|
||||||
|
public boolean matches(Object item) {
|
||||||
|
return item instanceof String && ((String) item).startsWith("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void describeTo(Description description) {
|
||||||
|
//NOOP
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}};
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
new FullVerifications(loginService) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void partialMocking() {
|
||||||
|
// use partial mock
|
||||||
|
final LoginService partialLoginService = new LoginService();
|
||||||
|
partialLoginService.setLoginDao(loginDao);
|
||||||
|
loginController.loginService = partialLoginService;
|
||||||
|
|
||||||
|
final UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
// let service's login use implementation so let's mock DAO call
|
||||||
|
new Expectations() {{
|
||||||
|
loginDao.login(userForm); result = 1;
|
||||||
|
// no expectation for loginService.login
|
||||||
|
partialLoginService.setCurrentUser("foo");
|
||||||
|
}};
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
// verify mocked call
|
||||||
|
new FullVerifications(partialLoginService) {};
|
||||||
|
new FullVerifications(loginDao) {};
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,129 @@
|
|||||||
|
package org.baeldung.mocks.mockito;
|
||||||
|
|
||||||
|
import org.baeldung.mocks.testCase.LoginController;
|
||||||
|
import org.baeldung.mocks.testCase.LoginDao;
|
||||||
|
import org.baeldung.mocks.testCase.LoginService;
|
||||||
|
import org.baeldung.mocks.testCase.UserForm;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Test for LoginController using Mockito.</p>
|
||||||
|
* Created by Alvaro on 12/06/2016.
|
||||||
|
*/
|
||||||
|
public class LoginControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private LoginDao loginDao;
|
||||||
|
|
||||||
|
@Spy
|
||||||
|
@InjectMocks
|
||||||
|
private LoginService spiedLoginService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private LoginService loginService;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private LoginController loginController;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
loginController = new LoginController();
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertThatNoMethodHasBeenCalled() {
|
||||||
|
loginController.login(null);
|
||||||
|
// no method called
|
||||||
|
Mockito.verifyZeroInteractions(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertTwoMethodsHaveBeenCalled() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
Mockito.when(loginService.login(userForm)).thenReturn(true);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
Mockito.verify(loginService).login(userForm);
|
||||||
|
Mockito.verify(loginService).setCurrentUser("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void assertOnlyOneMethodHasBeenCalled() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
Mockito.when(loginService.login(userForm)).thenReturn(false);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("KO", login);
|
||||||
|
Mockito.verify(loginService).login(userForm);
|
||||||
|
Mockito.verifyNoMoreInteractions(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mockExceptionThrowing() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
Mockito.when(loginService.login(userForm)).thenThrow(IllegalArgumentException.class);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("ERROR", login);
|
||||||
|
Mockito.verify(loginService).login(userForm);
|
||||||
|
Mockito.verifyZeroInteractions(loginService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mockAnObjectToPassAround() {
|
||||||
|
UserForm userForm = Mockito.when(Mockito.mock(UserForm.class).getUsername()).thenReturn("foo").getMock();
|
||||||
|
Mockito.when(loginService.login(userForm)).thenReturn(true);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
Mockito.verify(loginService).login(userForm);
|
||||||
|
Mockito.verify(loginService).setCurrentUser("foo");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void argumentMatching() {
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
// default matcher
|
||||||
|
Mockito.when(loginService.login(Mockito.any(UserForm.class))).thenReturn(true);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
Mockito.verify(loginService).login(userForm);
|
||||||
|
// complex matcher
|
||||||
|
Mockito.verify(loginService).setCurrentUser(Mockito.argThat(new ArgumentMatcher<String>() {
|
||||||
|
@Override
|
||||||
|
public boolean matches(Object argument) {
|
||||||
|
return argument instanceof String && ((String) argument).startsWith("foo");
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void partialMocking() {
|
||||||
|
// use partial mock
|
||||||
|
loginController.loginService = spiedLoginService;
|
||||||
|
UserForm userForm = new UserForm();
|
||||||
|
userForm.username = "foo";
|
||||||
|
// let service's login use implementation so let's mock DAO call
|
||||||
|
Mockito.when(loginDao.login(userForm)).thenReturn(1);
|
||||||
|
|
||||||
|
String login = loginController.login(userForm);
|
||||||
|
|
||||||
|
Assert.assertEquals("OK", login);
|
||||||
|
// verify mocked call
|
||||||
|
Mockito.verify(spiedLoginService).setCurrentUser("foo");
|
||||||
|
}
|
||||||
|
}
|
1
pom.xml
1
pom.xml
@ -23,6 +23,7 @@
|
|||||||
<module>jooq-spring</module>
|
<module>jooq-spring</module>
|
||||||
<module>json-path</module>
|
<module>json-path</module>
|
||||||
<module>mockito</module>
|
<module>mockito</module>
|
||||||
|
<module>mock-comparisons</module>
|
||||||
<module>jee7schedule</module>
|
<module>jee7schedule</module>
|
||||||
<!-- <module>jpa-storedprocedure</module> -->
|
<!-- <module>jpa-storedprocedure</module> -->
|
||||||
<module>querydsl</module>
|
<module>querydsl</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user