3

试图创建一个在我的单元测试中只调用一次的构造函数

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() );
    }

但是当我将文件作为 Android Junit 测试运行时,我在构造函数错误中遇到异常。这里也是堆栈跟踪

 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)
4

4 回答 4

2

将在测试运行期间只需要执行一次的代码放入静态初始化程序中。它应该只运行一次,当类被加载时(不要重用这个类)。

于 2013-02-06T19:06:59.417 回答
0

您还可以编写自己的 setUp() 方法来初始化您的“音乐”对象一次 有关详细信息,请参阅此页面 http://www.methodsandtools.com/tools/tools.php?junit

于 2013-02-06T19:17:49.430 回答
0

什么在ArtistTest.java:17

如果getInstrumentation只是返回,private field instr那么这里的调用返回null,导致后面的NPE

于 2013-02-06T19:26:12.910 回答
0

不要为您的测试用例使用具体的构造函数。

最好遵循junit范式。

使用这些注释来初始化您需要为每个类初始化一次的任何值。即@BeforeClass 和@AfterClass 确保你定义了你想要在类级别初始化的任何东西

@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
}

对于每个方法所需的任何初始化,请使用以下注释告诉 junit 为每个测试方法初始化这些值

@Before
public void setUp() throws Exception {
 //per method attributes initialized here

}

@After
public void tearDown() throws Exception {
 //per method attributes destroyed here
}

使用注解@Test 指定一个方法为测试方法

IE

@Test
public void testMethodx() {
}
于 2013-02-06T20:10:02.520 回答