File tree Expand file tree Collapse file tree 4 files changed +79
-0
lines changed
main/java/com/baeldung/mockfinal
test/java/com/baeldung/mockfinal Expand file tree Collapse file tree 4 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 74
74
<groupId >junit</groupId >
75
75
<artifactId >junit</artifactId >
76
76
</exclusion >
77
+ <exclusion >
78
+ <groupId >org.javassist</groupId >
79
+ <artifactId >javassist</artifactId >
80
+ </exclusion >
77
81
</exclusions >
78
82
</dependency >
83
+ <dependency >
84
+ <groupId >org.javassist</groupId >
85
+ <artifactId >javassist</artifactId >
86
+ <version >${javassist.version} </version >
87
+ </dependency >
79
88
<dependency >
80
89
<groupId >org.mockito</groupId >
81
90
<artifactId >mockito-junit-jupiter</artifactId >
125
134
<argLine >
126
135
--add-opens java.base/java.lang=ALL-UNNAMED
127
136
--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
128
139
</argLine >
129
140
</configuration >
130
141
</plugin >
136
147
<powermock .version>2.0.9</powermock .version>
137
148
<spring .version>5.0.1.RELEASE</spring .version>
138
149
<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-->
139
151
</properties >
140
152
141
153
</project >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments