简体   繁体   English

如何使用ScalaCheck测试Java程序?

[英]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进行测试,那么您应该考虑使用ScalaTestSpecs2

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.

相关问题 在哪里可以找到一组带有注入错误的Java测试程序? - Where can I find a set of Java test programs with injected errors? 如何在Java 6而不是Java 7或8中编译程序? - How can I compile programs in Java 6 rather than Java 7 or 8? 如何在Windows 7上删除Java程序的标题栏和任务栏图标? - How can I remove titlebar and taskbar icons of Java programs on Windows 7? 用Java编写的程序如何不需要JRE? - How can programs written in Java not require the JRE? 如何在其他 Java 程序可以与之交互的服务器上运行 .java 文件? - How do I run a .java file on a server that other java programs can interact with? Java多线程:如何启动正在执行线程的外部Java程序? - Java Multithreading: How can i start external Java Programs, which are executing threads? 我该如何更改程序图标 - how can I change the programs icon 如何从命令行查看内存使用情况,java程序的线程转储? - How can I view memory usage, thread dump for java programs, from command line? 当数据写入协议缓冲区时,如何使用Java触发C ++程序,反之亦然? - How can I have Java trigger C++ programs and vice versa when data is written to protocol buffers? 仅使用JRE,如何在没有JDK的情况下能够编译和运行Java程序? - How can I able to compile and run java programs without JDK with just JRE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM