Reputation: 3167
I am trying to run my soundkeeper
class which is not an activity, but it requires a context and I am not sure as to how to provide one for it. I haven't done any unit tests before so I am quite new to doing unit tests, this is the code I have for the test class
public class SoundKeeperTest {
SoundKeeper sounds;
Context mContext;
@Before
public void setUp() throws Exception {
sounds = new SoundKeeper();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testAddSound() {
sounds.initSounds(mContext);
}
@Test
public void testPlaySound() {
fail("Not yet implemented");
}
}
When I try to get the context method from InstrumentationTestCase
, I receive the following error
java.lang.NoClassDefFoundError: android/test/InstrumentationTestCase
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: android.test.InstrumentationTestCase
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 18 more
Upvotes: 0
Views: 1689
Reputation: 13817
Android has a number of test case classes that you can use to test various behaviours.
For standard unit testing you extend the AndroidTestCase class which provides you with the ability to use the Junit 3 API to test classes with no Android runtime dependencies.
If however you wish to test classes that have dependencies on the Android runtime i.e. Activity subclasses then you need to look at some of the special TestCase classes.
InstrumentationTestCase is one of the most basic TestCase implementations and provides access to a test context. It is further subclasses by more specialist class that provide convenience methods for testing activity testing, i.e. ActivityInstrumentationTestCase2.
For your purposes you should get access to everything you need through subclassing InstrumentationTestCase and using the included Instrumentation object to get a reference to a Context:
getInstrumentation().getContext();
The Android Testing Documentation guide is a good reference point for understanding the available TestCase classes are their application.
Upvotes: 2