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 ?
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.xmlthat just uses the scala compiler for the tests and uses the normal java compiler for the main java code.pom.xmlNow here’s a simple Java class that I want to test:
src/main/java/com/stackoverflow/Hello.javasrc/main/java/com/stackoverflow/HelloJava.javaAnd finally the ScalaTest test class.
src/test/scala/com/stackoverflow/HelloJavaTest.scalaYou can now run it with this command:
I can just right click on the test case in IntelliJ and run the test. It should be possible in eclipse as well.