Versions Compared

Key

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

...

spring test kit 역시 유닛테스를위한 라이브러리이며

akka test kit도 유사하게 설정하여 Junit에서 사용가능합니다.


SpringBoot에서 Actor 테스트 전략

Image Added

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

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

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

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

순서상 위 내용을 먼저 알아야하며 TestTool을 셋팅하기전 위 설정을 먼저 익혀둡니다. 


테스트 코드작성

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), "너의 메시지에 응답을함");				    	
	    }};		
	}
}

full src : http://git.webnori.com/projects/WEBF/repos/spring_cachedb/browse

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

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

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

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

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


공식문서 : https://doc.akka.io/docs/akka/2.5/testing.html