Java Testkit - create Actor problem

444 views
Skip to first unread message

Tom Hay

unread,
Apr 7, 2015, 6:38:33 PM4/7/15
to akka...@googlegroups.com
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());
}
}

Anders Båtstrand

unread,
Apr 8, 2015, 4:42:02 AM4/8/15
to akka...@googlegroups.com
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

Roland Kuhn

unread,
Apr 8, 2015, 6:47:23 AM4/8/15
to akka-user
While Anders has already pointed out the flaw, there were a few other things that I’d like to comment on, see inline.

8 apr 2015 kl. 00:36 skrev Tom Hay <tomh...@googlemail.com>:

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

Where exactly did you find it documented like this? There might be mistakes out there, but when I look at the docs I see something slightly different :-)

, 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

What do you mean? TestKit is used my thousands of developers every day, but mostly it just works and therefore does not lead to messages being posted here. We do get a few questions per month, though.

, has everyone given up on Java and moved to the Scala version?

What makes you say this? Our observation is that Akka users are firmly distributed across both sides of the fence—if you want to call it that—and all APIs that we offer present the same power and versatility to each of the audiences.

Or am I doing something stupid? (I have non-testkit Akka systems

I assume that you mean “running outside of tests”, right? Because all Akka systems are accessible to TestKit-based verification—variations in degree are only due to your particular design choices (i.e. which parts of an Actor you make fixed and which are replaceable in a test scenario).

Regards,

Roland

--
>>>>>>>>>> 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 akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Tom Hay

unread,
Apr 8, 2015, 3:30:08 PM4/8/15
to akka...@googlegroups.com
The problem is resolved, root cause was my own inability to follow simple instructions.

@Anders, thank you so much, that resolved the problem. At some point during my attempts to make the test compile (because I picked up old source), the scoping went astray.

@Roland, I must have picked up an old version of the documentation, the cut-and-pasted code didn't compile, from that point on my 'fixes' just made things worse. Regarding Java v Scala, I read too much into the comment in the documentation "Due to the conciseness of test DSLs available for Scala (ScalaTestSpecs2ScalaCheck), it may be a good idea to write the test suite in that language even if the main project is written in Java." Sorry if I sounded like I was criticising the Java API, so far I'm loving it. I'm coming to Akka bearing the scars of a big Erlang project and it's a delight to be able to use industry-standard IDE, tools and libraries!

Thanks again.
Tom.
Reply all
Reply to author
Forward
0 new messages