Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

유닛테스트는 서비스코드에 실제 사용되는 액터를 테스트해야합니다.

그리고 TestBase는 Spring Application의 초기화과정을 포함하며

여기 아티클은 Spring Boot에서 AKKA를 사용하는 방법에서 시작하였기에

http://wiki.webnori.com/display/AKKA/Spring+Boot+With+AKKA

순서상 위 내용을 먼저 알아야하며 TestTool을 셋팅하기전, SpringBoot에  SpringBoot에  AKKA를 내장하여야 합니다.


테스트 코드작성

Code Block
languagejava
themeEmacs
import akka.testkit.javadsl.TestKit;


@RunWith(SpringRunner.class)
@SpringBootTest
public class CachedbApplicationTests {	
	@Autowired
	ApplicationContext context;
	
	@Test
	public void contextLoads() {
		ActorSystem system = context.getBean(ActorSystem.class);
		SpringExtension ext = context.getBean(SpringExtension.class);		
		actorTest2(system,ext);
	}
	
	protected void actorTest2(ActorSystem system,SpringExtension ext) {
		ActorRef testActor = system.actorOf(ext.props("testActor"),"service1");		
	    new TestKit(system) {{			
			testActor.tell( "hi", getRef() );
		      // await the correct response
		    expectMsg(java.time.Duration.ofSeconds(1), "너의 메시지에 응답을함");				    	
	    }};		
	}
}

...

분석:  Spring의 요소가 다 로드되고나서(contextLoads()) 다음 유닛테스트가 수행됩니다. - 이것은 SpringBoot의 테스트요소이며

Akka테스트를 포함시켰습니다.  이전Part에서 testActor에 인사를 하면 '너의 메시지에 응답을함' 이라고 반응하는

액터를 이미 생성을 하였습니다.  TestKit은 메시지를 전달받을수 있으며 expectMsg 라는 검사기를 통해 

비동기의 흐름을 끊지 않고  메시지처리에대한 유닛테스트기를 작성할수 있습니다.

또한 JAVA8의 새로운 비동기 기능인 CompletableFuture<> 활용도가능합니다.



테스트 컨셉

만약 이러한 검사기가 없다고하면, 액터에게 메시지 전송후 그것을 기다려야하는 코드를 작성해야 했을것입니다.

...