I'm writing unit tests for a storing class and have to pass a context
to that class. I have some trouble to get the context
within the junit test.
I tried the solution which is described at Get context of test project in Android junit test case, respectively at http://ift.tt/1MbNgPU.
import android.test.AndroidTestCase;
import android.content.Context;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.*;
public class MyDataStoreTest extends AndroidTestCase {
private DataStore myDS;
private Context mContext;
@Override
protected void setUp() throws Exception {
super.setUp();
try {
Method getTestContext = AndroidTestCase.class.getMethod("getTestContext");
mContext = (Context) getTestContext.invoke(this);
} catch (final Exception exception) {
exception.printStackTrace();
throw exception;
}
}
@Test
public void testOne() {
System.out.println("Own context: " + String.valueOf(mContext));
myDS.store("Hello");
System.out.println("Return: " + myDS.read());
}
}
It gives me this error message:
java.lang.NoSuchMethodException: android.test.AndroidTestCase.getTestContext()
at java.lang.Class.getMethod(Class.java:1670)
at com.ibm.security.access.mobile.MyDataStoreTest.setUp(MyDataStoreTest.java:25)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
...
It looks like there is no method getTestContext
in AndroidtestCase
, which can might be expected, since it's marked as @hide:
/**
* @hide
*/
public Context getTestContext() {
return mTestContext;
}
How can this be solved?
Aucun commentaire:
Enregistrer un commentaire