0

我有一个 akka maven 项目,我想对其进行单元测试。

我已经定义了测试类,但是当我运行它时,我显示了一个错误。

演员类代码片段如下:

public class WorkerActor extends UntypedActor {

    @Override
    public void onReceive(Object msg) throws Exception {

         if(msg instanceof GesturePoints) 
         { GesturePoints message = (GesturePoints) msg;
         Result rslt = initial_Theta(message);
         getSender().tell(rslt);
          } 

         else {unhandled(msg);}
    }

    public Result initial_Theta(GesturePoints p) 
    {           
        Result result = new Result();

        if(p.getPoints().get("x").size() < 3)  //The number of x coordinates as size
        {   return null;}                    




 dx = (double) ((Integer)p.getPoints().get("x").get(2)  - (Integer)p.getPoints().get("x").get(0)) ;

  dy = ((Double)p.getPoints().get("y").get(2)) - ((Double)p.getPoints().get("y").get(0));

             {                                          
                   result.setResult("feature1", initial_cos);
                   result.setResult("feature2", initial_sin);

         } 
             return result;
         }  
}

测试类在下面的代码中给出

public class ActorTest {

    TestKit testKit;
    TestActorRef<WorkerActor>  testedActor ;
    TestProbe tProbe;

    @Before
    public void setUp()
    {   

        testKit = new TestKit(ActorSystem.apply());
        tProbe =  TestProbe.apply(testKit.system());
        testedActor = TestActorRef.apply(new Props(new UntypedActorFactory()
                {

            public WorkerActor create()
            {
                return new WorkerActor();
            }
                }),testKit.system());
    }

    @Test
    public void testWorkerActor()
    {    
        ArrayList<Object> x = new ArrayList<Object>();
        ArrayList<Object> y = new ArrayList<Object>();
        x.add(0, 1); x.add(1, 2); x.add(2, 3); x.add(3, 4); x.add(4, 5);
        y.add(0, 1.00); y.add(1, 2.00); y.add(2, 3.00); y.add(3, 4.00); y.add(4, 5.00);

        Map<String, ArrayList<Object>> map = new HashMap<String, ArrayList<Object>>();
        map.put("x", x);
        map.put("y", y);

        GesturePoints gp = new GesturePoints();
          gp.setPoints(map);

         tProbe = TestProbe.apply(testKit.system());
    testedActor.tell(gp , tProbe.ref());

    TestActor.Message message = tProbe.lastMessage();
    Result result = (Result)message;


assertEquals(0.25 , result.getResult().get("x"));
    assertEquals(0.25 , result.getResult().get("y"));

    }
}

maven 还包含一个默认的测试类。故障测试类和Actortest类都位于该src/test/java文件夹中。

这是pom文件

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.theta.gesture</groupId>
    <artifactId>com-theta-gesture</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>com-theta-gesture</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>Com.RubineEngine.GesturePoints</groupId>
            <artifactId>Com-RubineEngine-GesturePoints</artifactId> 
            <version>1.0-SANPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.result.gesture</groupId>
            <artifactId>com-result-gesture</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor</artifactId>
            <version>2.0.2</version>
        </dependency> 

        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-remote</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-slf4j</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-testkit</artifactId>
            <version>2.0.2</version>
        </dependency>

        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-kernel</artifactId>
            <version>2.0.2</version>
        </dependency>            
    </dependencies>

    <repositories>
        <repository>
            <id>typesafe</id>
            <name>Typesafe Repository</name>
            <url>http://repo.typesafe.com/typesafe/releases/</url>
        </repository>
        <repository>
            <id>repo</id>
            <name>repo</name>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <url>file://${project.basedir}/repo</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.12.4</version>
                    </dependency>
                </dependencies>
            </plugin>   
        </plugins>
    </build>    
</project>

错误信息如下

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building com-theta-gesture 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ com-theta-gesture ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ com-theta-gesture ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ com-theta-gesture ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\FAISAL\Desktop\disaster\com-theta-gesture\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ com-theta-gesture ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ com-theta-gesture ---
[WARNING] The POM for org.apache.maven:maven-plugin-api:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-artifact:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-project:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-core:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.maven:maven-toolchain:jar:2.0.9 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] Surefire report directory: C:\Users\FAISAL\Desktop\disaster\com-theta-gesture\target\surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Concurrency config is parallel='none', perCoreThreadCount=true, threadCount=2, useUnlimitedThreads=false
Running com.theta.gesture.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.011 sec
Running com.theta.gesture.ActorTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.562 sec <<< FAILURE!
testWorkerActor(com.theta.gesture.ActorTest)  Time elapsed: 0.002 sec  <<< ERROR!
java.lang.ClassCastException: akka.testkit.TestActor$NullMessage$ cannot be cast to com.result.gesture.Result
    at com.theta.gesture.ActorTest.testWorkerActor(ActorTest.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
    at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:62)
    at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:139)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error: 
  testWorkerActor(com.theta.gesture.ActorTest): akka.testkit.TestActor$NullMessage$ cannot be cast to com.result.gesture.Result

Tests run: 2, Failures: 0, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.098s
[INFO] Finished at: Tue Oct 23 05:24:12 BST 2012
[INFO] Final Memory: 16M/106M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project com-theta-gesture: There are test failures.
[ERROR] 
[ERROR] Please refer to C:\Users\FAISAL\Desktop\disaster\com-theta-gesture\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

我是否必须将任何配置添加到 application.conf 文件

希望有人能帮助我。

4

1 回答 1

0

在你的ActorTest,尝试更换

TestActor.Message message = tProbe.lastMessage();
Result result = (Result)message;

有了这个:

tProbe.expectMsgClass(Result.class);     
//TestActor.Message message = tProbe.lastMessage();
//Result result = (Result) message.msg();           <- Important

然后改成WorkerActor这样:

if(msg instanceof GesturePoints) { 
    GesturePoints message = (GesturePoints) msg;
    //Result rslt = initial_Theta(message);
    //getSender().tell(rslt);
    getSender().tell(new Result());
}

那应该会过去的。这将验证WorkerActor发送回正确类型的消息(例如 a Result)。

下一步是取消注释您的调用initial_Theta()并找出为什么没有Result正确填充。

于 2012-10-23T10:53:17.520 回答