Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to just test Java code from Scala then it is quite easy to set up such a maven project. Since I am not an eclipse user I am not sure how it works with eclipse. I have tested with IntelliJ and it works very well. Shouldn't be any problems with eclipse either.</p> <p>I created a simple <code>pom.xml</code> that just uses the scala compiler for the tests and uses the normal java compiler for the main java code.</p> <p><code>pom.xml</code></p> <p></p> <pre><code>&lt;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"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;groupId&gt;com.stackoverflow.Q13379591&lt;/groupId&gt; &lt;artifactId&gt;Q13379591&lt;/artifactId&gt; &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt; &lt;name&gt;${project.artifactId}-${project.version}&lt;/name&gt; &lt;properties&gt; &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt; &lt;scala.version&gt;2.9.2&lt;/scala.version&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.scala-lang&lt;/groupId&gt; &lt;artifactId&gt;scala-library&lt;/artifactId&gt; &lt;version&gt;${scala.version}&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.scalatest&lt;/groupId&gt; &lt;artifactId&gt;scalatest_${scala.version}&lt;/artifactId&gt; &lt;version&gt;2.0.M4&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.scala-tools&lt;/groupId&gt; &lt;artifactId&gt;maven-scala-plugin&lt;/artifactId&gt; &lt;version&gt;2.15.2&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;scala-test-compile&lt;/id&gt; &lt;phase&gt;test-compile&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;testCompile&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;configuration&gt; &lt;args&gt; &lt;arg&gt;-g:vars&lt;/arg&gt; &lt;arg&gt;-make:transitive&lt;/arg&gt; &lt;arg&gt;-dependencyfile&lt;/arg&gt; &lt;arg&gt;${project.build.directory}/.scala_dependencies&lt;/arg&gt; &lt;/args&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;plugin&gt; &lt;groupId&gt;org.scalatest&lt;/groupId&gt; &lt;artifactId&gt;scalatest-maven-plugin&lt;/artifactId&gt; &lt;version&gt;1.0-M2&lt;/version&gt; &lt;configuration&gt; &lt;reportsDirectory&gt;${project.build.directory}/surefire-reports&lt;/reportsDirectory&gt; &lt;junitxml&gt;.&lt;/junitxml&gt; &lt;filereports&gt;WDF TestSuite.txt&lt;/filereports&gt; &lt;stdout&gt;W&lt;/stdout&gt; &lt;!-- Skip coloring output --&gt; &lt;/configuration&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;scala-test&lt;/id&gt; &lt;goals&gt; &lt;goal&gt;test&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;/project&gt; </code></pre> <p>Now here's a simple Java class that I want to test:</p> <p><code>src/main/java/com/stackoverflow/Hello.java</code></p> <pre><code>package com.stackoverflow; /** * @author maba, 2012-11-14 */ public interface Hello { String hello(); } </code></pre> <p><code>src/main/java/com/stackoverflow/HelloJava.java</code></p> <pre><code>package com.stackoverflow; /** * @author maba, 2012-11-14 */ public class HelloJava implements Hello { public String hello() { return "Hello Java"; } } </code></pre> <p>And finally the ScalaTest test class.</p> <p><code>src/test/scala/com/stackoverflow/HelloJavaTest.scala</code></p> <pre><code>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 =&gt; true } assert(result) } it should "say Hello Java" in { val helloJava = new HelloJava assert(helloJava.hello === "Hello Java") } } </code></pre> <p>You can now run it with this command:</p> <pre><code>mvn test </code></pre> <p>I can just right click on the test case in IntelliJ and run the test. It should be possible in eclipse as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload