Skip to content

Commit ffe646f

Browse files
committed
initial commit for powermockito
1 parent 29deacf commit ffe646f

5 files changed

Lines changed: 259 additions & 122 deletions

File tree

‎mockito/pom.xml‎

Lines changed: 141 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,143 @@
1-
<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">
2-
<modelVersion>4.0.0</modelVersion>
3-
<groupId>com.baeldung</groupId>
4-
<artifactId>mockito</artifactId>
5-
<version>0.1-SNAPSHOT</version>
6-
7-
<name>mockito</name>
8-
9-
<dependencies>
10-
11-
<!-- utils -->
12-
13-
<dependency>
14-
<groupId>com.google.guava</groupId>
15-
<artifactId>guava</artifactId>
16-
<version>${guava.version}</version>
17-
</dependency>
18-
19-
<dependency>
20-
<groupId>org.apache.commons</groupId>
21-
<artifactId>commons-lang3</artifactId>
22-
<version>${commons-lang3.version}</version>
23-
</dependency>
24-
25-
<!-- web -->
26-
27-
<!-- test scoped -->
28-
29-
<dependency>
30-
<groupId>junit</groupId>
31-
<artifactId>junit</artifactId>
32-
<version>${junit.version}</version>
33-
<scope>test</scope>
34-
</dependency>
35-
36-
<dependency>
37-
<groupId>org.hamcrest</groupId>
38-
<artifactId>hamcrest-core</artifactId>
39-
<version>${org.hamcrest.version}</version>
40-
<scope>test</scope>
41-
</dependency>
42-
<dependency>
43-
<groupId>org.hamcrest</groupId>
44-
<artifactId>hamcrest-library</artifactId>
45-
<version>${org.hamcrest.version}</version>
46-
<scope>test</scope>
47-
</dependency>
48-
49-
<dependency>
50-
<groupId>org.mockito</groupId>
51-
<artifactId>mockito-core</artifactId>
52-
<version>${mockito.version}</version>
53-
<scope>test</scope>
54-
</dependency>
55-
56-
</dependencies>
57-
58-
<build>
59-
<finalName>mockito</finalName>
60-
<resources>
61-
<resource>
62-
<directory>src/main/resources</directory>
63-
<filtering>true</filtering>
64-
</resource>
65-
</resources>
66-
67-
<plugins>
68-
69-
<plugin>
70-
<groupId>org.apache.maven.plugins</groupId>
71-
<artifactId>maven-compiler-plugin</artifactId>
72-
<version>${maven-compiler-plugin.version}</version>
73-
<configuration>
74-
<source>1.8</source>
75-
<target>1.8</target>
76-
</configuration>
77-
</plugin>
78-
79-
<plugin>
80-
<groupId>org.apache.maven.plugins</groupId>
81-
<artifactId>maven-surefire-plugin</artifactId>
82-
<version>${maven-surefire-plugin.version}</version>
83-
</plugin>
84-
85-
</plugins>
86-
87-
</build>
88-
89-
<properties>
90-
<!-- persistence -->
91-
<hibernate.version>4.3.11.Final</hibernate.version>
92-
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
93-
94-
<!-- logging -->
95-
<org.slf4j.version>1.7.13</org.slf4j.version>
96-
<logback.version>1.1.3</logback.version>
97-
98-
<!-- various -->
99-
<hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
100-
101-
<!-- util -->
102-
<guava.version>19.0</guava.version>
103-
<commons-lang3.version>3.4</commons-lang3.version>
104-
105-
<!-- testing -->
106-
<org.hamcrest.version>1.3</org.hamcrest.version>
107-
<junit.version>4.12</junit.version>
108-
<mockito.version>1.10.19</mockito.version>
109-
110-
<httpcore.version>4.4.1</httpcore.version>
111-
<httpclient.version>4.5</httpclient.version>
112-
113-
<rest-assured.version>2.9.0</rest-assured.version>
114-
115-
<!-- maven plugins -->
116-
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
117-
<maven-war-plugin.version>2.6</maven-war-plugin.version>
118-
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
119-
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
120-
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
121-
122-
</properties>
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+
<groupId>org.baeldung</groupId>
5+
<artifactId>mockito</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
8+
<name>mockito</name>
9+
10+
<dependencies>
11+
12+
<!-- utils -->
13+
14+
<dependency>
15+
<groupId>com.google.guava</groupId>
16+
<artifactId>guava</artifactId>
17+
<version>${guava.version}</version>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-lang3</artifactId>
23+
<version>${commons-lang3.version}</version>
24+
</dependency>
25+
26+
<!-- web -->
27+
28+
<!-- test scoped -->
29+
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<version>${junit.version}</version>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.hamcrest</groupId>
39+
<artifactId>hamcrest-core</artifactId>
40+
<version>${org.hamcrest.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.hamcrest</groupId>
45+
<artifactId>hamcrest-library</artifactId>
46+
<version>${org.hamcrest.version}</version>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.mockito</groupId>
52+
<artifactId>mockito-core</artifactId>
53+
<version>${mockito.version}</version>
54+
<scope>test</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.powermock</groupId>
59+
<artifactId>powermock-module-junit4</artifactId>
60+
<version>${powermock.version}</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.powermock</groupId>
65+
<artifactId>powermock-api-mockito</artifactId>
66+
<version>${powermock.version}</version>
67+
<scope>test</scope>
68+
</dependency>
69+
70+
</dependencies>
71+
72+
<build>
73+
<finalName>mockito</finalName>
74+
<resources>
75+
<resource>
76+
<directory>src/main/resources</directory>
77+
<filtering>true</filtering>
78+
</resource>
79+
</resources>
80+
81+
<plugins>
82+
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<artifactId>maven-compiler-plugin</artifactId>
86+
<version>${maven-compiler-plugin.version}</version>
87+
<configuration>
88+
<source>1.8</source>
89+
<target>1.8</target>
90+
</configuration>
91+
</plugin>
92+
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-surefire-plugin</artifactId>
96+
<version>${maven-surefire-plugin.version}</version>
97+
</plugin>
98+
99+
</plugins>
100+
101+
</build>
102+
103+
<properties>
104+
<!-- Spring -->
105+
<org.springframework.version>4.1.5.RELEASE</org.springframework.version>
106+
<org.springframework.security.version>3.2.5.RELEASE</org.springframework.security.version>
107+
108+
<!-- persistence -->
109+
<hibernate.version>4.3.10.Final</hibernate.version>
110+
<mysql-connector-java.version>5.1.35</mysql-connector-java.version>
111+
112+
<!-- logging -->
113+
<org.slf4j.version>1.7.13</org.slf4j.version>
114+
<logback.version>1.1.3</logback.version>
115+
116+
<!-- various -->
117+
<hibernate-validator.version>5.1.3.Final</hibernate-validator.version>
118+
119+
<!-- util -->
120+
<guava.version>19.0</guava.version>
121+
<commons-lang3.version>3.4</commons-lang3.version>
122+
123+
<!-- testing -->
124+
<org.hamcrest.version>1.3</org.hamcrest.version>
125+
<junit.version>4.12</junit.version>
126+
<mockito.version>1.10.19</mockito.version>
127+
<powermock.version>1.6.4</powermock.version>
128+
129+
<httpcore.version>4.4.1</httpcore.version>
130+
<httpclient.version>4.5</httpclient.version>
131+
132+
<rest-assured.version>2.4.1</rest-assured.version>
133+
134+
<!-- maven plugins -->
135+
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
136+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
137+
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
138+
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
139+
<cargo-maven2-plugin.version>1.4.14</cargo-maven2-plugin.version>
140+
141+
</properties>
123142

124143
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.powermockito.introduction;
2+
3+
public class CollaboratorForPartialMocking {
4+
public static String staticMethod() {
5+
return "Hello Baeldung!";
6+
}
7+
8+
public final String finalMethod() {
9+
return "Hello Baeldung!";
10+
}
11+
12+
private String privateMethod() {
13+
return "Hello Baeldung!";
14+
}
15+
16+
public String privateMethodCaller() {
17+
return privateMethod() + " Welcome to the Java world.";
18+
}
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.powermockito.introduction;
2+
3+
public class CollaboratorWithFinalMethods {
4+
public final String helloMethod() {
5+
return "Hello World!";
6+
}
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.powermockito.introduction;
2+
3+
public class CollaboratorWithStaticMethods {
4+
public static String firstMethod(String name) {
5+
return "Hello " + name + " !";
6+
}
7+
8+
public static String secondMethod() {
9+
return "Hello no one!";
10+
}
11+
12+
public static String thirdMethod() {
13+
return "Hello no one again!";
14+
}
15+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.powermockito.introduction;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.powermock.api.mockito.PowerMockito.*;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.Mockito;
9+
import org.powermock.core.classloader.annotations.PrepareForTest;
10+
import org.powermock.modules.junit4.PowerMockRunner;
11+
12+
@RunWith(PowerMockRunner.class)
13+
@PrepareForTest(fullyQualifiedNames = "com.baeldung.powermockito.introduction.*")
14+
public class PowerMockitoTest {
15+
@Test
16+
public void givenFinalMethods_whenUsingPowerMockito_thenCorrect() throws Exception {
17+
CollaboratorWithFinalMethods mock = mock(CollaboratorWithFinalMethods.class);
18+
whenNew(CollaboratorWithFinalMethods.class).withNoArguments().thenReturn(mock);
19+
20+
CollaboratorWithFinalMethods collaborator = new CollaboratorWithFinalMethods();
21+
verifyNew(CollaboratorWithFinalMethods.class).withNoArguments();
22+
23+
when(collaborator.helloMethod()).thenReturn("Hello Baeldung!");
24+
String welcome = collaborator.helloMethod();
25+
Mockito.verify(collaborator).helloMethod();
26+
assertEquals("Hello Baeldung!", welcome);
27+
}
28+
29+
@Test(expected = RuntimeException.class)
30+
public void givenStaticMethods_whenUsingPowerMockito_thenCorrect() {
31+
mockStatic(CollaboratorWithStaticMethods.class);
32+
33+
when(CollaboratorWithStaticMethods.firstMethod(Mockito.anyString())).thenReturn("Hello Baeldung!");
34+
when(CollaboratorWithStaticMethods.secondMethod()).thenReturn("Nothing special");
35+
doThrow(new RuntimeException()).when(CollaboratorWithStaticMethods.class);
36+
CollaboratorWithStaticMethods.thirdMethod();
37+
38+
String firstWelcome = CollaboratorWithStaticMethods.firstMethod("Whoever");
39+
String secondWelcome = CollaboratorWithStaticMethods.firstMethod("Whatever");
40+
41+
assertEquals("Hello Baeldung!", firstWelcome);
42+
assertEquals("Hello Baeldung!", secondWelcome);
43+
44+
verifyStatic(Mockito.times(2));
45+
CollaboratorWithStaticMethods.firstMethod(Mockito.anyString());
46+
47+
verifyStatic(Mockito.never());
48+
CollaboratorWithStaticMethods.secondMethod();
49+
50+
CollaboratorWithStaticMethods.thirdMethod();
51+
}
52+
53+
@Test
54+
public void givenPartialMocking_whenUsingPowerMockito_thenCorrect() throws Exception {
55+
String returnValue;
56+
57+
spy(CollaboratorForPartialMocking.class);
58+
when(CollaboratorForPartialMocking.staticMethod()).thenReturn("I am a static mock method.");
59+
returnValue = CollaboratorForPartialMocking.staticMethod();
60+
verifyStatic();
61+
CollaboratorForPartialMocking.staticMethod();
62+
assertEquals("I am a static mock method.", returnValue);
63+
64+
CollaboratorForPartialMocking collaborator = new CollaboratorForPartialMocking();
65+
CollaboratorForPartialMocking mock = spy(collaborator);
66+
67+
when(mock.finalMethod()).thenReturn("I am a final mock method.");
68+
returnValue = mock.finalMethod();
69+
Mockito.verify(mock).finalMethod();
70+
assertEquals("I am a final mock method.", returnValue);
71+
72+
when(mock, "privateMethod").thenReturn("I am a private mock method.");
73+
returnValue = mock.privateMethodCaller();
74+
verifyPrivate(mock).invoke("privateMethod");
75+
assertEquals("I am a private mock method. Welcome to the Java world.", returnValue);
76+
}
77+
}

0 commit comments

Comments
 (0)