How do I run test groups with Scala + JUnit runner + Maven? - scala

I'm trying to support running tags/groups of tests on the Maven commandline. I think I have the correct syntax setup in my .scala file (see below), but I'm unsure how to actually execute on the commandline. Examples on the web are mostly in Java.
I want to be able to do something like mvn clean test -Dtags:SlowTest,DbTest to run all the tests tagged as "slow" and "db".
I've seen Surefire pom.xml examples out there, but I'm not sure how I'd then tag the Surefire groups in my test code itself.
Ideas?
import org.junit.experimental.categories.Category
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.Tag
class SlowTest extends Tag("com.mycompany.tags.SlowTest")
class DbTest extends Tag("com.mycompany.tags.DbTest")
#RunWith(classOf[JUnitRunner])
#Category(Array(classOf[SlowTest], classOf[DbTest]))
class FunSuiteTest extends FunSuite with DemoHelpers {
test("DemoTest1") {

If you are using the scalatest-maven-plugin you should be able to do something like this:
mvn -DtagsToInclude=SlowTest,DbTest clean test

I might suggest that if you want to do some test in Scala, why dont you try using
SCALA TEST
it is good and easy to use.

Related

IntelliJ cannot find Scala integration test framework

I have configured a Scala project for integration tests as described in https://www.scala-sbt.org/1.x/docs/Testing.html.
I can get the integration tests executed from sbt, but not from IntelliJ. IntelliJ seems to not recognize that my test class is a test class. The same test class works fine under test, but does not seem recognized as a test when it is under it.
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class BlackSpec extends AnyFlatSpec with Matchers {
behavior of "Black.adder"
it should "return 42 for any inputs" in {
val six = Black.adder(1, 2)
six shouldBe 42
}
}
When putting the above test under it and executing it from IntelliJ: "Passed: Total 0, Failed 0, Errors 0, Passed 0".
This problem happens in a large (proprietary) project. A minimal example project works fine everywhere, including in IntelliJ: https://gist.github.com/radumanolescu/601b0c743c73b42396b4f6ca9d5fc1db
In the large project, we probably have a setting that disturbs the recognition that my class is an integration test and should be run with ScalaTest. Any idea what that setting might be?

Type mismatch junit5 #ExtendWith

so this is probably a really dumb question but I just started to migrate a project from junit 4 to 5 and saw that #RunWith() does not longer exists. It is replaced by #ExtendWith. So I tried to do it like this:
import org.jboss.arquillian.junit.Arquillian;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
import de.msggillardon.services.journal.JournalService;
import de.msggillardon.system.UserContext;
import de.msggillardon.util.ITDeployment;
#ExtendWith(Arquillian.class)
.....
And I get the following exception: "Type mismatch: cannot convert from Class to Class <? extends Extension>
I am a really bloody beginner and have no Idea how I can fix the problem.. So maybe someone could help me or tell me where I can find the necessary Information.
Thank you all.
Unfortunately, you can't use JUnit 4's Runner as your JUnit Jupiter (part of JUnit 5) extensions. That's a completely new API and not compatible.
Although the JUnit Jupiter programming model and extension model will
not support JUnit 4 features such as Rules and Runners natively, it is
not expected that source code maintainers will need to update all of
their existing tests, test extensions, and custom build test
infrastructure to migrate to JUnit Jupiter. From the official JUnit documentation
So for each #RunWith, you need to use/include the JUnit Jupiter extension. In your particular case of Arquilian, I'm not quite sure if there is already an official extension for this.
The following links might help:
https://developer.jboss.org/thread/274569
https://github.com/arquillian/arquillian-core/issues/137
Arquillian now support junit 5.
Replace
#RunWith(Arquallian.class)
with
#ExtendWith(ArquillianExtension.class)
Also add the new dependency
<dependency>
<groupId>org.jboss.arquillian.junit5</groupId>
<artifactId>arquillian-junit5-container</artifactId>
<version>1.7.0.Alpha10</version>
</dependency>

How do I run a JUnit class annotated with #RunWith on Eclipse?

Usually, I would run my test classes by right clicking the class -> Run As -> JUnit Test. But I get errors if the class is annotated with #RunWith
For example, for a test class with the following structure:
import org.junit.Test;
import org.junit.runner.RunWith;
import mockit.integration.junit4.JMockit;
#RunWith(JMockit.class)
public class SwiftResourceIT {
}
I get the following error:
Caused by: java.lang.IllegalStateException: Running on JDK 9 requires -javaagent:<proper path>/jmockit-1.n.jar or -Djdk.attach.allowAttachSelf
As a Maven project that is configured to run tests when I do a build, the tests run fine when I clean install. I just don't know how to run this class by itself.
Thanks in advance
This is something you can configure in the "Run Configuration". Right after failing to run the test class, bring up the "Run Configurations" dialog (multiple ways to get there), the one you just ran should be selected. Click on the "Arguments" tab, put the required "javaagent" command-line arguments into that field and store them.
If you don't like having to edit this for every test class, you might consider changing your mocking technology. Mockito has no need for something like this.

Importing anorm.SQL in IntelliJ IDEA 13

IntelliJ 13 Community Edition
Play Framework 2.2
Scala 2.10.2
I am importing anorm._ and using SQL in my object. The object starts off as follows:
package controllers
import play.api.mvc._
import play.api.db.DB
import play.api.Play.current
import anorm._
object Walks extends Controller {
val futureWalksSql = SQL("SELECT * FROM walks where evt_date > now()")
IntelliJ cannot resolve symbol SQL. If I ctrl+Enter, after anorm. there is no SQL option, although there is a .Sql trait, object and class.
When I run the play project, it all works just fine, with no compilation errors so this Scala is syntactically correct but IntelliJ isn't picking this up. I have created the idea files by calling idea from within the play console and I've also tried idea with-sources=yes.
How do I get IntelliJ Community Editon to pick up anorm.SQL? What is so special about this object? I'm still learning Scala and so this might be a Scala issue.
SQL is a method defined in a package object anorm. So when you import anorm._ you import the entire package with the package object as well. I have actually no clue why Idea does not pick this up. But if you look into the package object sources you can see that the SQL method is just a wrapper on anorm.Sql.sql(inSql: String).
As a workaround you may try to import anorm.Sql._ and use sql("select 1") instead of SQL("select 1")

Run tests in broken project using SBT

When doing a serious refactor in a Java Eclipse project I will often break the build, but focus on getting one test to pass at a time. When running the tests Eclipse warns that the project cannot be compiled, but it will still run the tests it can compile.
Now I'm using SBT and would like to achieve the same thing with 'test-only', but it tries to compile the whole project, fails, and doesn't run the tests. How can I tell it to just compile the bits it can and run the tests.
You should add the following task to your project definition:
import sbt._
class Project(info: ProjectInfo) extends DefaultProject(info) {
lazy val justTest = testTask(testFrameworks, testClasspath, testCompileConditional.analysis, testOptions)
}
This is the same as the ordinary test task, but has no dependencies attached at the end. If you'd like it to have dependencies, call dependsOn on the testTask(...) expression and provide the tasks you want it to depend on.
testTask(testFrameworks, testClasspath, testCompileConditional.analysis, testOptions).dependsOn(testCompile, copyResources, copyTestResources)