Skip to content

Commit 7a526ad

Browse files
Merge pull request #18423 from hmdrzsharifi/BAEL-8669
BAEL-8669: Improve article "Mock Final Classes and Methods with Mockito"
2 parents a7e9c65 + 7c80dec commit 7a526ad

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

testing-modules/junit-5/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,17 @@
7474
<groupId>junit</groupId>
7575
<artifactId>junit</artifactId>
7676
</exclusion>
77+
<exclusion>
78+
<groupId>org.javassist</groupId>
79+
<artifactId>javassist</artifactId>
80+
</exclusion>
7781
</exclusions>
7882
</dependency>
83+
<dependency>
84+
<groupId>org.javassist</groupId>
85+
<artifactId>javassist</artifactId>
86+
<version>${javassist.version}</version>
87+
</dependency>
7988
<dependency>
8089
<groupId>org.mockito</groupId>
8190
<artifactId>mockito-junit-jupiter</artifactId>
@@ -125,6 +134,8 @@
125134
<argLine>
126135
--add-opens java.base/java.lang=ALL-UNNAMED
127136
--add-opens java.base/java.time=ALL-UNNAMED
137+
--add-opens java.base/java.time.format=ALL-UNNAMED
138+
--add-opens java.base/java.util=ALL-UNNAMED
128139
</argLine>
129140
</configuration>
130141
</plugin>
@@ -136,6 +147,7 @@
136147
<powermock.version>2.0.9</powermock.version>
137148
<spring.version>5.0.1.RELEASE</spring.version>
138149
<mockito.version>3.3.0</mockito.version> <!--Cannot upgrade to the latest version as powermock doesn't support that-->
150+
<javassist.version>3.30.2-GA</javassist.version> <!--Cannot upgrade to the latest version as powermock doesn't support that-->
139151
</properties>
140152

141153
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.mockfinal;
2+
3+
public final class FinalList extends MyList {
4+
5+
@Override
6+
public int size() {
7+
return 1;
8+
}
9+
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.mockfinal;
2+
3+
import java.util.AbstractList;
4+
5+
public class MyList extends AbstractList<String> {
6+
7+
@Override
8+
public String get(final int index) {
9+
return null;
10+
}
11+
12+
@Override
13+
public int size() {
14+
return 1;
15+
}
16+
17+
@Override
18+
public void add(int index, String element) {
19+
// no-op
20+
}
21+
22+
final public int finalMethod() {
23+
return 0;
24+
}
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.mockfinal;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.powermock.api.mockito.PowerMockito;
6+
import org.powermock.core.classloader.annotations.PrepareForTest;
7+
import org.powermock.modules.junit4.PowerMockRunner;
8+
9+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
10+
import static org.powermock.api.mockito.PowerMockito.when;
11+
12+
@RunWith(PowerMockRunner.class)
13+
@PrepareForTest({MyList.class, FinalList.class})
14+
public class PowerMockFinalsUnitTest {
15+
16+
@Test
17+
public void whenMockFinalMethod_thenMockWorks() throws Exception {
18+
MyList mockClass = PowerMockito.mock(MyList.class);
19+
when(mockClass.finalMethod()).thenReturn(1);
20+
21+
assertThat(mockClass.finalMethod()).isNotZero();
22+
}
23+
24+
@Test
25+
public void whenMockFinalClass_thenMockWorks() throws Exception {
26+
FinalList mockClass = PowerMockito.mock(FinalList.class);
27+
when(mockClass.size()).thenReturn(2);
28+
29+
assertThat(mockClass.size()).isNotEqualTo(1);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)