Course Content
Java JUnit Library. Types of Testing
Java JUnit Library. Types of Testing
Set-Up
Importing Maven Dependency
To write unit tests, we use the JUnit framework. Since it's a framework, we can't simply use it as is. We need to import JUnit, just like any other library.
To do this, you need to open IntelliJ IDEA and find the pom.xml
file, where all the settings and libraries for your project are located. This file should look something like this:
Once you've found this file, you need to import the JUnit. To do this, you can either follow the link or simply search in your browser for "JUnit maven dependency ". You will be given the first link from which you should copy this text:
example
<!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
Note
Instead of copying only the code provided above, it's better to follow the link and copy the current and up-to-date version of the JUnit framework. Using an outdated version may cause some annotations or operations from this framework to break.
Next, you need to specify the dependencies section and paste the copied text into it. After that, you should update Maven by clicking on the corresponding button as shown in the video:
Testing
Great, now the framework is imported into our project, and we are ready to use it. To ensure that the JUnit is working correctly, let's write a simple method and then test it:
Now, I will write and run several unit tests to check if the framework is working. You can copypaste this code into the test/java
directory and run it locally to verify if you have installed everything correctly:
Great, if you have achieved the same result, you have successfully added the JUnit framework dependency to your project. Next, you will learn how to write such unit tests for your methods and be able to test your assignments and cover your programs with tests.
Thanks for your feedback!