如果您只想测试 Scala 中的 Java 代码,那么设置这样的 Maven 项目非常容易。因为我不是 eclipse 用户,所以我不确定它如何与 eclipse 一起使用。我已经用 IntelliJ 进行了测试,效果非常好。eclipse 也不应该有任何问题。
我创建了一个简单的程序pom.xml
,仅使用 scala 编译器进行测试,并使用普通的 java 编译器来处理主要的 java 代码。
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow.Q13379591</groupId>
<artifactId>Q13379591</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.version>2.9.2</scala.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.version}</artifactId>
<version>2.0.M4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>scala-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<args>
<arg>-g:vars</arg>
<arg>-make:transitive</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0-M2</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
<stdout>W</stdout> <!-- Skip coloring output -->
</configuration>
<executions>
<execution>
<id>scala-test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
现在这是一个我想要测试的简单 Java 类:
src/main/java/com/stackoverflow/Hello.java
package com.stackoverflow;
/**
* @author maba, 2012-11-14
*/
public interface Hello {
String hello();
}
Run Code Online (Sandbox Code Playgroud)
src/main/java/com/stackoverflow/HelloJava.java
package com.stackoverflow;
/**
* @author maba, 2012-11-14
*/
public class HelloJava implements Hello {
public String hello() {
return "Hello Java";
}
}
Run Code Online (Sandbox Code Playgroud)
最后是 ScalaTest 测试类。
src/test/scala/com/stackoverflow/HelloJavaTest.scala
package com.stackoverflow
import org.scalatest.FlatSpec
/**
* @author maba, 2012-11-14
*/
class HelloJavaTest extends FlatSpec {
"HelloJava" should "be instance of Hello" in {
val hello = new HelloJava
val result = hello match {
case f:Hello => true
}
assert(result)
}
it should "say Hello Java" in {
val helloJava = new HelloJava
assert(helloJava.hello === "Hello Java")
}
}
Run Code Online (Sandbox Code Playgroud)
您现在可以使用以下命令运行它:
mvn test
Run Code Online (Sandbox Code Playgroud)
我只需右键单击 IntelliJ 中的测试用例并运行测试即可。在日食中也应该是可能的。
归档时间: |
|
查看次数: |
1379 次 |
最近记录: |