2017-09-27 30 views
0

ユニットテストを実行すると、thisFails()メソッドが3回再試行され、リカバリーロガー行が表示されると予想されますが、一度だけ試してからスローします例外。一番下の出力はテストを実行した後です。Spring Retry再試行が再試行も再試行もしない

私には何が欠けていますか?

このセクションを無視して、コードにジャンプしてください。リンターは投稿するために十分な説明をしていないと思った。私はそれが十分に私の質問を横切るように言われていたと思ったが、何らかの理由で私はこの質問を投稿することは許されない。だからここにはもっとたくさんのものがあります。

--SpringブートApplication--

package com.example.demo; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.retry.annotation.EnableRetry; 

@EnableRetry 
@SpringBootApplication 
public class DemoApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(DemoApplication.class, args); 
    } 
} 

--Service--

package com.example.demo; 

import lombok.extern.slf4j.Slf4j; 
import org.springframework.retry.annotation.Recover; 
import org.springframework.retry.annotation.Retryable; 
import org.springframework.stereotype.Service; 

@Service 
@Slf4j 
public class MyService { 

    @Retryable(include = RuntimeException.class) 
    public int thisFails() { 
    log.info("Help I am failing"); 
    throw new RuntimeException(); 

    } 
    @Recover 
    public int thisRecovers(RuntimeException re) { 
    log.info("I recovered"); 
    return 0; 
    } 
} 

--test Class--

package com.example.demo; 

import static org.junit.Assert.*; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.InjectMocks; 
import org.mockito.runners.MockitoJUnitRunner; 

@RunWith(MockitoJUnitRunner.class) 
public class MyServiceTest { 
    @InjectMocks 
    MyService service; 

    @Test 
    public void recovery(){ 
    service.thisFails(); 
    } 

} 

出力

Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x10983b4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1099034e0). One of the two will be used. Which one is undefined. 
12:58:32.067 [main] INFO com.example.demo.MyService - Help I am failing 

java.lang.RuntimeException 
    at com.example.demo.MyService.thisFails(MyService.java:15) 
    at com.example.demo.MyServiceTest.recovery(MyServiceTest.java:17) 
    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:498) 
    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.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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) 
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 


Process finished with exit code 255 

答えて

4

Spring再試行ではSpring ApplicationContextが必要です。春の@Autowiredの代わりにMockitoの@InjectMocksSpringJunit4ClassRunner(またはそれより新しいもの)と一緒に使用しています。

テスト用にApplicationContextが存在しないため、再試行はありません。