JUnit参数化测试不是与Mockito,Spring,Reflection和AspectJ隔离运行的

时间:2016-03-31 10:04:33

标签: java spring reflection junit mockito

我有以下测试类,它已被剥离到重现问题所需的最低限度:

@RunWith(Parameterized.class)
@ContextConfiguration(locations = { "classpath:restful-service-test-context.xml" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class ResftulServiceTest {

    @Autowired
    @Qualifier("failedAspectTestMessage")
    private String failedAspectTestMessage;

    @Autowired
    private ProviderService ProviderService;

    private String methodName;
    private Set valuesToReturnByMock;
    private TestContextManager testContextManager;

    public ResftulServiceTest(String methodName, String[] valuesToReturnByMockArray) {
        this.methodName = methodName;
        this.valuesToReturnByMock = new HashSet<>(Arrays.asList(valuesToReturnByMockArray));
    }

    @Parameterized.Parameters
    public static Collection values() {
        return Arrays.asList(new Object[][] { { "getCountries", new String[] { "GB", "US" } }, });
    }

    @Before
    public void setUpSpringContext() throws Exception {
        testContextManager = new TestContextManager(getClass());
        testContextManager.prepareTestInstance(this);
    }

    @Test
    public void testGetValues_Fail_MyException() throws Exception {
        Method methodInProviderService = ProviderService.class.getDeclaredMethod(methodName);

        Mockito.when(methodInProviderService.invoke(ProviderService))
                .thenThrow(new MyException(failedAspectTestMessage, StatusCodeType.ERROR));
    }

    @Test
    public void testGetValues_Fail_Exception() throws Exception {
        Method methodInProviderService = ProviderService.class.getDeclaredMethod(methodName);

        Mockito.when(methodInProviderService.invoke(ProviderService))
                .thenThrow(new AspectException(failedAspectTestMessage));
    }
}

如果我单独运行每个测试,它们都能正常工作。但是,如果我运行所有这些操作,则{M}中的testGetValues_Fail_Exception会失败并显示错误:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com<obfuscated>.ResftulServiceTest.testGetValues_Fail_Exception(ResftulServiceTest.java:111)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: com.<obfuscated>.MyException: FAILED_ASPECT_TEST_MESSAGE
    ... 40 more

如果我将.thenThrow中的testGetValues_Fail_MyException替换为.thenReturn,则一切正常,因此在某些时候隔离正在被破坏。 如何解决此问题?

1 个答案:

答案 0 :(得分:1)

尝试使用Mockito.reset()传递您的模拟。通常这样做是为了防止测试之间的相互作用。它重置先前配置的模拟行为。但是,在您的情况下,我无法提供更多详细信息,因为您使用Parameterized与Spring上下文和DirtiesContext的不寻常组合。