Refactor mock-comparisons
This commit is contained in:
parent
9a915901af
commit
d9223b24fb
|
@ -7,14 +7,18 @@
|
|||
|
||||
<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>
|
||||
|
||||
<!-- utils -->
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
@ -61,8 +65,8 @@
|
|||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
@ -76,30 +80,5 @@
|
|||
|
||||
</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>
|
|
@ -4,10 +4,10 @@ public class LoginController {
|
|||
|
||||
public LoginService loginService;
|
||||
|
||||
public String login(UserForm userForm){
|
||||
if(null == userForm){
|
||||
public String login(UserForm userForm) {
|
||||
if (null == userForm) {
|
||||
return "ERROR";
|
||||
}else{
|
||||
} else {
|
||||
boolean logged;
|
||||
|
||||
try {
|
||||
|
@ -16,10 +16,10 @@ public class LoginController {
|
|||
return "ERROR";
|
||||
}
|
||||
|
||||
if(logged){
|
||||
if (logged) {
|
||||
loginService.setCurrentUser(userForm.getUsername());
|
||||
return "OK";
|
||||
}else{
|
||||
} else {
|
||||
return "KO";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.baeldung.mocks.testCase;
|
|||
|
||||
public class LoginDao {
|
||||
|
||||
public int login(UserForm userForm){
|
||||
public int login(UserForm userForm) {
|
||||
//actual call to a third party library
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ public class LoginService {
|
|||
|
||||
int loginResults = loginDao.login(userForm);
|
||||
|
||||
switch (loginResults){
|
||||
switch (loginResults) {
|
||||
case 1:
|
||||
return true;
|
||||
default:
|
||||
|
@ -20,7 +20,7 @@ public class LoginService {
|
|||
}
|
||||
|
||||
public void setCurrentUser(String username) {
|
||||
if(null != username){
|
||||
if (null != username) {
|
||||
this.currentUser = username;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ public class UserForm {
|
|||
|
||||
public String username;
|
||||
|
||||
public String getUsername(){
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,10 +9,6 @@ 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 {
|
||||
|
||||
|
|
|
@ -12,10 +12,6 @@ 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 {
|
||||
|
||||
|
@ -32,7 +28,8 @@ public class LoginControllerTest {
|
|||
public void assertThatNoMethodHasBeenCalled() {
|
||||
loginController.login(null);
|
||||
// no method called
|
||||
new FullVerifications(loginService) {};
|
||||
new FullVerifications(loginService) {
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -40,14 +37,16 @@ public class LoginControllerTest {
|
|||
final UserForm userForm = new UserForm();
|
||||
userForm.username = "foo";
|
||||
new Expectations() {{
|
||||
loginService.login(userForm); result = true;
|
||||
loginService.login(userForm);
|
||||
result = true;
|
||||
loginService.setCurrentUser("foo");
|
||||
}};
|
||||
|
||||
String login = loginController.login(userForm);
|
||||
|
||||
Assert.assertEquals("OK", login);
|
||||
new FullVerifications(loginService) {};
|
||||
new FullVerifications(loginService) {
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -55,43 +54,51 @@ public class LoginControllerTest {
|
|||
final UserForm userForm = new UserForm();
|
||||
userForm.username = "foo";
|
||||
new Expectations() {{
|
||||
loginService.login(userForm); result = false;
|
||||
loginService.login(userForm);
|
||||
result = false;
|
||||
// no expectation for setCurrentUser
|
||||
}};
|
||||
|
||||
String login = loginController.login(userForm);
|
||||
|
||||
Assert.assertEquals("KO", login);
|
||||
new FullVerifications(loginService) {};
|
||||
new FullVerifications(loginService) {
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockExceptionThrowing() {
|
||||
final UserForm userForm = new UserForm();
|
||||
new Expectations() {{
|
||||
loginService.login(userForm); result = new IllegalArgumentException();
|
||||
loginService.login(userForm);
|
||||
result = new IllegalArgumentException();
|
||||
// no expectation for setCurrentUser
|
||||
}};
|
||||
|
||||
String login = loginController.login(userForm);
|
||||
|
||||
Assert.assertEquals("ERROR", login);
|
||||
new FullVerifications(loginService) {};
|
||||
new FullVerifications(loginService) {
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mockAnObjectToPassAround(@Mocked final UserForm userForm) {
|
||||
new Expectations() {{
|
||||
userForm.getUsername(); result = "foo";
|
||||
loginService.login(userForm); result = true;
|
||||
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) {};
|
||||
new FullVerifications(loginService) {
|
||||
};
|
||||
new FullVerifications(userForm) {
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -119,7 +126,8 @@ public class LoginControllerTest {
|
|||
String login = loginController.login(userForm);
|
||||
|
||||
Assert.assertEquals("OK", login);
|
||||
new FullVerifications(loginService) {};
|
||||
new FullVerifications(loginService) {
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -133,7 +141,8 @@ public class LoginControllerTest {
|
|||
userForm.username = "foo";
|
||||
// let service's login use implementation so let's mock DAO call
|
||||
new Expectations() {{
|
||||
loginDao.login(userForm); result = 1;
|
||||
loginDao.login(userForm);
|
||||
result = 1;
|
||||
// no expectation for loginService.login
|
||||
partialLoginService.setCurrentUser("foo");
|
||||
}};
|
||||
|
@ -142,7 +151,9 @@ public class LoginControllerTest {
|
|||
|
||||
Assert.assertEquals("OK", login);
|
||||
// verify mocked call
|
||||
new FullVerifications(partialLoginService) {};
|
||||
new FullVerifications(loginDao) {};
|
||||
new FullVerifications(partialLoginService) {
|
||||
};
|
||||
new FullVerifications(loginDao) {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,10 +9,6 @@ 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
|
||||
|
|
Loading…
Reference in New Issue