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!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- How to come out of dquote prompt in Terminal - macOS/Linux - MacOS
- SharePoint error 2130575251 - The security validation for this page is invalid and might be corrupted - SharePoint
- ActivityManager Warning: Activity not started, its current task has been brought to the front - Android
- scp: ssh: connect to host xxxx port 22: Connection refused Error - Linux
- How to create a Java Project as a Git Repository with IntelliJ - Java
- macOS Ventura Login Item Added Google Chrome Notification - MacOS
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED - Android
- How to Use Snipping Tool on Windows 10/11 to Take Screenshots - Windows