2011-03-29 61 views
0

想对maven-lift-eclipse原型生成的默认Lift片段类进行简单的单元测试。Scala提升JUnit测试失败,出现“error:not found:type foo”使用mvn测试

我把它叫做HelloWorld.scala(在project/src/main/scala/my/namespace/helloworld/snippet/HelloWorld.scala

package my.namespace.helloworld { 
package snippet { 

import _root_.scala.xml.NodeSeq 
import _root_.net.liftweb.util.Helpers 
import _root_.java.util.Date 
import Helpers._ 

class HelloWorld { 
    def howdy(in: NodeSeq): NodeSeq = 
    Helpers.bind("b", in, "time" -> (new Date).toString) 
}}} 

,所以我说添加了一个新的测试,testHowdy,在默认生成AppTest.scala(在project/src/test/scala/my/namespace/AppTest.scala

package my.namespace 

import _root_.java.io.File 
import _root_.junit.framework._ 
import Assert._ 
import _root_.scala.xml.XML 
import _root_.net.liftweb.util._ 
import _root_.net.liftweb.common._ 
import my.namespace._ 

object AppTest { 
    def suite: Test = { 
    val suite = new TestSuite(classOf[AppTest]) 
    suite 
    } 

    def main(args : Array[String]) { 
    _root_.junit.textui.TestRunner.run(suite) 
    } 
} 

/** 
* Unit test for simple App. 
*/ 
class AppTest extends TestCase("app") { 

    /** 
    * Rigourous Tests :-) 
    */ 
    def testOK() = assertTrue(true) 
    // def testKO() = assertTrue(false); 

    def testHowdy() = { 
     val h = new HelloWorld() 
     assertFalse(h.howdy("<!DOCTYPE html><title></title><p><b:time></p>") contains "") 
    } 

    def testXml() = { 
    <snip> 
    } 
} 

有了这额外的测试,我生成编译错误$ mvn test

[INFO] Scanning for projects... 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building helloworld Project 
[INFO] task-segment: [test] 
[INFO] ------------------------------------------------------------------------ 
[INFO] [resources:resources {execution: default-resources}] 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 0 resource 
[INFO] [resources:copy-resources {execution: default-copy-resources}] 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 1 resource 
[INFO] [yuicompressor:compress {execution: default}] 
[INFO] nb warnings: 0, nb errors: 0 
[INFO] [compiler:compile {execution: default-compile}] 
[INFO] Nothing to compile - all classes are up to date 
[INFO] [scala:compile {execution: default}] 
[INFO] Checking for multiple versions of scala 
[INFO] includes = [**/*.scala,**/*.java,] 
[INFO] excludes = [] 
[INFO] Nothing to compile - all classes are up to date 
[INFO] [resources:testResources {execution: default-testResources}] 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 0 resource 
[INFO] [compiler:testCompile {execution: default-testCompile}] 
[INFO] Nothing to compile - all classes are up to date 
[INFO] [scala:testCompile {execution: default}] 
[INFO] Checking for multiple versions of scala 
[INFO] includes = [**/*.scala,**/*.java,] 
[INFO] excludes = [] 
[INFO] /home/noel/workspace/helloworld/src/test/scala:-1: info: compiling 
[INFO] Compiling 2 source files to /home/noel/workspace/helloworld/target/test-classes at 1301433670806 
[ERROR] /home/noel/workspace/helloworld/src/test/scala/my/namespace/helloworld/AppTest.scala:34: error: not found: type HelloWorld 
[INFO] val h = new HelloWorld() 
[INFO]      ^
[ERROR] one error found 
[INFO] ------------------------------------------------------------------------ 
[ERROR] BUILD ERROR 
[INFO] ------------------------------------------------------------------------ 
[INFO] wrap: org.apache.commons.exec.ExecuteException: Process exited with an error: 1(Exit value: 1) 

[INFO] ------------------------------------------------------------------------ 
[INFO] For more information, run Maven with the -e switch 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 14 seconds 
[INFO] Finished at: Tue Mar 29 16:21:18 CDT 2011 
[INFO] Final Memory: 25M/61M 
[INFO] ------------------------------------------------------------------------ 

这看起来像是某种简单的命名空间问题,但我无法追查到我输入错误的地方!

注意:默认生成的AppTest.scala确实运行成功。它在我的额外测试后才开始编译失败。

回答

1

我似乎无法找到你这样做意味着任何进口...

import my.namespace.helloworld.snippet.HelloWorld 
+0

这可能反映了我import'的'无知,但不会'进口my.namespace._'照顾那个? – Noel 2011-03-30 14:51:23

+0

当然不是。我没有在名称空间文档中看到任何提及的导入匹配深度。 “_”实际上做了什么? – Noel 2011-03-30 14:55:11

+1

_只匹配一个实体级别(class/trait/object/package)。所以你也可以这样写: “import my.namespace.helloworld.snippet._” – thoredge 2011-03-30 19:14:40