I made a Scala/Java mixed project with SBT 0.11.2. My config for JUnit testing is
resolvers += "twitter.com" at "http://maven.twttr.com/"
seq(com.github.retronym.SbtOneJar.oneJarSettings: _*)
libraryDependencies += "com.novocode" % "junit-interface" % "0.10-M2" % "test"
When I write JUnit test cases in Scala with #Test, every goes well. But when I write a Java JUnit test case, then run test in sbt, the Java JUnit test cannot be reconized. Only test cases written in Scala are executed.
How can I make sbt recognize my Java and Scala test cases at the same time?
Probably late for the origional question, but..
I've just been looking at this. The JUnit tests in my project were not running for me until I ran sbt clean test. Now all working like a charm.
There was a bug in 0.11.x in detecting Java tests that was fixed in 0.12.0, although I didn't think it affected detecting annotated tests. You might try coming up with a minimal test case and checking with the latest sbt version (0.12.1). If the problem still exists, file a bug.
you should put your test classes into src/test/java and your class name should end with "Test" (for example myTest.java)
Related
I'm currently experiencing a problem with Specs2 + SBT where my tests always fail via command-line because of dependency order in the classpath. Specs2 requires that the Mockito jars come after the Specs2 jars so that Mockito classes can be overridden to fix issues with by-name scala method parameters (see this issue for more information: https://github.com/etorreborre/specs2/issues/428).
In IntelliJ, I can order my dependencies via the Project Structure/Modules/Dependencies window, which fixes my tests when run inside IntelliJ, however, I have not found a solution to fix this issue when running my tests on the command-line via sbt test.
Does anyone know if it is possible to change the classpath order of dependencies for SBT using settings in build.sbt (or similar)?
To my knowledge you need to make sure that specs2-mock comes before mockito in your libraryDependencies setting.
Does anyone have an example of a cucumber project with sbt 0.13 and Scala 2.11?
Do I need both the cucumber-scala_2.11 and the sbt-cucumber-plugin" % "0.8.0" (is that plugin up to date)?
Where does the plugin go now?
Where do the .feature files go?
Where do the cucumber tests go?
How do I run the tests from sbt?
(optional) How can I run the tests from IntellJ (15)?
Ok, I figured out a solution with the following caveat: I'm not using sbt.
Idea
We will write cucumber features and steps first. Then we will write a "Runner" class, that will be run by JUnit runner (which will be oblivious to the presence of cucumber)
Procedure
Step 1. Depend only on one thing for writing cucumber features and steps!
libraryDependencies += "info.cukes" % "cucumber-scala_2.11" % "1.2.4"
Step 2: Now we depend on junit (for writing the runner of our tests) and cucumber-junit connection library that will allow us to tell JUni to run our cucumber tests:
libraryDependencies += "info.cukes" % "cucumber-junit" % "1.2.4"
libraryDependencies += "junit" % "junit" % "4.12"
Step 3: Write feature and steps definitions (they should be placed in the same folder in tests in my experience):
# My.feature
Feature: blah blah
....
// MySteps.scala
...
Step 4: Write our test runner:
import cucumber.api.junit.Cucumber
import org.junit.runner.RunWith
#RunWith(classOf[Cucumber])
class RunTests extends {
}
Step 5: (optional) Integration with IntelliJ
a) Enable JUnit in IntelliJ. This will allow us to run our cucumber tests by running our junit runner. Beautiful!
File
Settings...
Plugins
Search for "JUnit" and make sure it's enabled
Now we can run our cucumber tests by simply right clicking on the .feature file and selecting Run!
b) Enable Cucumber for Scala IntelliJ plugin. (make sure Scala plugin is enabled). This enables IntelliJ to determine how to associate feature files with scala files. Without this your feature files will always be highlighted in yellow with an error undefined step reference:
Files
Settings...
Plugins
Browse repositories...
search for "Cucumber for Scala"
Install Cucumber for Scala plugin
Restart IntelliJ.
Now your feature files will be highlighted properly!
Useful Resources
Java tutorial for cucumber, that led me to this solution: https://c0deattack.wordpress.com/2012/03/28/cucumber-jvm-with-cucumber-java-cucumber-junit-example/
Cucumber chat room where people are super helpful: https://gitter.im/cucumber/chat
Directory structure (note that in scala we seem to need to place both feature and steps files into the same folder: however the idea of individual runners is interesting): http://www.hascode.com/2014/12/bdd-testing-with-cucumber-java-and-junit/#Directory_Structure
To add to #drozzy 's answer, specifically for sbt:
add junit-interface as a dependency in your sbt project
"com.novocode" % "junit-interface" % "0.11" % Test
Write the test runner as #drozzy shows at step 4.
Make sure you include the connection between JUnit and Cucumber:
#RunWith(classOf[Cucumber])
Once you do that, sbt test will run your JUnit tests, and one of your "JUnit tests" will run your Cucumber tests.
I'm trying to run Specs2 tests from IDEA from both Windows and Mac versions of Intellij IDEA 14. I generated the idea project files using both gen-idea and the built in SBT plugin and get the same results...
When I try to run them, I get
Error running Specs2 in 'scala': Test class not found
I've added packages and switched "search for tests" to in single module but still no avail. I also get the message below on the run config.
Multiple suite traits detected: List(ScTrait: SpecificationStructure, ScTrait: SpecificationStructure,)
The similar post How to run all Specs2 tests under IntelliJ IDEA? doesn't help and it all runs fine from SBT, BTW. I can't run individual tests via the short-cut either :'(
I'm suspecting it's a combination of scalamock and specs2, as if I remove the following from my build.sbt, I can run them again.
"org.scalamock" %% "scalamock-specs2-support" % "3.2" % "test"
Here's the run config.
You should check if you have multiple specs2 jars dependencies in your module. If you just leave one jar dependency the configuration error should disappear.
To help other googlers, it could also be that you have one package depending on specs2-core, and scalamock-specs2-support which depends on specs2. The solution is to add an explicit dependency on specs2, and both dependencies should be evicted to the one you import. I was just able to fix this with the following dependency: "org.specs2" %% "specs2" % "3.7" % "test"
I've been avoiding using SBT since the support in intellij for maven has always been far superior, plus I don't see much advantage in SBT; but I figure why fight the masses.
So one of my open source projects I've converted over to SBT. Now when I run tests (approx 1000 test cases), I get OOMs. Ok so I've tried
fork in Test := true
javaOptions in Test ++= Seq("-Xmx2048m", "-XX:MaxPermSize=512m")
Ok so my OOMs go away but now I get
sbt.ForkMain$Run$RunAborted: java.net.SocketException: Broken pipe
at sbt.ForkMain$Run.write(ForkMain.java:114)
at sbt.ForkMain$Run$1.info(ForkMain.java:132)
Seems to be in different places each time.
These tests all pass if I'm building via maven (scala test maven plugin).
Help me Obi-wan or SBT lovers.
Edit: Adding env details
sbt 0.12.4
java 7.25
scala 2.10.2
Is there a way to build tests with SBT without running them?
My own use case is to run static analysis on the test code by using a scalac plugin. Another possible use case is to run some or all of the test code using a separate runner than the one built into SBT.
Ideally there would be a solution to this problem that applies to any SBT project. For example, Maven has a test-compile command that can be used just to compile the tests without running them. It would be great if SBT had the same thing.
Less ideal, but still very helpful, would be solutions that involve modifying the project's build files.
Just use the Test / compile command.
Test/compile works for compiling your unit tests.
To compile integration tests you can use IntegrationTest/compile.
Another hint to continuously compile on every file change: ~Test/compile
We have a build.sbt file that is used for multiple projects. Doing sbt test:compile compiled the tests for every single project and took over 30 minutes.
I found out I can compile only the tests for a specific project named xyz by doing:
sbt xyz/test:compile
Using sbt version 1.5.0 and higher test:compile returns deprecation warning.
Use Test / compile.
(docs)