Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 637
siledh Avatar asked Nov 07 '13 15:11

siledh


3 Answers

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.

like image 172
Faiz Avatar answered Sep 27 '22 16:09

Faiz


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.

like image 37
roterl Avatar answered Sep 27 '22 17:09

roterl


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.
like image 39
Dmytro Mitin Avatar answered Sep 27 '22 16:09

Dmytro Mitin