gpt4 book ai didi

java - @After 方法首先由 JUnit 运行

转载 作者:行者123 更新时间:2023-12-02 04:52:18 24 4
gpt4 key购买 nike

我的 JUnit 测试用例遇到了一个奇怪的问题:@After 方法首先由 JUnit 调用。

这是我用于测试的抽象类:

@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

最佳答案

似乎在 @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);
}

关于java - @After 方法首先由 JUnit 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26802607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com