We are in the process of upgrading all of our tests to use JUnit 5. Previously we had to set the test.workingDir so that our tests would work the same in Eclipse and Intellij IDEA. Now that I'm using JUnit 5 I'm running into issues with this. I've gone through the JUnit 5 Gradle documentation, but have found no way to set the workingDir for the tests. This causes issues because the tests run differently in the IDEs than they do in the command line.
That Gradle plugin adds a task of type JavaExec called junitPlatformTest. So configure it using junitPlatformTest.workingDir = ....
Besides that, you should maybe rewrite your tests to not depend on the current working dir, but using classpath lookup to get files you need with methods like getResource() and getResourceAsUrl().
Related
I found instructions on how to start a JUnit 4 test case from within Java, but have been unable to put together constructs that will fire up tests on a JUnit 5 test case.
The JUnit 4 solution I tried was this: How do I run JUnit tests from inside my java application?
I've been trying to get the ConsoleLauncher in JUnit5 to work, but it is throwing exceptions. That was documented here: http://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher
I'd like to automate starting tests to match running a program to simplify the instructions I'm providing when I provide unit tests to students.
Thanks!
If the goal is to simplify a task for students, I think it would be good to use a build tool like Maven or Gradle. This makes it simple to run tests, build their code, etc. JUnit5 tests can be run by using plugins - http://junit.org/junit5/docs/current/user-guide/#running-tests-build.
Test cases are written using JMockIt to mock complex behavior. Mocking is working as expected when test cases are executed using eclipse.
But when test cases are executed using ANT, mocking is not working as expected since mocking mocking method throws Exception.
In Ant build script launchJunit task is used instead of junit task, to execute the Junit test cases.
As per analysis, root cause seems to be build script launches JRE to execute test cases and JRE does not have tools.jar file. I added tools.jar file in my application classpath but still problem is not solved.
Java home point to correct JDK but as per ant diagnostic tool java home is %JAVA_HOME%/jre.
Property env.JAVA_HOME = %JAVA_HOME% correct JDK
Property java.home = %JAVA_HOME%/jre
I am getting most of the hint to solve problem for junit task but not came across any solution of launchJunit task.
Is my understanding of the problem is correct and how to resolve issue?
I also meet your case.When i run jmockit in junit eclipse plugins, it works. But switch to the ant task, it halts in junit task, no error, either no info. So i check all the classpath and environment variable in the ant xml and eclipse junit lancher. When i make them same config, it turns to be ok! I think you can try my way, just check the classpath and variable in the two cases.
I have configured unit testing for AndroidStudio as described on the Android documentation (http://tools.android.com/tech-docs/unit-testing-support).
I would like to run every test method in its own JVM, so all static properties in my project are set back to their default values.
I couldn't find anything about this in the documentation and I'm afraid this is not possible yet.
I'm running my tests from the command line (gradlew --daemon test) as I didn't get the testrunner in Android Studio to work.
Does anyone know how to fork every testmethod in its own jvm process, so they run 'standalone'? Please let me know if this is possible or if there are alternative ways to run every testmethod in it's own process using gradle.
Thats easy. In your test task set
forkEvery 1
That will cause a new jvm to be forked for every single test.
I have a multiproject gradle build which contains a mix of Java with JUnit and Scala with Specs tests, plus a few other technologies.
I have pretty much everything building fine but I am hitting an issue with the tests.
My projects are all organised with the following structure
<project>/src/main/... - containing the production source code
<project>/src/tests-unit/... - containing the JUnit or Specs test code
and then optionally a combination of
<project>/src/tests-util/... - containing utility classes for setting up tests
<project>/src/tests-functional/... - containing functional level test code
<project>/src/tests-integration/... - containing integration level test code
My problem is that running grade tests on this project will actually run the tests in all the test directories and I want to be able to control what tests are being run. I had the idea of setting up a separate task for each level of test, but how do I specify to just run tests in the /src/tests-unit hierarchy and ignore the others?
I have tried several things including setting the runtimeClasspath of the sourceset for a target but I just get a deprecation warning for this and it has no effect.
Many thanks
You'd typically declare separate source sets and separate Test tasks. See samples/java/withIntegrationTests in the full Gradle distribution for details.
I have a large suite of tests that takes about half an hour to run and would love to be able to the test classes in parallel.
Is there a way to do that with IntelliJ IDEA 9?
IDEA will understand parallel JUnit tests only since version 10.
There is a tracker issue which you can vote for and watch for progress:
http://youtrack.jetbrains.net/issue/IDEA-47103
We plan to add it in IDEA 10, but the priority would depend on the number of votes.
Answering late for posterity.
You can make JUnit tests run in parallel (or serial) to any level of granularity in IntelliJ by changing the Fork mode in the test's run configuration.
Be careful not to confuse this with the Allow parallel run option, which lets you start the test execution multiple times within your IDE.
UPDATE in 2022: This plugin lets you run and debug tests in parallel in IntelliJ IDEA with JUnit4, JUnit5 and TestNG: https://plugins.jetbrains.com/plugin/16229-unit-test-parallel-runner
If you are using JUnit4, you can give this plugin a try:
https://plugins.jetbrains.com/plugin/12959-junit4-parallel-runner
Running tests in parallel is as easy as clicking on the green triangle next to a
class -> all the test methods in the class are run in parallel
package -> all the classes in the package are run in parallel