Refactor mock-comparisons

This commit is contained in:
Grzegorz Piwowarek 2016-06-18 20:21:17 +02:00
parent 9a915901af
commit d9223b24fb
8 changed files with 54 additions and 72 deletions

View File

@ -7,14 +7,18 @@
<name>mockito</name> <name>mockito</name>
<properties>
<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-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
</properties>
<dependencies> <dependencies>
<!-- utils -->
<!-- web -->
<!-- test scoped -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@ -61,8 +65,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version> <version>${maven-compiler-plugin.version}</version>
<configuration> <configuration>
<source>1.7</source> <source>1.8</source>
<target>1.7</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
@ -76,30 +80,5 @@
</build> </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> </project>

View File

@ -4,10 +4,10 @@ public class LoginController {
public LoginService loginService; public LoginService loginService;
public String login(UserForm userForm){ public String login(UserForm userForm) {
if(null == userForm){ if (null == userForm) {
return "ERROR"; return "ERROR";
}else{ } else {
boolean logged; boolean logged;
try { try {
@ -16,10 +16,10 @@ public class LoginController {
return "ERROR"; return "ERROR";
} }
if(logged){ if (logged) {
loginService.setCurrentUser(userForm.getUsername()); loginService.setCurrentUser(userForm.getUsername());
return "OK"; return "OK";
}else{ } else {
return "KO"; return "KO";
} }
} }

View File

@ -2,7 +2,7 @@ package org.baeldung.mocks.testCase;
public class LoginDao { public class LoginDao {
public int login(UserForm userForm){ public int login(UserForm userForm) {
//actual call to a third party library //actual call to a third party library
return 0; return 0;
} }

View File

@ -11,7 +11,7 @@ public class LoginService {
int loginResults = loginDao.login(userForm); int loginResults = loginDao.login(userForm);
switch (loginResults){ switch (loginResults) {
case 1: case 1:
return true; return true;
default: default:
@ -20,7 +20,7 @@ public class LoginService {
} }
public void setCurrentUser(String username) { public void setCurrentUser(String username) {
if(null != username){ if (null != username) {
this.currentUser = username; this.currentUser = username;
} }
} }

View File

@ -8,7 +8,7 @@ public class UserForm {
public String username; public String username;
public String getUsername(){ public String getUsername() {
return username; return username;
} }

View File

@ -9,10 +9,6 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
/**
* <p>Test for LoginController using EasyMock.</p>
* Created by Alvaro on 12/06/2016.
*/
@RunWith(EasyMockRunner.class) @RunWith(EasyMockRunner.class)
public class LoginControllerTest { public class LoginControllerTest {
@ -133,7 +129,7 @@ public class LoginControllerTest {
loginServicePartial.setLoginDao(loginDao); loginServicePartial.setLoginDao(loginDao);
loginController.loginService = loginServicePartial; loginController.loginService = loginServicePartial;
EasyMock.replay(loginDao); EasyMock.replay(loginDao);
EasyMock.replay(loginServicePartial); EasyMock.replay(loginServicePartial);

View File

@ -12,10 +12,6 @@ import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
/**
* <p>Test for LoginController using JMockit.</p>
* Created by Alvaro on 12/06/2016.
*/
@RunWith(JMockit.class) @RunWith(JMockit.class)
public class LoginControllerTest { public class LoginControllerTest {
@ -32,7 +28,8 @@ public class LoginControllerTest {
public void assertThatNoMethodHasBeenCalled() { public void assertThatNoMethodHasBeenCalled() {
loginController.login(null); loginController.login(null);
// no method called // no method called
new FullVerifications(loginService) {}; new FullVerifications(loginService) {
};
} }
@Test @Test
@ -40,14 +37,16 @@ public class LoginControllerTest {
final UserForm userForm = new UserForm(); final UserForm userForm = new UserForm();
userForm.username = "foo"; userForm.username = "foo";
new Expectations() {{ new Expectations() {{
loginService.login(userForm); result = true; loginService.login(userForm);
result = true;
loginService.setCurrentUser("foo"); loginService.setCurrentUser("foo");
}}; }};
String login = loginController.login(userForm); String login = loginController.login(userForm);
Assert.assertEquals("OK", login); Assert.assertEquals("OK", login);
new FullVerifications(loginService) {}; new FullVerifications(loginService) {
};
} }
@Test @Test
@ -55,43 +54,51 @@ public class LoginControllerTest {
final UserForm userForm = new UserForm(); final UserForm userForm = new UserForm();
userForm.username = "foo"; userForm.username = "foo";
new Expectations() {{ new Expectations() {{
loginService.login(userForm); result = false; loginService.login(userForm);
result = false;
// no expectation for setCurrentUser // no expectation for setCurrentUser
}}; }};
String login = loginController.login(userForm); String login = loginController.login(userForm);
Assert.assertEquals("KO", login); Assert.assertEquals("KO", login);
new FullVerifications(loginService) {}; new FullVerifications(loginService) {
};
} }
@Test @Test
public void mockExceptionThrowing() { public void mockExceptionThrowing() {
final UserForm userForm = new UserForm(); final UserForm userForm = new UserForm();
new Expectations() {{ new Expectations() {{
loginService.login(userForm); result = new IllegalArgumentException(); loginService.login(userForm);
result = new IllegalArgumentException();
// no expectation for setCurrentUser // no expectation for setCurrentUser
}}; }};
String login = loginController.login(userForm); String login = loginController.login(userForm);
Assert.assertEquals("ERROR", login); Assert.assertEquals("ERROR", login);
new FullVerifications(loginService) {}; new FullVerifications(loginService) {
};
} }
@Test @Test
public void mockAnObjectToPassAround(@Mocked final UserForm userForm) { public void mockAnObjectToPassAround(@Mocked final UserForm userForm) {
new Expectations() {{ new Expectations() {{
userForm.getUsername(); result = "foo"; userForm.getUsername();
loginService.login(userForm); result = true; result = "foo";
loginService.login(userForm);
result = true;
loginService.setCurrentUser("foo"); loginService.setCurrentUser("foo");
}}; }};
String login = loginController.login(userForm); String login = loginController.login(userForm);
Assert.assertEquals("OK", login); Assert.assertEquals("OK", login);
new FullVerifications(loginService) {}; new FullVerifications(loginService) {
new FullVerifications(userForm) {}; };
new FullVerifications(userForm) {
};
} }
@Test @Test
@ -119,7 +126,8 @@ public class LoginControllerTest {
String login = loginController.login(userForm); String login = loginController.login(userForm);
Assert.assertEquals("OK", login); Assert.assertEquals("OK", login);
new FullVerifications(loginService) {}; new FullVerifications(loginService) {
};
} }
@Test @Test
@ -133,7 +141,8 @@ public class LoginControllerTest {
userForm.username = "foo"; userForm.username = "foo";
// let service's login use implementation so let's mock DAO call // let service's login use implementation so let's mock DAO call
new Expectations() {{ new Expectations() {{
loginDao.login(userForm); result = 1; loginDao.login(userForm);
result = 1;
// no expectation for loginService.login // no expectation for loginService.login
partialLoginService.setCurrentUser("foo"); partialLoginService.setCurrentUser("foo");
}}; }};
@ -142,7 +151,9 @@ public class LoginControllerTest {
Assert.assertEquals("OK", login); Assert.assertEquals("OK", login);
// verify mocked call // verify mocked call
new FullVerifications(partialLoginService) {}; new FullVerifications(partialLoginService) {
new FullVerifications(loginDao) {}; };
new FullVerifications(loginDao) {
};
} }
} }

View File

@ -9,10 +9,6 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.*; import org.mockito.*;
/**
* <p>Test for LoginController using Mockito.</p>
* Created by Alvaro on 12/06/2016.
*/
public class LoginControllerTest { public class LoginControllerTest {
@Mock @Mock