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:
Álvaro Fernández González 2016-07-06 09:36:46 +02:00 committed by Grzegorz Piwowarek
parent 89373cd1eb
commit 710182ff92
17 changed files with 167 additions and 6 deletions

7
mocks/jmockit/README.md Normal file
View File

@ -0,0 +1,7 @@
=========
## JMockit realated tutorials
### Relevant Articles:
- [JMockit 101](http://www.baeldung.com/jmockit-101)

68
mocks/jmockit/pom.xml Normal file
View File

@ -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>

View File

@ -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
}
}

View File

@ -0,0 +1,7 @@
package org.baeldung.mocks.jmockit;
public class Model {
public String getInfo(){
return "info";
}
}

View File

@ -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);
}
}

View File

@ -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);
}};
}
}

View File

@ -1,11 +1,16 @@
<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>
<groupId>org.baeldung</groupId>
<artifactId>mock-comparisons</artifactId>
<version>0.1-SNAPSHOT</version>
<name>mockito</name>
<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>

20
mocks/pom.xml Normal file
View File

@ -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>

View File

@ -7,8 +7,12 @@
<name>parent-modules</name>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>apache-fop</module>
<module>assertj</module>
<module>core-java</module>
@ -25,7 +29,7 @@
<module>jooq-spring</module>
<module>json-path</module>
<module>mockito</module>
<module>mock-comparisons</module>
<module>mocks</module>
<module>jee7schedule</module>
<!-- <module>jpa-storedprocedure</module> -->
<module>querydsl</module>