2

最近、私はMockitoに苦労しています。しかし、勇敢な努力の後、特定のケースでこれを除いてエラーなしでコンパイルしました:

同じパッケージ内のテストで Mockito を使用してパッケージ プライベート クラスをモックすると、次のエラーが発生します。

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)
    [...]

これが私のクラスです:

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エラーなしでコンパイルされたテストの最初の行は、このテストがモックしようとしているクラスと同じフォルダーにあることを意味すると思います。

最後に、これらのインポートを 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"

何が欠けていますか?

ご回答ありがとうございます


編集

モックしようとするクラスのコードは次のとおりです。

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){
        //[...]
    }
}
4

1 に答える 1