File tree Expand file tree Collapse file tree 4 files changed +69
-0
lines changed
main/java/com/baeldung/mockfinal
test/java/com/baeldung/mockfinal Expand file tree Collapse file tree 4 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 125
125
<argLine >
126
126
--add-opens java.base/java.lang=ALL-UNNAMED
127
127
--add-opens java.base/java.time=ALL-UNNAMED
128
+ --add-opens java.base/java.time.format=ALL-UNNAMED
129
+ --add-opens java.base/java.util=ALL-UNNAMED
128
130
</argLine >
129
131
</configuration >
130
132
</plugin >
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