I believe the test should be outside the actor class... The test is not
static, so JUnit has to create an instance of the class using "new".
Best regards,
Anders
onsdag 8. april 2015 00.38.33 UTC+2 skrev Tom Hay følgende:
>
> Hi, I'm an Akka noob so apologies if this is a dumb question. I'm trying
> to make the Testkit work with Java, starting with JUnit testing. Example
> taken from documentation, tweaked to make it compile, source included
> below. When I run the test using JUnit 4 in Eclipse Luna, I get the stack
> trace below. I'm using 2.11-2.3.9 jars. There hasn't been any discussion
> of testkit for a couple of years, has everyone given up on Java and moved
> to the Scala version? Or am I doing something stupid? (I have non-testkit
> Akka systems runnning happily in the same build / run environment).
>
> TIA,
> Tom.
>
> akka.actor.ActorInitializationException: You cannot create an instance of
> [test.MyActor] explicitly using the constructor (new). You have to use one
> of the 'actorOf' factory methods to create a new actor. See the
> documentation.
> at akka.actor.ActorInitializationException$.apply(Actor.scala:165)
> at akka.actor.Actor$class.$init$(Actor.scala:421)
> at akka.actor.UntypedActor.<init>(UntypedActor.scala:97)
> at test.MyActor.<init>(MyActor.java:12)
> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
> at
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
> at
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
> at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
> at
> org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
> at
> org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
> at
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
> at
> org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
> at
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
> at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
> at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
> at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
> at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
> at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
> at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
> at
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
> at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
> at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
>
>
>
> package test;
>
> import static org.junit.Assert.*;
>
> import org.junit.Test;
>
> import akka.actor.ActorSystem;
> import akka.actor.Props;
> import akka.actor.UntypedActor;
> import akka.testkit.TestActorRef;
>
> public class MyActor extends UntypedActor {
> public void onReceive(Object o) throws Exception {
> if (o.equals("say42")) {
> getSender().tell(42, getSelf());
> } else if (o instanceof Exception) {
> throw (Exception) o;
> }
> }
> public boolean testMe() { return true; }
>
>
> @Test
> public void demonstrateTestActorRef() {
> final ActorSystem system = ActorSystem.apply();
> final Props props = Props.create(MyActor.class);
> final TestActorRef<MyActor> ref = TestActorRef.create(system, props,
> "testA");
> final MyActor actor = ref.underlyingActor();
> assertTrue(actor.testMe());
> }
> }
>
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.