-
Notifications
You must be signed in to change notification settings - Fork 599
Unit Tests
Normally, code should be tested (explicitly or implicitly) by FAT tests which run test applications that access APIs exposed by configured features. This is partly because unit tests are only run during compile/build phase, and when the full test suites are run, the compile/build phase is generally only run on one or a few machines, rather than all of them (FAT tests run on all).
However, in some cases, it may be useful to write unit tests outside of the FAT infrastructure.
Unit tests are housed within the test
subfolder of a project.
-
Add to
.classpath
:<classpathentry kind="src" output="bin_test" path="test"/>
-
Add to
bnd.bnd
:-testpath: \ ../build.sharedResources/lib/junit/old/junit.jar;version=file
If you want to capture the trace.log and messages*.log in the collected regression cloud tests then you can do this in the Java Junit tearDown method with:
outputMgr.copyTraceStream();
outputMgr.copyMessageStream();
e.g. have a look at this https://github.com/OpenLiberty/open-liberty/pull/25810/files#diff-b95e89cebd843b07c2f84aea4a6b5ce02ee4b732a9ed889d89caf7c865a4f59eR86
You can also send output to the gradle log file with: add to the build.gradle file (or create one) and add:
test {
testLogging {
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
Different ways to run unit tests:
./gradlew com.ibm.ws.whatever:test
./gradlew com.ibm.ws.whatever:build
- In Eclipse, right-click on the
test
directory and select Run As > JUnit Test - If you want a code coverage report to be generated (in addition to running the unit tests), you can do
./gradlew com.ibm.ws.whatever:jacocoTestReport
- the report is output tocom.ibm.ws.whatever/build/libs/reports/jacoco/test/html/jacoco-sessions.html
Individual tests can be run using --tests
followed by the fully qualified class or test name.
./gradlew com.ibm.ws.whatever:test --tests com.ibm.ws.whatever.ImportantTest
./gradlew com.ibm.ws.whatever:test --tests com.ibm.ws.whatever.ImportantTest.testImportantThings
Note: Using --tests seems to cause a lot of additional startup with bnd, so it can take over a minute to initialize before running the tests.
Tests can be debugged by attaching the Eclipse debugger. Start by adding the --debug-jvm
flag to your gradle command. For example:
./gradlew com.ibm.ws.whatever:test --debug-jvm
By default this will pause before running tests, and wait for a debugger to attach on port 5005.
In eclipse, create a Debug Configuration for Remote Java Application
for the project containing the unit tests. Under Connection Properties, set the port to 5005.
When gradle says Listening for transport dt_socket at address: 5005
run the eclipse debug configuration.
If you need to add extra dependencies to your unit test classpath, do so via -testpath
in bnd.bnd instead of -buildpath
. Note that -buildpath
gets inherited into the -testpath
.