Skip to content

Commit 9a91590

Browse files
varomorfpivovarit
authored andcommitted
Add mock-comparisons module (eugenp#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.
1 parent b9ddf5f commit 9a91590

10 files changed

Lines changed: 623 additions & 0 deletions

File tree

‎mock-comparisons/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=========
2+
3+
## Mock comparison realated tutorials
4+
5+
6+
### Relevant Articles:
7+
- [Mockito vs EasyMock vs JMockit](http://www.baeldung.com/mockito-vs-easymock-vs-jmockit)

‎mock-comparisons/pom.xml‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.baeldung</groupId>
5+
<artifactId>mock-comparisons</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
8+
<name>mockito</name>
9+
10+
<dependencies>
11+
12+
<!-- utils -->
13+
14+
<!-- web -->
15+
16+
<!-- test scoped -->
17+
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>${junit.version}</version>
22+
<scope>test</scope>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.mockito</groupId>
27+
<artifactId>mockito-core</artifactId>
28+
<version>${mockito.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.easymock</groupId>
34+
<artifactId>easymock</artifactId>
35+
<version>${easymock.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.jmockit</groupId>
41+
<artifactId>jmockit</artifactId>
42+
<version>${jmockit.version}</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
</dependencies>
47+
48+
<build>
49+
<finalName>mock-comparisons</finalName>
50+
<resources>
51+
<resource>
52+
<directory>src/main/resources</directory>
53+
<filtering>true</filtering>
54+
</resource>
55+
</resources>
56+
57+
<plugins>
58+
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-compiler-plugin</artifactId>
62+
<version>${maven-compiler-plugin.version}</version>
63+
<configuration>
64+
<source>1.7</source>
65+
<target>1.7</target>
66+
</configuration>
67+
</plugin>
68+
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-surefire-plugin</artifactId>
72+
<version>${maven-surefire-plugin.version}</version>
73+
</plugin>
74+
75+
</plugins>
76+
77+
</build>
78+
79+
<properties>
80+
<!-- Spring -->
81+
82+
<!-- persistence -->
83+
84+
<!-- logging -->
85+
86+
<!-- various -->
87+
88+
<!-- util -->
89+
90+
<!-- testing -->
91+
<junit.version>4.12</junit.version>
92+
<mockito.version>1.10.19</mockito.version>
93+
<easymock.version>3.4</easymock.version>
94+
<jmockit.version>1.24</jmockit.version>
95+
96+
<!-- maven plugins -->
97+
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
98+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
99+
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
100+
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
101+
<cargo-maven2-plugin.version>1.4.14</cargo-maven2-plugin.version>
102+
103+
</properties>
104+
105+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.mocks.testCase;
2+
3+
public class LoginController {
4+
5+
public LoginService loginService;
6+
7+
public String login(UserForm userForm){
8+
if(null == userForm){
9+
return "ERROR";
10+
}else{
11+
boolean logged;
12+
13+
try {
14+
logged = loginService.login(userForm);
15+
} catch (Exception e) {
16+
return "ERROR";
17+
}
18+
19+
if(logged){
20+
loginService.setCurrentUser(userForm.getUsername());
21+
return "OK";
22+
}else{
23+
return "KO";
24+
}
25+
}
26+
}
27+
28+
// standard setters and getters
29+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.mocks.testCase;
2+
3+
public class LoginDao {
4+
5+
public int login(UserForm userForm){
6+
//actual call to a third party library
7+
return 0;
8+
}
9+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.mocks.testCase;
2+
3+
public class LoginService {
4+
5+
private LoginDao loginDao;
6+
7+
private String currentUser;
8+
9+
public boolean login(UserForm userForm) {
10+
assert null != userForm;
11+
12+
int loginResults = loginDao.login(userForm);
13+
14+
switch (loginResults){
15+
case 1:
16+
return true;
17+
default:
18+
return false;
19+
}
20+
}
21+
22+
public void setCurrentUser(String username) {
23+
if(null != username){
24+
this.currentUser = username;
25+
}
26+
}
27+
28+
public void setLoginDao(LoginDao loginDao) {
29+
this.loginDao = loginDao;
30+
}
31+
32+
// standard setters and getters
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.mocks.testCase;
2+
3+
public class UserForm {
4+
5+
// public access modifiers as only for testing
6+
7+
public String password;
8+
9+
public String username;
10+
11+
public String getUsername(){
12+
return username;
13+
}
14+
15+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package org.baeldung.mocks.easymock;
2+
3+
import org.baeldung.mocks.testCase.LoginController;
4+
import org.baeldung.mocks.testCase.LoginDao;
5+
import org.baeldung.mocks.testCase.LoginService;
6+
import org.baeldung.mocks.testCase.UserForm;
7+
import org.easymock.*;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
12+
/**
13+
* <p>Test for LoginController using EasyMock.</p>
14+
* Created by Alvaro on 12/06/2016.
15+
*/
16+
@RunWith(EasyMockRunner.class)
17+
public class LoginControllerTest {
18+
19+
@Mock
20+
private LoginDao loginDao;
21+
22+
@Mock
23+
private LoginService loginService;
24+
25+
@TestSubject
26+
private LoginController loginController = new LoginController();
27+
28+
@Test
29+
public void assertThatNoMethodHasBeenCalled() {
30+
EasyMock.replay(loginService);
31+
loginController.login(null);
32+
33+
// no method called
34+
EasyMock.verify(loginService);
35+
}
36+
37+
@Test
38+
public void assertTwoMethodsHaveBeenCalled() {
39+
UserForm userForm = new UserForm();
40+
userForm.username = "foo";
41+
EasyMock.expect(loginService.login(userForm)).andReturn(true);
42+
loginService.setCurrentUser("foo");
43+
EasyMock.replay(loginService);
44+
45+
String login = loginController.login(userForm);
46+
47+
Assert.assertEquals("OK", login);
48+
EasyMock.verify(loginService);
49+
}
50+
51+
@Test
52+
public void assertOnlyOneMethodHasBeenCalled() {
53+
UserForm userForm = new UserForm();
54+
userForm.username = "foo";
55+
EasyMock.expect(loginService.login(userForm)).andReturn(false);
56+
EasyMock.replay(loginService);
57+
58+
String login = loginController.login(userForm);
59+
60+
Assert.assertEquals("KO", login);
61+
EasyMock.verify(loginService);
62+
}
63+
64+
@Test
65+
public void mockExceptionThrowing() {
66+
UserForm userForm = new UserForm();
67+
EasyMock.expect(loginService.login(userForm)).andThrow(new IllegalArgumentException());
68+
EasyMock.replay(loginService);
69+
70+
String login = loginController.login(userForm);
71+
72+
Assert.assertEquals("ERROR", login);
73+
EasyMock.verify(loginService);
74+
}
75+
76+
@Test
77+
public void mockAnObjectToPassAround() {
78+
UserForm userForm = EasyMock.mock(UserForm.class);
79+
EasyMock.expect(userForm.getUsername()).andReturn("foo");
80+
EasyMock.expect(loginService.login(userForm)).andReturn(true);
81+
loginService.setCurrentUser("foo");
82+
EasyMock.replay(userForm);
83+
EasyMock.replay(loginService);
84+
85+
String login = loginController.login(userForm);
86+
87+
Assert.assertEquals("OK", login);
88+
EasyMock.verify(userForm);
89+
EasyMock.verify(loginService);
90+
}
91+
92+
@Test
93+
public void argumentMatching() {
94+
UserForm userForm = new UserForm();
95+
userForm.username = "foo";
96+
// default matcher
97+
EasyMock.expect(loginService.login(EasyMock.isA(UserForm.class))).andReturn(true);
98+
// complex matcher
99+
loginService.setCurrentUser(specificArgumentMatching("foo"));
100+
EasyMock.replay(loginService);
101+
102+
String login = loginController.login(userForm);
103+
104+
Assert.assertEquals("OK", login);
105+
EasyMock.verify(loginService);
106+
}
107+
108+
private static String specificArgumentMatching(final String expected) {
109+
EasyMock.reportMatcher(new IArgumentMatcher() {
110+
@Override
111+
public boolean matches(Object argument) {
112+
return argument instanceof String && ((String) argument).startsWith(expected);
113+
}
114+
115+
@Override
116+
public void appendTo(StringBuffer buffer) {
117+
//NOOP
118+
}
119+
});
120+
return null;
121+
}
122+
123+
@Test
124+
public void partialMocking() {
125+
UserForm userForm = new UserForm();
126+
userForm.username = "foo";
127+
// use partial mock
128+
LoginService loginServicePartial = EasyMock.partialMockBuilder(LoginService.class).
129+
addMockedMethod("setCurrentUser").createMock();
130+
loginServicePartial.setCurrentUser("foo");
131+
// let service's login use implementation so let's mock DAO call
132+
EasyMock.expect(loginDao.login(userForm)).andReturn(1);
133+
134+
loginServicePartial.setLoginDao(loginDao);
135+
loginController.loginService = loginServicePartial;
136+
137+
EasyMock.replay(loginDao);
138+
EasyMock.replay(loginServicePartial);
139+
140+
String login = loginController.login(userForm);
141+
142+
Assert.assertEquals("OK", login);
143+
// verify mocked call
144+
EasyMock.verify(loginServicePartial);
145+
EasyMock.verify(loginDao);
146+
}
147+
}

0 commit comments

Comments
 (0)