有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在测试用例中创建构造函数,以便只调用一次

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

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 安卓.test.AndroidTestRunner.getTest(AndroidTestRunner.java:148)
 at 安卓.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:56)
 at   安卓.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:80)
 at 安卓.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:444)
 at 安卓.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:425)
 at 安卓.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:370) 
 at 安卓.app.ActivityThread.handleBindApplication(ActivityThread.java:4382)
 at 安卓.app.ActivityThread.access$1300(ActivityThread.java:141)
 at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
 at 安卓.os.Handler.dispatchMessage(Handler.java:99)
 at 安卓.os.Looper.loop(Looper.java:137)
 at 安卓.app.ActivityThread.main(ActivityThread.java:5039)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 at dalvik.system.NativeStart.main(Native Method)
 )
 at 安卓.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
 at 安卓.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
 at 安卓.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
 at 安卓.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

共 (4) 个答案

  1. # 1 楼答案

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

  2. # 3 楼答案

    不要在测试用例中使用具体的构造函数

    最好遵循junit范式

    使用这些注释初始化每个类需要初始化一次的任何值。 i、 e.@课前和课后 确保在类级别定义要初始化的内容

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

    使用annotation@Test将方法指定为测试方法

    @Test
    public void testMethodx() {
    }
    
  3. # 4 楼答案

    ArtistTest.java:17是什么

    如果getInstrumentation只是返回private field instr,那么它在这里的调用中返回null,导致以后出现NPE