Mockito 和 Dexmaker 的 UnsupportedOperationException
UnsupportedOperationException with Mockito and Dexmaker
最近我一直在为 Mockito 苦苦挣扎。但是经过不懈的努力,我让它编译没有错误,除了这个在特定情况下的错误:
当我使用 Mockito 模拟包私有 class 时,在同一个包中进行测试时,出现以下错误:
java.lang.UnsupportedOperationException: cannot proxy inaccessible class class [...].AndroidCalendarGenerator.ManageEventsUI.CalendarMonitorServiceConnection
at com.android.dx.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:269)
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)
[...]
这是我的 class :
package [...].AndroidCalendarGenerator.ManageEventsUI;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.mock;
@RunWith(MockitoJUnitRunner.class)
public class CalendarMonitorServiceConnectionTest {
@Test
public void testOne() {
CalendarMonitorServiceConnection c1 = new CalendarMonitorServiceConnection(null);
CalendarMonitorServiceConnection c = mock(CalendarMonitorServiceConnection.class);
}
}
我认为测试编译的第一行没有错误意味着这个测试与我尝试模拟的 CalendarMonitorServiceConnection
class 在同一个文件夹中。
最后,我的 build.gradle 中有了这些导入:
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.4"
我错过了什么?
非常感谢您的回答
编辑
这是我尝试模拟的 class 的代码:
package [...].AndroidCalendarGenerator.ManageEventsUI;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import [...].AndroidCalendarGenerator.CalendarEventMonitor;
import [...].AndroidCalendarGenerator.Event;
import [...].AndroidCalendarGenerator.EventChangeListener;
class CalendarMonitorServiceConnection implements ServiceConnection {
private CalendarEventMonitor mMonitor;
private EventListViewAdapter mEventListViewAdapter;
CalendarMonitorServiceConnection(EventListViewAdapter eventListViewAdapter) {
mEventListViewAdapter = eventListViewAdapter;
}
/**
* Custom event change listener that defines what to do in case of changes in the calendar
*/
private class CustomEventChangeListener implements EventChangeListener {
@Override
public void onEventActivated(Event event) {
//[...]
}
@Override
public void onEventRemoved(Event event) {
//[...]
}
@Override
public void onEventListChanged() {
//[...]
}
}
/**
* Pulls data for next 30 days and send it to the adapter
*/
private void sendNextThirtyDaysEventsToAdapter() {
//[...]
}
@Override
public void onServiceConnected(ComponentName className,
IBinder service){
//[...]
}
@Override
public void onServiceDisconnected(ComponentName arg0){
//[...]
}
}
经过一些研究,在我看来:
- Mockito + Dexmaker 无法模拟包私有 class,即使单元测试在同一个包中。
- Powermock 可以模拟包私有 class,但前提是单元测试在同一个包中。
因此,解决方案是使用 Powermock,因为我想保持我的 class 包私有。
主要来源:
- Mocking package private classes
- Provide mock for package-private class?
- 我自己的测试
我会在试用 Powemock 时更新 post。
或者,如果有人可以同意或更正我的回答。
最近我一直在为 Mockito 苦苦挣扎。但是经过不懈的努力,我让它编译没有错误,除了这个在特定情况下的错误:
当我使用 Mockito 模拟包私有 class 时,在同一个包中进行测试时,出现以下错误:
java.lang.UnsupportedOperationException: cannot proxy inaccessible class class [...].AndroidCalendarGenerator.ManageEventsUI.CalendarMonitorServiceConnection
at com.android.dx.stock.ProxyBuilder.buildProxyClass(ProxyBuilder.java:269)
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)
[...]
这是我的 class :
package [...].AndroidCalendarGenerator.ManageEventsUI;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.mock;
@RunWith(MockitoJUnitRunner.class)
public class CalendarMonitorServiceConnectionTest {
@Test
public void testOne() {
CalendarMonitorServiceConnection c1 = new CalendarMonitorServiceConnection(null);
CalendarMonitorServiceConnection c = mock(CalendarMonitorServiceConnection.class);
}
}
我认为测试编译的第一行没有错误意味着这个测试与我尝试模拟的 CalendarMonitorServiceConnection
class 在同一个文件夹中。
最后,我的 build.gradle 中有了这些导入:
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile "com.crittercism.dexmaker:dexmaker:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-mockito:1.4"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:1.4"
我错过了什么?
非常感谢您的回答
编辑
这是我尝试模拟的 class 的代码:
package [...].AndroidCalendarGenerator.ManageEventsUI;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.os.IBinder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import [...].AndroidCalendarGenerator.CalendarEventMonitor;
import [...].AndroidCalendarGenerator.Event;
import [...].AndroidCalendarGenerator.EventChangeListener;
class CalendarMonitorServiceConnection implements ServiceConnection {
private CalendarEventMonitor mMonitor;
private EventListViewAdapter mEventListViewAdapter;
CalendarMonitorServiceConnection(EventListViewAdapter eventListViewAdapter) {
mEventListViewAdapter = eventListViewAdapter;
}
/**
* Custom event change listener that defines what to do in case of changes in the calendar
*/
private class CustomEventChangeListener implements EventChangeListener {
@Override
public void onEventActivated(Event event) {
//[...]
}
@Override
public void onEventRemoved(Event event) {
//[...]
}
@Override
public void onEventListChanged() {
//[...]
}
}
/**
* Pulls data for next 30 days and send it to the adapter
*/
private void sendNextThirtyDaysEventsToAdapter() {
//[...]
}
@Override
public void onServiceConnected(ComponentName className,
IBinder service){
//[...]
}
@Override
public void onServiceDisconnected(ComponentName arg0){
//[...]
}
}
经过一些研究,在我看来:
- Mockito + Dexmaker 无法模拟包私有 class,即使单元测试在同一个包中。
- Powermock 可以模拟包私有 class,但前提是单元测试在同一个包中。
因此,解决方案是使用 Powermock,因为我想保持我的 class 包私有。
主要来源:
- Mocking package private classes
- Provide mock for package-private class?
- 我自己的测试
我会在试用 Powemock 时更新 post。 或者,如果有人可以同意或更正我的回答。