2016-03-31 5 views
0

yayınlanmadığını testleri:JUnit Parametreli Ben sorunu yeniden oluşturmak için gerekli minimumda soyulmuş aşağıda test sınıfı var mockito, Bahar, Yansıma ile izolasyon ve 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)); 
    } 
} 

Her çalıştırırsanız Testlerden ayrı olarak, iyi çalışıyorlar. Ben .thenReturn ile testGetValues_Fail_MyException yılında .thenThrow değiştirirseniz,

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 

izolasyon kırık ediliyor noktada bu nedenle tüm ince işleri: Ancak hepsi çalıştırırsanız, testGetValues_Fail_Exception hatasıyla mockito hattı üzerinde başarısız olur. Bu sorunu nasıl düzeltebilirim?

+1

Mockito.reset() 'ı denediniz mi? Ayrıca, alayı nasıl kuruyorsun bana çok garip geliyor. 'Çağır' dediğinizde aslında yöntemi çağırıyorsunuz. 'Yöntem' örneklerinin alay olduğuna inanmıyorum. –

+0

Mockito.reset() 'i şimdi denedim ama şans yok. Ancak, doğru olduğunu düşünüyorum, başarısız olma şekli, aslında yöntemde ne zaman deyim olduğunu gösterir. Hala yansımayı kullanırken bunu başka bir şekilde nasıl yapabilirim? –

+0

Acele bir hata yaptım ve herhangi bir nedenden dolayı geçerli olan hiçbir parametreyle sıfırlamayı denemeye çalıştım. Mockito.reset (methodInProviderService) yapmak aslında onu çözdü. Lütfen bunu bir cevap olarak gönderin. –

cevap

1

Sahtecinizi geçerek Mockito.reset() kullanmayı deneyin. Genellikle bu, test arasındaki etkileşimi önlemek için yapılır. Sahnenin önceden yapılandırılmış davranışını sıfırlar. Ancak, durumunuzda, Parameterized'un Spring context ve DirtiesContext ile olağandışı bir kombinasyonunu kullandığınız için daha fazla ayrıntı veremem.

+0

Sadece dikkat etmek gerekirse, Mockito.reset() eklendikten sonra artık DirtiesContext gerekli değildi. –

İlgili konular