문제

I have class that reads properties from assets directory. Construtor of this class have Context. I want to write test for this class but I really don't don't how to get context in Android Tests.

public class PropertyLoader {

    private Context context;
    private Properties properties;

    public PropertyLoader(Context context) {
        this.context = context;
        properties = new Properties();
    }

    public Properties getProperties(String FileName) {
        ...
    }
}

Test class

public class PropertyLoaderTest extends InstrumentationTestCase {
    @Test
    public void testSimple() {
        Context context = getInstrumentation().getContext();
        PropertyLoader propertyLoader = new PropertyLoader(context);
    }
}

I get following exception

junit.framework.AssertionFailedError: Exception in constructor: testSimple (java.lang.RuntimeException: Stub!
도움이 되었습니까?

해결책

Run your tests on an Android emulator or a device.

The android.jar you're using on your desktop doesn't have anything implemented. All methods there throw the RuntimeException: Stub!.

Alternatively, remove all Android dependencies from the code so that you can run your tests without importing android.jar.

다른 팁

TestCase with context ( Test will run on device or emulator ) -http://developer.android.com/reference/android/test/AndroidTestCase.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top