-1

I have written a method deleteProject(Project project) in Administrator.java which sets the project to null. After this I tried to write a test method which calls the deleteProject(Project project)-method and checks if the Object project == null. This is not the case. I have put 3 system.out.println(project) into the methods. If you look at the output the project is set to null in the deleteProject(Project project), but for some reason it still printed in AdministratorTest.

I can't figure out why project doesn't stay 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);        
}

Output

model.Project@6d5380c2
null
model.Project@6d5380c2

1 Answers1

1

Setting the parameter project to null in the method deleteProject does nothing to the object you gave to that method. It just changes the parameter which goes out of scope at the end of deleteProject.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142