How to create a Java lab with JUnit?
Attach to course item
button. Once that is done, click the three dots again and click on “Edit” to edit the lab.Edit
button a new page will open. On this page you need to setup instructions for lab. These instructions would be visible to the user when they’re attempting the lab. Therefore, include all the helper material, lab setup instructions here.
.cdmrc
file, a critical configuration file for Fermion labs. Familiarize yourself with the .cdmrc guide to understand how it works before proceeding.
$UNIT_TEST_OUTPUT_FILE
for the UI to show the results.
/home/damner/code
directory and running javac -cp . *.java
. This would compile all files. If you want to compile your user code in a different way, you can do it here.TestFile.java
(the JUnit code that we wrote in the full JUnit testing script above) with junit5main.jar
being in the classpath.TestFile
class using --select-class TestFile
(remember, this is the same class name of the JUnit test script as we wrote above in Test File section).--reports-dir
flag outputs the result in the same directory which then we read with a simple Node.js script.xml2js
parser to convert this into a JS object, and then convert the results into a boolean JSON array. We write this array inside $UNIT_TEST_OUTPUT_FILE
which is then read by the playground and challenges are marked as passed or failed.Edit Test File
in the Evaluation
tab. Click on it.
When you click on it, a new window will open. This is a test file area.
You can write anything here. Whatever script you write here, can be executed from the Test command to run section
inside the evaluation tab we were in earlier.
The point of having a file like this to provide you with a place where you can write your evaluation script.
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
and @Order(1)
decorators. This is important if you want to map the results of these tests (passed/failed) correctly with the tests you added in challenges tab earlier.ByteArrayOutputStream
.
In your test() method, you can use the out.toString()
method from the out
object declared at the class level to read the output from the terminal and finally do a assertEquals()
to compare the output.
Sample Code to check Hello World
program: