我有一个名为TestBase
的课程和两个方法setUp()
和tearDown()
。有这些方法的代码:
@Before
public void setUp() throws Exception {
app = ApplicationManager.getInstance();
}
@After
public void tearDown() throws Exception {
app.stop();
}
此外,我有两个类,它们来自TestBase
,并且方法带有@Test
注释:
public class SubscriptionTests extends TestBase<Subscription> {
@Test
public void testValidSubscription() {
DataGenerator.createTestData(Subscription.class);
Subscription subscription = (Subscription) getFromXml("subscription", new String[]{"email"}).get(0);
app.getModelHelper().createModel(subscription);
}
}
public class UserRegistrationTests extends TestBase<User> {
@Test
public void testRegistrationValidUser() {
DataGenerator.createTestData(User.class);
List<BaseModel> users = getFromXml("user", new String[]{ "email", "name", "password", "passwordConfirmation"});
User validUser = (User) users.get(0);
app.getModelHelper().createModel(validUser);
}}
我尝试开始测试。第一个成功结束,但在此之后调用tearDown()
类中的方法TestBase
(带@After
注释)并获得SessionNotFoundException
。结果,第二次测试没有开始。怎么了?
答案 0 :(得分:2)
@Before
和@After
标记(实例)方法。也许您希望@BeforeClass
和@AfterClass
标记(静态)方法分别仅在开头和结尾处运行一次。