Skip to content

Commit 710182f

Browse files
varomorfpivovarit
authored andcommitted
Code for JMockit 101 (eugenp#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.
1 parent 89373cd commit 710182f

17 files changed

Lines changed: 167 additions & 6 deletions

File tree

‎mocks/jmockit/README.md‎

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

‎mocks/jmockit/pom.xml‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
5+
<parent>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>mocks</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>jmockit</artifactId>
13+
<name>jmockit</name>
14+
15+
<properties>
16+
<junit.version>4.12</junit.version>
17+
<jmockit.version>1.24</jmockit.version>
18+
19+
<!-- maven plugins -->
20+
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
21+
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>junit</groupId>
27+
<artifactId>junit</artifactId>
28+
<version>${junit.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.jmockit</groupId>
34+
<artifactId>jmockit</artifactId>
35+
<version>${jmockit.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<finalName>jmockit</finalName>
42+
<resources>
43+
<resource>
44+
<directory>src/main/resources</directory>
45+
<filtering>true</filtering>
46+
</resource>
47+
</resources>
48+
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-compiler-plugin</artifactId>
53+
<version>${maven-compiler-plugin.version}</version>
54+
<configuration>
55+
<source>1.8</source>
56+
<target>1.8</target>
57+
</configuration>
58+
</plugin>
59+
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-surefire-plugin</artifactId>
63+
<version>${maven-surefire-plugin.version}</version>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.mocks.jmockit;
2+
3+
public class Collaborator {
4+
public boolean collaborate(String string){
5+
return false;
6+
}
7+
public void receive(boolean bool){
8+
//NOOP
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.baeldung.mocks.jmockit;
2+
3+
public class Model {
4+
public String getInfo(){
5+
return "info";
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.mocks.jmockit;
2+
3+
public class Performer {
4+
private Collaborator collaborator;
5+
6+
public void perform(Model model){
7+
boolean value = collaborator.collaborate(model.getInfo());
8+
collaborator.receive(value);
9+
}
10+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.mocks.jmockit;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
6+
import mockit.*;
7+
import mockit.integration.junit4.JMockit;
8+
9+
@RunWith(JMockit.class)
10+
public class PerformerTest {
11+
12+
@Injectable
13+
private Collaborator collaborator;
14+
15+
@Tested
16+
private Performer performer;
17+
18+
@Test
19+
public void testThePerformMethod(@Mocked Model model) {
20+
new Expectations() {{
21+
model.getInfo();result = "bar";
22+
collaborator.collaborate("bar"); result = true;
23+
}};
24+
performer.perform(model);
25+
new Verifications() {{
26+
collaborator.receive(true);
27+
}};
28+
}
29+
30+
}
File renamed without changes.
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<groupId>org.baeldung</groupId>
5-
<artifactId>mock-comparisons</artifactId>
6-
<version>0.1-SNAPSHOT</version>
74

8-
<name>mockito</name>
5+
<parent>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>mocks</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>mock-comparisons</artifactId>
13+
<name>mock-comparisons</name>
914

1015
<properties>
1116
<junit.version>4.12</junit.version>

mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginController.java renamed to mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginController.java

File renamed without changes.

mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginDao.java renamed to mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginDao.java

File renamed without changes.

0 commit comments

Comments
 (0)