Create a constructor in a test case so that it will only be called once
Trying to create a constructor that is called only once in my unit tests
public class ArtistTest extends InstrumentationTestCase{
private static final String TAG_NAME = "TESTING_SUITE";
private TestingMusicDAO musicDAO;
private List<Song> songs;
private Instrumentation instr;
MusicService musicService;
public ArtistTest() throws Exception {
super();
instr = this.getInstrumentation();
Log.d(TAG_NAME, "Setting up testing songs");
musicDAO = new TestingMusicDAO(instr.getContext());
musicService = new MusicServiceImpl(musicDAO);
musicDAO.getAllSongsFromFile();
songs = musicDAO.getAllSongs();
for(Song song : songs)
Log.d( TAG_NAME, song.toString() );
}
But I am getting a constructor error when I run the file as Android Junit Test. There is also a stack trace
junit.framework.AssertionFailedError: Exception in constructor: test0 (java.lang.NullPointerException
at com.intellimec.ilane.ice.mediaservices.ArtistTest.<init>(ArtistTest.java:17)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:118)
at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:148)
at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:56)
at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:80)
at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:444)
at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:425)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:370)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4382)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
source to share
you can also write your own setUp () method to initialize Music objects once See this page for more details http://www.methodsandtools.com/tools/tools.php?junit
source to share
Don't use a specific constructor for test cases.
Better to follow the junit paradigm.
use these annotations to initialize any values you need to initialize once per class. i.e. @BeforeClass and @AfterClass Make sure you define whatever you want for class-level initialization
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//per class attributes initialized here
instr = this.getInstrumentation();
Log.d(TAG_NAME, "Setting up testing songs");
musicDAO = new TestingMusicDAO(instr.getContext());
musicService = new MusicServiceImpl(musicDAO);
musicDAO.getAllSongsFromFile();
songs = musicDAO.getAllSongs();
for(Song song : songs)
Log.d( TAG_NAME, song.toString() );
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
//per class attributes destroyed here
}
for any initialization needed for each method, use the following annotations to tell junit to initialize these values for each test method.
@Before
public void setUp() throws Exception {
//per method attributes initialized here
}
@After
public void tearDown() throws Exception {
//per method attributes destroyed here
}
use @Test annotation to mark a method as a test method
i.e.
@Test
public void testMethodx() {
}
source to share