How JUnit starts - java

I'm figuring out how JUnit works. I can't understand how it runs itself, I understand that the surefire plugin interacts with JUnit, but I don't understand how JUnit itself runs. I'm trying to understand the debug output, and came across the getRunner method, but I can't get down to it, I can't understand what kind of package com.intellij.rt.junit is. Can you help me.

com.intellij.rt.junit is a package of IntelliJ for its built-in runner for JUnit tests, just like Surefire is the runner when tests are run from Maven.
JUnit itself also contains a basic commandline runner. For JUnit 5, see Console Launcher, for JUnit 4, see Test runners. These are probably - but I didn't check it - easier to understand than the internals of the runners of Surefire or IntelliJ.

Related

Java - Maven Test/Unit Test passed when running the whole package but failed when running the test individually

I just received a project from my collage and when I run the whole project with Maven test/ Maven install by right click to the whole project and choose Maven test or Maven install, it runs successfully. However if I run an individual test, this test failed. I do not really understand here why it happened. Can anyone give me a hint please? Thank you!
It is possible that another unit test is establishing some state that is affecting the unit test that is failing. It is best practice to avoid writing unit tests that depend on shared state, as there is usually no guarantee as to what order the tests run in. See this article for a good explanation of unit test best practices.

Tell Eclipse to skip tests

Is there a way to tell only Eclipse to skip various individual junit tests (but still run the rest of them)? I am familiar with the #Ignore annotation, but that is not what I am looking for because that will cause the tests to always be skipped.
I would like the tests to be skipped when ran by eclipse (Run As -> Junit Test) but ran in any other context, most likely during a maven build.
Any ideas?
So far the following solution has worked well. It is not a complete solution because it causes the tests to only be ran by maven when maven is invoked from the command line. For now though, it works.
When maven is invoked, the maven command line arguments are placed in the environment map (System.getenv()). When the tests are run through jUnit in eclipse, there is no entry in System.getenv() for the maven command line arguments. So, I am using jUnits Assume class to check that this property is not null for the tests that I want eclipse to skip.
Code:
Assume.assumeNotNull(System.getenv("MAVEN_CMD_LINE_ARGS"));
FYI, one downside of this solution is that eclipse marks the tests as passed instead of skipped.

How can I view integration tests in the Netbeans Test Results?

My development environment is Netbeans 7.4 and Maven 3.0.5 (bundled with Netbeans). I'm using JUnit 4.11 to build unit (class names ending with Test) and integration tests (class names ending with IT). I'm running unit tests and excluding integration tests with the "-DskipITs" Maven option. I have a custom action which runs just the integration tests with the failsafe plugin. Both execute successfully. However, I only see the results in the "Test Results" window when running the unit tests. How can I get the integration tests to show in the "Test Results" window? With the integration tests, I'm only seeing the output in the console.
The maven-failsafe-plugin only executes in integration-test and verify (and of course the help) goal while the maven-surefire-plugin runs during test goal.
NetBeans Test Results window only shows the tests that were executed using the 'test' goal.
My solution for this situation is to categorize my integration tests into
testintegration - just lightweight ITs, I run them with surefire (to see them in Test Results window)
testheavy - those that will require me to bootstrap something I run with the fail-safe plugin
I hope you have the option of doing something close to that.

Within Eclipse, Debug configurations for different groups in TestNG

How do we setup Eclipse, TestNG (and possibly TestNG-Eclipse plugin?) so that our devs can run two different test configurations conveniently? We have hundreds of unit tests but only 20 integration tests, so we would prefer to a minimum number of changes to the unit tests if possible.
We have added attributes to our unit tests:
// unit tests
#Test
// integration tests
#Test(groups = { "IntegrationTest" })
The holy grail would be that we could right-click our package and select either:
debug configuration which only runs unit tests
debug configuration which the slow running integration tests (and possibly the unit tests).
It seems like we could add these configurations to the "Favorites" list. It is an acceptable compromise.
Finally, the last requirement is that the unit tests will also run on the build server (maven surefire plugin compatible). This doesn't seem to be a challenge so I'm not too concerned about it.
Also, if there is another framework I should investigate that supports these scenarios, I would also like to hear about that as well. Right now we are using junit and are exploring TestNG.
When you create a TestNG launch configuration, you can specify various things such as which groups to run.

Run all the junit tests that are in a given package in Netbeans?

We have a bunch junit tests in our current project. Essentially we are looking for a way to run all the test in a given package. Currently in Netbeans I see you can run all the tests or a single test, but no way to run a a sub-set of tests.
Is this built into Netbeans? Or is there another way we can do this?
In JUnit this is achieved through TestSuite. You can check for more information here (look at the code, not at the pictures).
At least in the Eclipse IDE there is a functionality that lets you add a new TestSuite, select which tests it is to include and then have the IDE generate it for you.
I haven't seen such thing in Netbeans, but you should check for any additional JUnit plugins.
Just set up junit test groups like documented e.g. on this question and then run the test group via your ant or maven build.

Categories

Resources