Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing scala, maven and java within Eclipse - unit testing

I have a java maven project that I want to unit test with scala. But how can I mix java and scala code within the one Eclipse project since java and scala uses their own compilers. Because of this scala code will not compile in Eclipse since the java compiler expects java syntax.

Currently my projects are based on Eclipse and they are java based projects. Do they need to be converted to a different project type such a scala ?

like image 281
user701254 Avatar asked Nov 14 '12 13:11

user701254


1 Answers

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.

I created a simple pom.xml that just uses the scala compiler for the tests and uses the normal java compiler for the main java code.

pom.xml

<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow.Q13379591</groupId>
    <artifactId>Q13379591</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <scala.version>2.9.2</scala.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
        <dependency>
            <groupId>org.scalatest</groupId>
            <artifactId>scalatest_${scala.version}</artifactId>
            <version>2.0.M4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>
                    <execution>
                        <id>scala-test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-g:vars</arg>
                        <arg>-make:transitive</arg>
                        <arg>-dependencyfile</arg>
                        <arg>${project.build.directory}/.scala_dependencies</arg>
                    </args>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
                <version>1.0-M2</version>
                <configuration>
                    <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                    <junitxml>.</junitxml>
                    <filereports>WDF TestSuite.txt</filereports>
                    <stdout>W</stdout> <!-- Skip coloring output -->
                </configuration>
                <executions>
                    <execution>
                        <id>scala-test</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Now here's a simple Java class that I want to test:

src/main/java/com/stackoverflow/Hello.java

package com.stackoverflow;

/**
 * @author maba, 2012-11-14
 */
public interface Hello {
    String hello();
}

src/main/java/com/stackoverflow/HelloJava.java

package com.stackoverflow;

/**
 * @author maba, 2012-11-14
 */
public class HelloJava implements Hello {

    public String hello() {
        return "Hello Java";
    }
}

And finally the ScalaTest test class.

src/test/scala/com/stackoverflow/HelloJavaTest.scala

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 => true
    }
    assert(result)
  }

  it should "say Hello Java" in {
    val helloJava = new HelloJava
    assert(helloJava.hello === "Hello Java")
  }
}

You can now run it with this command:

mvn test

I can just right click on the test case in IntelliJ and run the test. It should be possible in eclipse as well.

like image 126
maba Avatar answered Oct 12 '22 23:10

maba