我在我的JUnit测试用例中遇到了一个奇怪的问题:JUnit首先调用@After
方法。
这是我用于测试的抽象类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { TestServicePlusRepositoryIntegrationConfiguration.class })
@ActiveProfiles(Profiles.TEST)
@Transactional
public abstract class AbstractServicePlusRepositoryIntegrationTest {
}
以下是有问题的测试用例:
public class AdvertisementServiceSecurityTest extends AbstractServicePlusRepositoryIntegrationTest {
...
@Before
public void setUp() {
advertisement = advertisementFactory.createAdvertisement(OWNER_OF_ADVERTISEMENT_EMAIL);
impersonator = memberFactory.createMember(IMPERSONATOR_EMAIL, Role.ROLE_BASIC_CHILDCAREWORKER);
when(geolocationServiceMock.retrieveAddressFromReference("valid-reference")).thenReturn(new Address());
}
@After
public void tearDown() {//Executed first!
advertisementFactory.deleteAdvertisement(advertisement);
memberFactory.deleteMember(impersonator);
}
...
@Test(expected = AccessDeniedException.class)
@WithUserDetails(IMPERSONATOR_EMAIL)
public void deleteAdvertisementByIdWithInvalidMember() {
advertisementService.deleteAdvertisement(advertisement.getId());
}
什么原因会导致@After
方法先于其他方法运行?顺便提一下,我没有使用JUnit 3。
这是测试用例的完整源代码:https://gist.github.com/balteo/e8c2ea96db22d3f9c88e