Maven not discovering ScalaTest unit tests - scala

I have the following unit tests:
import org.scalatest.FunSpec
import org.scalatest.Matchers._
class MyClassSpec extends FunSpec {
describe("MyClass"){
describe("Scenario 1"){
it("Condition 1") {
true shouldEqual false
}
it("Condition 2"){
true shouldEqual false
}
}
}
}
When I run maven test, this compiles fine but the tests are not found. Here is the output:
[INFO] --- maven-surefire-plugin:2.7:test (default-test) # project-name ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- scalatest-maven-plugin:1.0:test (test) # project-name ---
Discovery starting.
Discovery completed in 202 milliseconds.
Run starting. Expected test count is: 0
DiscoverySuite:
Run completed in 236 milliseconds.
Total number of tests run: 0
Suites: completed 1, aborted 0
Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
No tests were executed.
As the output shows, I'm using the scalatest plugin for maven. Here is the relevant section of my pom.xml:
<!-- disable surefire -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- enable scalatest -->
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/scalatest-reports</reportsDirectory>
<junitxml>junit</junitxml>
<filereports>report.txt</filereports>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
I'm pretty new to getting this stuff set up, so I'm not sure if there is some other thing I'm not checking. I get the exact same results if I run maven clean and then maven test. I'm using ScalaTest version 3.0.1. I have another project with tests running successfully also using 3.0.1 (I've copied everything I can find between the two projects that seems even remotely related).

You have placed <skipTest> true </skipTest> which is used for skipping the test cases. So basically you have disabled the surefire and it is used for:
The Surefire Plugin is used during the test phase of the build
lifecycle to execute the unit tests of an application. It generates
reports in two different file formats Plain text files (.txt) XML
files (.xml)
Update your pom with:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,...-->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
<include>**/*Spec.*</include>
</includes>
</configuration>
</plugin>
In include you have to provide the extension of your test file and you can change the version of pluggin.
Thanks

Naturally, the problem seems to have stemmed from some information I didn't think was relevant, so I didn't include it my original post.
The source code I wrote was a tool for unit testing, so I put it in the namespace my.cool.package.testUtilities. The unit tests were thus in my.cool.package.testUtilities.test. Moving the source code out of testUtilities (to just my.cool.package) and moving the tests to just my.cool.package.test allowed the tests to be discovered.
Being new to the java ecosystem (previous experience in .NET), I'm not sure if this is some weird bug with the test runner, expected behavior, or if the act of moving the files to new namespaces did something else, and the namespaces themselves are irrelevant. So I'm not sure what I learned from this.

Related

ScalaTest Maven Plugin Does not Detect Scala Test Suite

I have the following configured in the parent pom of my multi module Maven project.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<skipTests>false</skipTests>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,...-->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
<include>**/*Spec.*</include>
</includes>
</configuration>
</plugin>
<!-- enable scalatest -->
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDFTestSuite.txt</filereports>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
Nothing fancy here as it is just straight out the same configuration settings that could be found under the Scalatest documentation. But strangely, running the following does not detect any of my scala tests:
mvn clean install
[INFO]
[INFO] --- scalatest-maven-plugin:1.0:test (test) # My-Project ---
Discovery starting.
Discovery completed in 103 milliseconds.
Run starting. Expected test count is: 0
DiscoverySuite:
Run completed in 160 milliseconds.
Total number of tests run: 0
Suites: completed 1, aborted 0
Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
No tests were executed.
Is there anything that I'm missing in the form of any configuration?
EDIT:
Here is how my project is structured:
parent-project
pom.xml
src
main
scala
java
child-project
pom.xml
src
main
test
scala
java

Configure NetBeans to execute single unit tests with JMockit as javaagent

When using JMockit with Maven for unit tests, it is required to pass the location of jmockit.jar to the VM by setting the -javaagent parameter. The maven-dependency-plugin can do this automatically, I have set up a configuration that does the expected like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${dependency.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
This works when the test suite and also single tests are called from command line, e.g. by
mvn test -Dtest=MyClass#someTest
From within NetBeans it is also possible to run the whole test suite (e.g. when "Clean and Build" is executed). But when a single file is tested, the path is not injected. There is a command like the following in the log when the VM crashes:
Command was /bin/sh -c cd /home/kap && /usr/lib/jvm/adoptopenjdk-8-hotspot-amd64/jre/bin/java '-javaagent:${org.jmockit:jmockit:jar}' -jar ...
i.e. the placeholder is not filled with the correct location. In contrast, a call on the command line produces
[DEBUG] Forking command line: /bin/sh -c cd /home/kap/ && /usr/lib/jvm/adoptopenjdk-8-hotspot-amd64/jre/bin/java -javaagent:/home/kap/.m2/repository/org/jmockit/jmockit/1.49/jmockit-1.49.jar
It is especially weird that it works with the whole test suite, but not for single tests.
Why does the single test fail?
When executing clean and build NetBeans by default executes mvn clean install. So the maven executes the goal org.apache.maven.plugins:maven-dependency-plugin:properties during its normal build lifecycle and the plugin creates variable ${org.jmockit:jmockit:jar}.
When executing single test file (i.e. Project -> TestFile -> RightClick -> Test File (or Ctl+F6)) NetBeans executes only single goal mvn -Dtest=MyClass#someTest surefire:test. So the maven-dependency-plugin:properties doesn't execute at all and maven could not find the variable ${org.jmockit:jmockit:jar} because it was not been created.
How to solve it?
Option 1.
Go to Project -> Properties -> Actions and for actions Test File set options to execute goal as follows:
test-compile org.apache.maven.plugins:maven-dependency-plugin:properties surefire:test
NetBeans creates the nbactions.xml file so the solution would work only when executing a single test from NetBeans.
Option 2.
Remove maven-dependency-plugin from you pom.xm. Instead specify the location to jmockit.jar using ${settings.localRepository} property:
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jmockit.version>1.43</jmockit.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar</argLine>
</configuration>
</plugin>
</plugins>
</build>
...
Option 3.
I would assume binding the properties goal to the test-compile phase of maven but it would only work if disabling the Compile On Save feature of NetBeans.
...
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>test-compile</phase>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
...

Scalatest Maven Plugin "no tests were executed"

I'm trying to use scalatest and spark-testing-base on Maven for integration testing Spark. The Spark job reads in a CSV file, validates the results, and inserts the data into a database. I'm trying to test the validation by putting in files of known format and seeing if and how they fail. This particular test just makes sure the validation passes. Unfortunately, scalatest can't find my tests.
Relevant pom plugins:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- enable scalatest -->
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<wildcardSuites>com.cainc.data.etl.schema.proficiency</wildcardSuites>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
And here's the test class:
class ProficiencySchemaITest extends FlatSpec with Matchers with SharedSparkContext with BeforeAndAfter {
private var schemaStrategy: SchemaStrategy = _
private var dataReader: DataFrameReader = _
before {
val sqlContext = new SQLContext(sc)
import sqlContext._
import sqlContext.implicits._
val dataInReader = sqlContext.read.format("com.databricks.spark.csv")
.option("header", "true")
.option("nullValue", "")
schemaStrategy = SchemaStrategyChooser("dim_state_test_proficiency")
dataReader = schemaStrategy.applySchema(dataInReader)
}
"Proficiency Validation" should "pass with the CSV file proficiency-valid.csv" in {
val dataIn = dataReader.load("src/test/resources/proficiency-valid.csv")
val valid: Try[DataFrame] = Try(schemaStrategy.validateCsv(dataIn))
valid match {
case Success(v) => ()
case Failure(e) => fail("Validation failed on what should have been a clean file: ", e)
}
}
}
When I run mvn test, it can't find any tests and outputs this message:
[INFO] --- scalatest-maven-plugin:1.0:test (test) # load-csv-into-db ---
[36mDiscovery starting.[0m
[36mDiscovery completed in 54 milliseconds.[0m
[36mRun starting. Expected test count is: 0[0m
[32mDiscoverySuite:[0m
[36mRun completed in 133 milliseconds.[0m
[36mTotal number of tests run: 0[0m
[36mSuites: completed 1, aborted 0[0m
[36mTests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0[0m
[33mNo tests were executed.[0m
UPDATE
By using:
<suites>com.cainc.data.etl.schema.proficiency.ProficiencySchemaITest</suites>
Instead of:
<wildcardSuites>com.cainc.data.etl.schema.proficiency</wildcardSuites>
I can get that one Test to run. Obviously, this is not ideal. It's possible wildcardSuites is broken; I'm going to open a ticket on GitHub and see what happens.
This is probably because there are some space characters in the project path.
Remove space in project path and the tests can be discovered successfully.
Hope this help.
Try excluding junit as a transitive dependency. Works for me. Example below, but note the Scala and Spark versions are specific to my environment.
<dependency>
<groupId>com.holdenkarau</groupId>
<artifactId>spark-testing-base_2.10</artifactId>
<version>1.5.0_0.6.0</version>
<scope>test</scope>
<exclusions>
<!-- junit is not compatible with scalatest -->
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusion>
</dependency>
With me, it's because I wasn't using the following plugin:
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.8</arg>
</args>
</configuration>
</plugin>
The issue I had with tests not getting discovered came down to the fact that the tests are discovered from the class files, so to make the tests get discovered I need to add <goal>testCompile</goal> to scala-maven-plugin goals.
In my case it's because of the nesting of tests inside the test directory and using the <memberOnlySuites> configuration. <memberonlySuites> only looks out for the test files in the give package / directory. Instead use <wildcardSuites> which will look into a package / directory and all it's subdirectories.
This happens quiet often when you are adding more tests to your test suite and organising them in a more structured manner.
Cause: Maven plugins does not compile your test code whenever you run mvn commands.
Work around:
Run scala tests using your IDE which will compile the test code and saves it in target directory. And when next time you run mvn test or any maven command which internally triggers maven's test cycle it should run the scala tests

Errors when using maven plugin for Weblogic deployment

I have been trying to deploy application to Weblogic 10.3.6 using maven
I have created weblogic plugin for maven as mentioned in this article.
I have added the following to pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>2.3</version>
<configuration>
<goalPrefix>weblogic</goalPrefix>
</configuration>
</plugin>
<plugin>
<groupId>weblogic</groupId>
<artifactId>weblogic-maven-plugin</artifactId>
<version>10.3.6.0</version>
<configuration>
<adminurl>t3://localdomain:7001</adminurl>
<user>weblogic</user>
<password>password</password>
<name>wldemo</name>
<remote>true</remote>
<upload>true</upload>
<targets>AdminServer</targets>
</configuration>
<executions>
<execution>
<id>deploy</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<source>target/EmployeesApp-1.0-SNAPSHOT.war</source>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I did mvn com.oracle.weblogic:weblogic-maven-plugin:deploy I am getting the following errors, how can I resolve these errors?
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Final Memory: 3M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.oracle.weblogic:weblogic-maven-plugin:10.3.6.
0:deploy (default-cli) on project EmployeesApp: The parameters 'source' for goal
com.oracle.weblogic:weblogic-maven-plugin:10.3.6.0:deploy are missing or invali
d
You have specified the source parameter in the execution configuration, so in order to make it taken into account you should invoke this particular execution. It can be done using the phase key you specified, so e.g.:
mvn integration-test
Maven will go through the whole lifecycle and on the pre-integration-test test phase (which precedes the integration-test one) it will run the execution of weblogic-maven-plugin you configured.

Eclipse and Junit, skipover some testcase with some pattern

000 unit test in one application. I know in one particular folder, all the tests takes long time, I donot want to test these cases everytime I run unit test. So may I have some property file to indiciate a pattern/folder which test cases shall skip?
I am using mvn. Java 1.6.
Take a look at Maven Surefire Plugin: Inclusions and Exclusions of Tests.
To exclude certain tests you can use:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
Alternatively, on the command line, you can explicitly specify which tests you want to run. For example:
mvn -Dtest=TestSquare,TestCi*le test