📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Overview
To ignore a test in JUnit you can either comment a method or delete the @Test annotation, but the test runner will not report such a test. Alternatively, you can add the @Ignore annotation in front or after @Test.
Test runners will report the number of ignored tests, along with the number of tests that ran and the number of tests that failed. Note that @Ignore takes an optional parameter (a String) if you want to record a reason why a test is being ignored.
Let's create a simple maven project to demonstrate usage of ignore tests in Junit 4.
1. Create a Simple Maven Project
mvn archetype:generate
-DgroupId=org.yourcompany.project
-DartifactId=application
2. Project Packaging Structure
- src/main/java – Java Source code packages and classes
- src/main/resources – NON-Java Resources, such as property files and Spring configuration
- src/test/java – Test Source code packages and classes
- src/test/resources – NON-Java Resources, such as property files and Spring configuration
── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── javadevelopersguide
│ │ └── junit
│ ├── resources
└── test
├── java
│ └── com
│ └── javadevelopersguide
│ └── junit
│ └── TestCaseIgnoreTest.java
└── resources
3. Update JUnit dependencies in a pom.xml file
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
package com.developersguide.junit;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Ignore;
import org.junit.Test;
public class TestCaseIgnoreTest {
@Test
public void test1() {
assertThat(1 + 1, is(2));
}
@Ignore
@Test
public void test2() {
assertThat(1 + 2, is(3));
}
@Test
public void test3() {
assertThat(1 + 3, is(4));
}
@Ignore("Test is ignored as a demonstration")
@Test
public void test4() {
assertThat(1 + 4, is(5));
}
@Test
public void test5() {
assertThat(1 + 5, is(6));
}
}
Output:
You can run tests from the maven command as shown below.
5. Run the test
mvn test
6. Output
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-ignoring-tests-examples 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-ignoring-tests-examples ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-ignoring-tests-examples ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-ignoring-tests-examples ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-ignoring-tests-examples ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-ignoring-tests-examples\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ junit-ignoring-tests-examples ---
[INFO] Surefire report directory: E:\Git_Work\junit-developers-guide\junit-ignoring-tests-examples\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.developersguide.junit.TestCaseIgnoreTest
Tests run: 5, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.068 sec
Results :
Tests run: 5, Failures: 0, Errors: 0, Skipped: 2
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.302 s
[INFO] Finished at: 2018-06-27T22:26:31+05:30
[INFO] Final Memory: 12M/29M
[INFO] ------------------------------------------------------------------------
Comments
Post a Comment
Leave Comment