假设我有以下简单的 JUnit 测试
private Person person;
@Before
public void createObject()
{
String firstName = "John";
String lastName = "Doe";
person = new Person(firstName, lastName);
}
@Test
public void test1()
{
assert(firstName.equals("John"));
}
@Test
public void test2()
{
assert(lastName.equals("Doe"));
}
我应该有一个设置 person = null
的 @After 方法吗?
我在没有 @After 方法的情况下尝试了类似的测试,我似乎无法弄清楚将它放在那里有任何优点或缺点。
忽略测试名称不是很有意义。我只是发布了一个简单的示例。
最佳答案
可以,但是没有意义。 @Before
方法在每次 测试运行之前被调用。 Person
将在使用和不使用@After
时进行 GC。
关于java - JUnit:调用@After 将对象设置为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23685431/