Tutorial Assertj in Netbeans 11 with maven

Why we need [Unit] Testing or why they make our life easier ? There are many answer about this question.

  • It allows you to check the expected behavior of the piece(s) of code you are testing, serving as a contract that it must satisfy.
  • It also allows you to safely re-factor code without breaking the functionality (contract) of it.
  • It allows you to make sure that bug fixes stay fixed by implementing a Unit test after correcting a bug.
  • It may serve as as a way to write decoupled code (if you have testing in mind while writing your code). (SO — Unit testing in Java — what is it ?)

based on the above reasons, [Unit] Testing is very important. There are several unit tests namely Black box Testing and White box Testing. WHITE BOX TESTING is testing of a software solution’s internal structure, design, and coding. In this type of testing, the code is visible to the tester. BLACK BOX TESTING is defined as a testing technique in which functionality of the Application Under Test (AUT) is tested without looking at the internal code structure, implementation details and knowledge of internal paths of the software.

Illustration Black Box Testing

One of the Black Box Testing implementations in Java Swing with AssertJ Swing. AssertJ Swing is a Java library that provides a fluent interface for functional Swing UI testing. AssertJ Swing is based on JDK standard types assertions and can be used with either JUnit or TestNG. In this tutorial, we implement AssertJ Swing using Netbeans 11. Actually you can use your favorite IDE

Step 0: Prepration

using maven add dependency to your pom.xml

<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-swing-junit</artifactId>
<version>3.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout</artifactId>
<version>3.7.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.13.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-swing-aut</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

Step 1: Create Project and Add Library

Make a project on Netbeans with name TestSwing. Using File > New Project >Java with Maven > Java Application > Next > Project Name : TestSwingMaven > Create package with name com.ttm.testswingmaven > Finish. Add 2 files SimpleCopyApplication.java to Source Package and SimpleCopyApplicationTest.java to Test Package and add maven above.

Tree Explorer of TestSwingMaven

Step 2: Write Code

Add this code to SimpleCopyApplication.java and try to running it.

Code of testing Swing

then GUI will appear like this one

GUI SimpleCopyApplication

Step 3: Test GUI

Add this code into SimpleCopyApplicationTest.java

after add the code and test it

Conclusion

This article is based on several articles :

  1. https://joel-costigliola.github.io/assertj/assertj-swing-getting-started.html
  2. https://stackoverflow.com/questions/46676726/how-to-get-started-testing-java-swing-gui-with-assertj-swing
  3. https://stackoverflow.com/questions/91179/automated-tests-for-java-swing-guis

Python Backend Engineer: Specializing in Django and Java GUI Developer