2016-03-05 63 views
-1

我在Administrator.java中寫了一個方法deleteProject(Project project),它將項目設置爲null。在此之後,我嘗試編寫一個調用deleteProject(Project project)方法的測試方法,並檢查Object項目== null。不是這種情況。我已將3個system.out.println(項目)放入方法中。如果查看輸出項目,在deleteProject(Project項目)中將項目設置爲null,但由於某些原因,它仍然以AdministratorTest打印。JUnit test刪除方法Object設置爲null後不爲空

我不明白爲什麼項目不保留爲空。

AdministratorTest.java

@Before 
public void setUp() throws Exception { 
    admin1 = new Administrator("Sponge", "Bob", "Squarepants", "Spongy");  
    project = admin1.createProject(new VersionID(2, 3), "ProjectX", "Secret", new Date(93, 10, 17));   
} 

@After 
public void tearDown() throws Exception { 
    admin1 = null;  
    project = null; 
} 

@Test 
public void testDeleteProject() { 
    assertEquals("2.3", project.getVersion().toString()); 
    assertEquals("ProjectX", project.getName()); 
    assertEquals("Secret", project.getDescription()); 
    assertEquals("Wed Nov 17 00:00:00 CET 1993", project.getStartDate().toString()); 
    admin1.deleteProject(project); 
    System.out.println(project); 
    assertNull(project); 
} 

Administrator.java

public void deleteProject(Project project) {  
    System.out.println(project);   
    project = null; 
    System.out.println(project);   
} 

輸出

[email protected] 
null 
[email protected] 

回答

1

設置噸他將參數項目設置爲null,方法deleteProject對您給該方法的對象不做任何處理。它只是改變deleteProject結束時超出範圍的參數。

相關問題