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確實運行成功。它在我的額外測試後纔開始編譯失敗。
這可能反映了我import'的'無知,但不會'進口my.namespace._'照顧那個? – Noel 2011-03-30 14:51:23
當然不是。我沒有在名稱空間文檔中看到任何提及的導入匹配深度。 「_」實際上做了什麼? – Noel 2011-03-30 14:55:11
_只匹配一個實體級別(class/trait/object/package)。所以你也可以這樣寫: 「import my.namespace.helloworld.snippet._」 – thoredge 2011-03-30 19:14:40