[英]How can I test Java programs with ScalaCheck?
I have read in the ScalaCheck user guide that it's a tool for testing Scala and Java programs . 我在ScalaCheck 用户指南中读到它是测试Scala和Java程序的工具 。
I wonder, is it just marketing, or testing Java-only codebase with it would be a reasonable idea? 我想知道,这只是营销,还是测试仅使用Java的代码库是一个合理的想法? And if so, what's the best way to integrate it with the Java project?
如果是这样,将它与Java项目集成的最佳方法是什么?
No, it's not just marketing. 不,这不仅仅是营销。 Funcional Java ( http://functionaljava.org/ ) is tested with ScalaCheck.
功能Java( http://functionaljava.org/ )使用ScalaCheck进行测试。 Some test cases in the FJ sources: https://github.com/functionaljava/functionaljava/blob/724081f0f87f34b2f4c26b8b748877955180ecaa/props-core-scalacheck/src/test/scala/fj/data/CheckList.scala
FJ来源中的一些测试用例: https : //github.com/functionaljava/functionaljava/blob/724081f0f87f34b2f4c26b8b748877955180ecaa/props-core-scalacheck/src/test/scala/fj/data/CheckList.scala
I'm not sure what's the best way to integrate ScalaCheck into an existing java project but I guess you could borrow some ideas from how it's done in FJ. 我不确定将ScalaCheck集成到现有Java项目中的最佳方法是什么,但我想你可以借鉴一下FJ中的一些想法。
You can integrate it to test Java code. 您可以将其集成到测试Java代码中。 The test code will be written in Scala using ScalaCheck, but the tested code may be either Scala or Java.
测试代码将使用ScalaCheck在Scala中编写,但测试的代码可能是Scala或Java。 If you already use scala for tests then you should consider using ScalaTest or Specs2 .
如果您已经使用scala进行测试,那么您应该考虑使用ScalaTest或Specs2 。
Actually you can write ScalaCheck tests in pure Java although you'll have to resolve Scala implicits manually. 实际上,你可以用纯Java编写ScalaCheck测试,尽管你必须手动解决Scala implicits。
For example the code in Scala 例如Scala中的代码
import org.scalacheck.Properties
import org.scalacheck.Prop.forAll
object CommutativityTest extends Properties("Commutativity Test") {
property("commutativity") = forAll { (a: Int, b: Int) =>
a + b == b + a
}
}
with build.sbt 使用build.sbt
scalaVersion := "2.12.3"
libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.5" % Test
can be translated to Java as 可以翻译成Java
import org.scalacheck.*;
import org.scalacheck.util.Pretty;
import scala.math.Numeric;
public class CommutativityTest$ extends Properties {
public static final CommutativityTest$ MODULE$ = new CommutativityTest$();
public CommutativityTest$() {
super("Commutativity Test");
Arbitrary arbInt = Arbitrary.arbInt();
Shrink shrinkInt = Shrink.shrinkIntegral(Numeric.IntIsIntegral$.MODULE$);
Prop prop = Prop.forAll(
(Integer a, Integer b) -> a + b == b + a,
Prop::propBoolean,
arbInt,
shrinkInt,
Pretty::prettyAny,
arbInt,
shrinkInt,
Pretty::prettyAny
);
property().update("commutativity", () -> prop);
}
}
and 和
public class Runner {
public static void main(String[] args) {
CommutativityTest$.MODULE$.main(args);
}
}
with pom.xml 用pom.xml
<project...
<dependencies>
<dependency>
<groupId>org.scalacheck</groupId>
<artifactId>scalacheck_2.12</artifactId>
<version>1.13.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang.modules</groupId>
<artifactId>scala-java8-compat_2.12</artifactId>
<version>0.8.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The output: 输出:
+ Commutativity Test.commutativity: OK, passed 100 tests.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.