|
| 1 | +package org.baeldung.akka; |
| 2 | + |
| 3 | +import java.util.concurrent.TimeUnit; |
| 4 | + |
| 5 | +import akka.actor.ActorRef; |
| 6 | +import akka.actor.ActorSystem; |
| 7 | +import akka.util.Timeout; |
| 8 | +import org.baeldung.akka.GreetingActor.Greet; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Assert; |
| 11 | +import org.junit.Test; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.test.context.ContextConfiguration; |
| 14 | +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; |
| 15 | +import scala.concurrent.Await; |
| 16 | +import scala.concurrent.Future; |
| 17 | +import scala.concurrent.duration.FiniteDuration; |
| 18 | + |
| 19 | +import static akka.pattern.Patterns.ask; |
| 20 | +import static org.baeldung.akka.SpringExtension.SPRING_EXTENSION_PROVIDER; |
| 21 | + |
| 22 | +@ContextConfiguration(classes = AppConfiguration.class) |
| 23 | +public class SpringAkkaTest extends AbstractJUnit4SpringContextTests { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private ActorSystem system; |
| 27 | + |
| 28 | + @Test |
| 29 | + public void whenCallingGreetingActor_thenActorGreetsTheCaller() throws Exception { |
| 30 | + ActorRef greeter = system.actorOf( |
| 31 | + SPRING_EXTENSION_PROVIDER.get(system) |
| 32 | + .props("greetingActor"), "greeter"); |
| 33 | + |
| 34 | + FiniteDuration duration = FiniteDuration.create(1, TimeUnit.SECONDS); |
| 35 | + Timeout timeout = Timeout.durationToTimeout(duration); |
| 36 | + |
| 37 | + Future<Object> result = ask(greeter, new Greet("John"), timeout); |
| 38 | + |
| 39 | + Assert.assertEquals("Hello, John", Await.result(result, duration)); |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + public void tearDown() { |
| 44 | + system.shutdown(); |
| 45 | + system.awaitTermination(); |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments