2017-08-02 21 views
-4

數組列表我有ArrayList中包含學生的ID和這個測試代碼:如何編寫斷言NOTNULL對於在JUnit

@Test public void studidtest() { 
    String arrdata={stud1 stud2 stud 3 stud 4 }; 
    for (i=0;¡<arrdata ; i++) { 
    assertNotNull(students.stdids).equals(arradata[i]) 
    } 

這不起作用 - 那麼如何寫一個正確的測試案例?

+3

你甚至谷歌這個? 'Assert.assertNotNull' – vikingsteve

+0

這個問題的另一個答案可以在這裏看到:https://stackoverflow.com/questions/7123555/assertnull-should-be-used-or-assertnotnull –

+0

@prasanna更新你的問題與這個信息,做沒有把它納入評論。 –

回答

0

第一關。你需要努力向我們展示你在解決問題的過程中走了多遠。我們可以更容易地寫下你需要的反饋,以便能夠了解這個問題的任何內容。

我會開始,因爲你以前從未寫過任何junit測試課。

你開始使用你的方法。

@Test 
public void testStudentId() { 

} 

現在,我們需要測試數據。最簡單的方法就是硬編碼,但如果你有其他選擇,你可以使用它。爲了讓自己更容易,我只是假設我有一些學生數據要通過。

@Test 
    public void testStudentId() { 
     ArrayList<Student> students = new ArrayList<>(); 

    } 

現在我們需要通過arraylist的每個元素。

@Test 
    public void testStudentId() { 
     ArrayList<Student> students = new ArrayList<>(); 
     for (Student student: students) { 
      Assert.assertNotNull(student); 
     } 

    } 

Wollah,你明白了。如果學生obj爲空,它將被標記爲錯誤。

0

我覺得這是你所需要的:

@Test 
public void testId(){ 
    List<User> users = getTestUsers(); // load your testUsers as you want. 
    users.stream().map(User::getId).forEach(Assert::assertNotNull); 
}