Code for JMockit 101 (#479)
* 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. * Use correct encoding. Was having problems with buildings. * Remove failing module. * Create new module mocks and move mock-comparisons there. * Add jmockit module. * Add model class. * Add collaborator class. * Add performer class. * Add performer test.
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
89373cd1eb
commit
710182ff92
@@ -0,0 +1,7 @@
|
||||
=========
|
||||
|
||||
## JMockit realated tutorials
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
- [JMockit 101](http://www.baeldung.com/jmockit-101)
|
||||
@@ -0,0 +1,68 @@
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>mocks</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>jmockit</artifactId>
|
||||
<name>jmockit</name>
|
||||
|
||||
<properties>
|
||||
<junit.version>4.12</junit.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>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.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>jmockit</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.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Collaborator {
|
||||
public boolean collaborate(String string){
|
||||
return false;
|
||||
}
|
||||
public void receive(boolean bool){
|
||||
//NOOP
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Model {
|
||||
public String getInfo(){
|
||||
return "info";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Performer {
|
||||
private Collaborator collaborator;
|
||||
|
||||
public void perform(Model model){
|
||||
boolean value = collaborator.collaborate(model.getInfo());
|
||||
collaborator.receive(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import mockit.*;
|
||||
import mockit.integration.junit4.JMockit;
|
||||
|
||||
@RunWith(JMockit.class)
|
||||
public class PerformerTest {
|
||||
|
||||
@Injectable
|
||||
private Collaborator collaborator;
|
||||
|
||||
@Tested
|
||||
private Performer performer;
|
||||
|
||||
@Test
|
||||
public void testThePerformMethod(@Mocked Model model) {
|
||||
new Expectations() {{
|
||||
model.getInfo();result = "bar";
|
||||
collaborator.collaborate("bar"); result = true;
|
||||
}};
|
||||
performer.perform(model);
|
||||
new Verifications() {{
|
||||
collaborator.receive(true);
|
||||
}};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
=========
|
||||
|
||||
## Mock comparison realated tutorials
|
||||
|
||||
|
||||
### Relevant Articles:
|
||||
- [Mockito vs EasyMock vs JMockit](http://www.baeldung.com/mockito-vs-easymock-vs-jmockit)
|
||||
@@ -0,0 +1,91 @@
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>mocks</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>mock-comparisons</artifactId>
|
||||
<name>mock-comparisons</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>
|
||||
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
<!-- 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>
|
||||
<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.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
|
||||
</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;
|
||||
}
|
||||
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
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;
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
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;
|
||||
|
||||
@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) {
|
||||
};
|
||||
}
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
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.*;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>mocks</artifactId>
|
||||
<name>mocks</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>mock-comparisons</module>
|
||||
<module>jmockit</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user