qdrzwd / dexmaker

Automatically exported from code.google.com/p/dexmaker
0 stars 0 forks source link

mockito+dexmaker can't mock package-protected classes #34

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Problem:
Mockito should be able to mock package-protected classes.

---
import static org.mockito.Mockito.*;

class A {}

public class Test {
  public static void main(String[] argv) {
    A a = mock(A.class);
  }
}

---

What is the expected output? What do you see instead?

This code above works well on JVM, but not on Dalvik with mockito+dexmaker, 
causes:

java.lang.UnsupportedOperationException: cannot proxy inaccessible class class A
at com.google.dexmaker.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:257)
at 
com.google.dexmaker.mockito.DexmakerMockMaker.createMock(DexmakerMockMaker.java:
56)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:26)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:51)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.Mockito.mock(Mockito.java:1120)

Mocking package-private classes is handy when you test them without exposing 
publicly.
Otherwise you'll have to declare public every class you want to mock.

What version of the product are you using? On what operating system?
Mockito1.9.5 + Dexmaker fresh cut (6ada76ae1356), on Android 4.4.2

Original issue reported on code.google.com by oceanb...@gmail.com on 23 Feb 2014 at 4:44

GoogleCodeExporter commented 8 years ago
Any solution for this problem so far? I am using Powermock to unit test a 
protected class outside its' package. But I don't find ways to get this 
protected class mocked or initialized in my unit test class.

Original comment by wangyu0...@gmail.com on 29 Jul 2014 at 3:04

GoogleCodeExporter commented 8 years ago
Anybody figure out a workaround for this?  Just ran into it myself.

Original comment by andy.den...@affectiva.com on 4 Sep 2014 at 6:08

GoogleCodeExporter commented 8 years ago
Also looking for a solution...

Original comment by fredd...@gmail.com on 20 Nov 2014 at 10:10