@After方法首先由JUnit运行

时间:2014-11-07 13:41:59

标签: java junit junit4 spring-junit

我的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

1 个答案:

答案 0 :(得分:0)

@Before方法中出现异常,导致其无法完成:

2014-11-08 13:39:12.169  WARN 4513 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@44c03695] to process 'before' execution of test method [public void com.bignibou.it.service.advertisement.AdvertisementServiceSecurityTest.deleteAdvertisementByIdWithInvalidMember()] for test instance [com.bignibou.it.service.advertisement.AdvertisementServiceSecurityTest@49438269]

java.lang.IllegalStateException: Unable to create SecurityContext using @org.springframework.security.test.context.support.WithUserDetails(value=joe.hacker@gmail.com)
    at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:84)
    at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.beforeTestMethod(WithSecurityContextTestExecutionListener.java:60)
    at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:249)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.security.core.userdetails.UsernameNotFoundException: Username: joe.hacker@gmail.com not found!
    at com.bignibou.service.security.MemberUserDetailsService.loadUserByUsername(MemberUserDetailsService.java:22)
    at org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.createSecurityContext(WithUserDetailsSecurityContextFactory.java:48)
    at org.springframework.security.test.context.support.WithUserDetailsSecurityContextFactory.createSecurityContext(WithUserDetailsSecurityContextFactory.java:36)
    at org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener.createSecurityContext(WithSecurityContextTestExecutionListener.java:82)
    ... 24 common frames omitted

tearDown

使用Spring @BeforeTransaction查看javadocs here允许测试运行并通过:

@BeforeTransaction
    public void setUp() {
        System.out.println("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());
    }

    @AfterTransaction
    public void tearDown() {
        System.out.println("tearDown");
        advertisementFactory.deleteAdvertisement(advertisement);
        memberFactory.deleteMember(impersonator);
    }