Step 1: Create a Java Project in IntelliJ with Maven
Open IntelliJ IDE and click on New Project button, or go to the menu option File -> New -> Project...

Make sure to select the Build System as Maven.

Step 2: Adding Junit 4 Maven Dependency to pom.xml
In the project folder, open the pom.xml file.
Add the below junit 4 dependency to your pom.xml file.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Make sure to "Load Maven Changes" so the required jar files get downloaded.

Step 3: Sample code to verify setup
Create a class TestJunit.java under src -> test -> java
import org.junit.Test;
import static org.junit.Assert.*;
public class TestJunit {
@Test
public void test1() {
int expectedResult = 20;
int actualResult = 20; //for testing
assertEquals(expectedResult,actualResult);
}
@Test
public void test2() {
int expectedResult = 20;
int actualResult = 19; //for testing
assertEquals(expectedResult,actualResult);
}
}
Now right-click on this class file and select - "Run TestJunit with Coverage", you should see one test pass and another fails.
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- 21 Essential Mac Terminal Shortcuts for Devs and DevOps to Boost Productivity - MacOS
- error CAML Query containing special characters - SharePoint
- Jupyter Notebook Markup Cell Table Example - Python
- How to Force Quit Microsoft Excel Application on Mac - Microsoft
- How to install Microsoft OneDrive on Mac Sonoma 14 - MacOS
- How to close all tabs of Notepad++? - NotepadPlusPlus
- Word wrap text in Notepad++ - NotepadPlusPlus
- Reading JSON file in Python with Examples - Python