2016-03-31 39 views
0

運行我有下面的測試類,它已被剝奪,以重現該問題所需的最低限度:JUnit的參數化測試不符合的Mockito,春天,思考隔離和AspectJ

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

如果我運行的每個的測試分開,他們工作得很好。但是,如果我跑所有這些,testGetValues_Fail_Exception上的Mockito線失敗,錯誤:

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 

如果我.thenReturn更換.thenThrowtestGetValues_Fail_MyException,這一切工作正常,所以在某些點上的隔離正在被打破。 我該如何解決這個問題?

+1

你試過'Mockito.reset()'?另外,你如何設置嘲笑似乎真的很奇怪。當你調用'invoke'時,你實際上正在調用這個方法。我不相信'Method'的例子是嘲弄的。 –

+0

我已經嘗試Mockito.reset()現在,但沒有運氣。不過,我認爲你是正確的,它的失敗方式表明它實際上是在調用when語句中的方法。任何想法如何在仍然使用反射的同時以其他方式做到這一點? –

+0

我犯了一個匆忙的錯誤,只是試圖調用沒有參數,由於某種原因是有效的重置。做Mockito.reset(methodInProviderService)實際上解決了它。請張貼這個答案。 –

回答

1

嘗試使用Mockito.reset()通過你的模擬。通常這是爲了防止測試之間的交互。它重置以前配置的模擬行爲。然而,在你的情況下,我不能提供更多的細節,因爲你正在使用Parameterized與Spring上下文和DirtiesContext的不尋常組合。

+0

只需注意,添加Mockito.reset()後,DirtiesContext不再是必需的。 –