2012-10-23 157 views
0

我有一個akka maven項目,我想運行單元測試。java單元測試

我已經定義了測試類,但是當我運行它時,我顯示了一個錯誤。

演員類代碼片段下面給出:

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")); 

    } 
} 

行家還包含一個默認的測試類。默認測試類和Actortest類都位於src/test/java文件夾中。

這是POM文件下面

[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 

<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> 

錯誤消息給予我一定的任何配置添加到application.conf文件

希望有人幫助我出去了。

+3

** java.lang.ClassCastException **這意味着您正在類型轉換爲與該類沒有關係的類..請檢查一次您錯誤地類型轉換的位置..它不是配置問題... :) –

+0

你能顯示你的pom文件嗎? – khmarbaise

+0

我按照您的建議添加了pom文件@ Mr.Chowdary –

回答

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正在發回正確類型的消息(例如Result)。

下一步是取消對initial_Theta()的呼叫,並找出爲什麼沒有正確填充Result

+0

感謝您的建議,已經能夠運行沒有類轉換錯誤,但我得到一個空結果和測試失敗。看起來問題是如何將測試數據注入到actor類中。而它的演員沒有運行就有這方面的建議。 –

+0

@faisalabdulai首先簡化事物。將'WorkerActor'改爲:'getSender()。tell(new Result());'並在'ActorTest'中註釋掉'assertEquals'。然後你的測試通過。然後添加另一個測試,在其中驗證「結果」的內容。 – vegemite4me

+0

謝謝,會盡力回覆你 –