junit
testing framework into the application; the gradle
build tool is used while writing the article.Installation
I'm using gradle
version 8.8
to initialize a new project. It by default comes with a junit
testing framework, but to keep everyone on the same route, I'll assume that everyone is working on an ongoing project and will proceed to start from the installation process, but for everything to work correctly the minimum required version of gradle
is 4.6
.
Add the necessary junit
dependencies in the app/build.gradle
file:
dependencies {
...
testImplementation 'org.junit.jupiter:junit-jupiter:5.11.0'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.11.0'
...
}
After adding the dependencies, we'll need to create a task named test
which will be set to use the junit
version 5
testing framework, add the following code at the end in the same file:
tasks.named('test') {
useJUnitPlatform()
}
Once done, the application will be able to run tests, but currently, it will not log the test output in the console, to add that functionality, we can append the following code in the tasks block:
...
testLogging {
events "started", "passed", "skipped", "failed"
}
Based on the desired output we can remove the unnecessary events (events whose logs do not need to be output), common events are normally passed
and failed
in most cases but the rest might be useful depending on the situation.
Writing and running an test
Assuming the project's structure is similar to default and it includes these two files:
app/src/main/java/org/example/App.java
app/src/test/java/org/example/AppTest.java
If the second file is not present, please create it, as we'll be working with these two files.
In the app/src/main/java/org/example/App.java
file add the following code:
...
public class App {
public String heading() {
return "...";
}
}
And in the app/src/test/java/org/example/AppTest.java
file add this code:
...
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class AppTest {
// annotation (required to label this method as test)
@Test
void headingTest() {
App testClass = new App();
String expectedResult = "Java does not seems to be going anywhere";
assertEquals(expectedResult, testClass.heading());
}
}
Open the terminal, change the active directory to the project's folder, and execute the following command to run the test:
./gradlew clean test
That's it, we've successfully written our first test, which is failing as per the displayed output, so let's adjust our test to make it pass in the next section.
Test report and correction
Note, that when the test is run there is a report generated by gradle
that can be accessed from app/build/reports/tests/test/index.html
to view in a browser. If we navigate to the test we've created, a useful error message is displayed in the first line on why the test is failing:
The assertEquals
normally take two arguments, the first is the expected value, and the second is the received value, because the received result is different than the expected one, the test is failing. Let's update the code in the app/src/main/java/org/example/App.java
file as following:
public String heading() {
return "Java does not seems to be going anywhere";
}
Execute the test command again and the output will be that the test is successfully passed.
Conclusion
This was a minimal article on how to implement and use the framework. The junit
framework provides much more functionalities, if anyone is interested in exploring more, please check the framework's official document site.