BAEL-1182 - mocking final classes and methods with Mockito (#3083)

This commit is contained in:
Eric Goebelbecker 2017-11-19 14:47:03 -05:00 committed by maibin
parent 2a9570cc1f
commit e2202f0285
4 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package org.baeldung.mockito;
public class FinalList extends MyList {
@Override
public int size() {
return 1;
}
}

View File

@ -0,0 +1,35 @@
package org.baeldung.mockito;
import org.junit.Test;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockFinals {
@Test
public void whenMockFinalClassMockWorks() {
FinalList finalList = new FinalList();
FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2);
assertNotEquals(mock.size(), finalList.size());
}
@Test
public void whenMockFinalMethodMockWorks() {
MyList myList = new MyList();
MyList mock = mock(MyList.class);
when(mock.finalMethod()).thenReturn(1);
assertNotEquals(mock.finalMethod(), myList.finalMethod());
}
}

View File

@ -19,4 +19,7 @@ class MyList extends AbstractList<String> {
// no-op
}
final public int finalMethod() {
return 0;
}
}