question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot mock concrete classes with Mockito

See original GitHub issue

I’m getting

java.lang.NoClassDefFoundError: com/android/dx/stock/ProxyBuilder

or

java.lang.ExceptionInInitializerError

when I try to mock a concrete class on dexmaker 1.4 or 1.3. Reverting to 1.2 from google code works fine.

java.lang.NoClassDefFoundError: com/android/dx/stock/ProxyBuilder
at com.android.dx.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:56)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1285)
at org.mockito.Mockito.mock(Mockito.java:1163)
at lt.segfoltas.asthmacontroltest.DbAsyncTests.setUp(DbAsyncTests.java:28)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)


java.lang.ExceptionInInitializerError
at com.android.dx.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:56)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1285)
at org.mockito.Mockito.mock(Mockito.java:1163)
at lt.segfoltas.asthmacontroltest.DbAsyncTests.setUp(DbAsyncTests.java:28)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Caused by: java.lang.ExceptionInInitializerError
at com.android.dx.stock.ProxyBuilder.<clinit>(ProxyBuilder.java:758)
... 15 more
Caused by: java.lang.NoClassDefFoundError: com.android.dx.rop.type.Type
at com.android.dx.TypeId.<clinit>(TypeId.java:36)
... 16 more

Mocking interfaces works as before. I’d just use 1.2, but I hit #12 on lollipop.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vaughandroidcommented, Sep 10, 2015

It turns out that all I was missing is a dependency on the new(?) dexmaker-dx module. This is a new requirement in 1.3/1.4, but hasn’t been mentioned anywhere that I’ve seen.

0reactions
drewhannaycommented, Dec 1, 2016

Closing this one as a duplicate of #22

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking Chained calls in Concrete Class Mockito
Mockito is not able to mock out final classes. There are a few ways to get around this. 1) In MyAbstractClass consider not...
Read more >
Mocking concrete class - Not recommended
I've just read an excerpt of "Growing Object-Oriented Software" book which explains some reasons why mocking concrete class is not recommended.
Read more >
Mock Final Classes and Methods with Mockito - Baeldung
In this short tutorial, we'll focus on how to mock final classes and methods using Mockito. As with other articles focused on the...
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Mockito mocks not only interfaces but also abstract classes and concrete non-final classes. Out of the box, Mockito cannot mock final classes and...
Read more >
Installation - EasyMock
Finally, since EasyMock 4.1, JUnit 5 extensions are supported. ... the type of the concrete class can't be checked statically against the mock...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found