mockito


PowerMock failing to initialize final class


I am using Robolectric 3.3.2 and PowerMock to unit test a final class.
#RunWith(RobolectricTestRunner.class)
#Config(sdk = 23)
#PrepareForTest({ FinalClass.class})
public class FragmentTest {
private TestFragment mFragment;
private TestActivity mActivity;
private FinalClass mFinalClassMock;
#Before
public void setUp() throws Exception{
mFinalClassMock = PowerMockito.mock(FinalClass.class);
PowerMockito.whenNew(FinalClass.class).withAnyArguments()
.thenReturn(mFinalClassMock);
mActivity = Robolectric.buildActivity(TestActivity.class)
.create().start().resume().get();
mFragment = new TestFragment();
}
}
is failing with below exception.
java.lang.IllegalArgumentException: Cannot subclass final class class com.android.contacts.common.preference.ContactsPreferences
at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378)
at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318)
at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:123)
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:59)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143)
I am using JUNIT 4.12 and Powermock 1.6.4.
Changed versions of junit and powermock as suggested in another post, but no success.
Anyone faced this issue recently ?
Adding a rule inside the class seems to fix the issue
#Rule
public PowerMockRule rule = new PowerMockRule();

Related Links

Using mockito is there a reason to nullify mocks in #After?
Can we use ArgumentCaptor to check log message in Mockito
Using Mockito for writing ATG test case
mocking/spying private member of super class
How to set members of mocked object
How to Mock repository calls ATG
PowerMockito can't seem to match and overloaded method
how to use mockito for method chains
How to match the second invocation on mock?
checking constructor arguments in mockito' verify method
Mocking two objects of the same type with Mockito
Mockito and this calls
Powermock no last call on a mock available
How should I use org.mockito.AdditionalMatchers.gt?
Argument captor mockito
PowerMocking static does not return expected object

Categories

HOME
urbancode
vmware
sqlite-net-extensions
graphql
electronics
cross-browser
blueprintjs
directx
primary-key
ssl-client-authentication
node-pdfkit
volttron
u-sql
fallback
flask-wtforms
windows-server-2012
ghc
libtiff
google-static-maps
code-review
swiftlint
zapier
cx-freeze
intentfilter
web-sql
tapestry
excel-2007
extjs5
lcd
dxf
firebase-crash-reporting
typo3-6.2.x
neo4j-spatial
scorm2004
wallpaper
overlap
devextreme
neuroscience
occlusion
twitch
angularjs-factory
tasker
optix
ensembles
janrain
jvm-languages
ibpy
nxlog
heightmap
outlook-api
skeleton-css-boilerplate
scrollable
parentheses
azure-application-gateway
qsslsocket
pnotify
domain-model
jxcore
csound
diagnostics
gcsfuse
spring-mongodb
redundancy
or-tools
pintos
plottable.js
lttng
pagedlist
make-install
captivenetwork
php-internals
energy
rtbkit
endeca-workbench
jmeter-maven-plugin
django-unittest
pundit
responsive-images
sankey-diagram
unity3d-gui
inmobi
document-classification
mesa
poller
imdbpy
eventlistener
jspinclude
getmessage
online-compilation
pendrive
window-management
locate
site.master
sudzc
sortable-tables
gamma
substrings
kdbg
photoshop-cs4
ctp4
defensive-programming
ntvdm.exe

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile