How can I test Java programs with ScalaCheck?
Asked Answered
K

3

8

I have read in the ScalaCheck user guide that it's a tool for testing Scala and Java programs.

I wonder, is it just marketing, or testing Java-only codebase with it would be a reasonable idea? And if so, what's the best way to integrate it with the Java project?

Kostman answered 7/11, 2013 at 15:53 Comment(0)
K
8

No, it's not just marketing. Funcional Java (http://functionaljava.org/) is tested with 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

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.

Krak answered 7/11, 2013 at 16:14 Comment(1)
The class has moved, it's better to link to a specific revision: github.com/functionaljava/functionaljava/blob/…Frumentaceous
E
3

You can integrate it to test Java code. The test code will be written in Scala using ScalaCheck, but the tested code may be either Scala or Java. If you already use scala for tests then you should consider using ScalaTest or Specs2.

Expostulatory answered 10/11, 2013 at 9:35 Comment(1)
ScalaTest will be easier to learn for someone coming from a Java background. Specs2 is a more functional-programming-oriented testing framework.Daveta
H
2

Actually you can write ScalaCheck tests in pure Java although you'll have to resolve Scala implicits manually.

For example the code in 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

scalaVersion := "2.12.3"

libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.5" % Test

can be translated to Java as

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

<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.
Hypnotize answered 26/8, 2017 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.