“No JUnit Tests Found” Error Solution
1. Introduction
One of the first steps that developers take in order to create Unit Test case is to ensure that their environment is ready. This means that all required libraries are setup properly and that the JUnit Test cases are correctly annotated. There might be a couple of technical hiccups like proper source folder configuration in your specific IDE, but regardless, this issues shouldn’t be a roadblock to begin with.
2. Issue: No Junit Tests Found
Developers might encounter issues along the way of setting up their test case. I myself have encountered a lot of issues before I even get my maven project building successfully. For this particular post, I will discuss how to resolve the “No JUnit Test Found” error.
3. Solution
3.1 Library setup
We need to ensure that our project has the correct dependencies or libraries. Legacy projects that do not use maven dependencies often need to import their libraries manually. This manual way of doing it is prone to inconsistent configurations and might lead to more erroneous setup. Convert your project to maven. Invest on converting your project to a full blown maven configured project.
Using maven will really get some of those setup in tact and you’ll never have to worry about missing any dependencies again.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.areyes1.jgc.itest</groupId> <artifactId>junit-no-test-found</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
In Eclipse, the libraries will be represented as a maven dependency as shown below.
3.2 @RunWith
annotation
By default this is implicitly called. When an annotated method (@Test
) is detected, it automatically calls as default runner for that entire class. If the issue persists though, you can explicitly indicate it on your class, just like the one below:
JUnitTestingSampleNoTestFound.java
package com.areyes1.jgc.no.tests; import static org.junit.Assert.assertFalse; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class JUnitTestingSampleNoTestFound { int totalNumberOfApplicants = 0; int totalNumberOfAcceptableApplicants = 10; @Test public void testAssertFalseWithMessage() { assertFalse( "Is total number of applicants acceptable?", (this.totalNumberOfApplicants != this.totalNumberOfAcceptableApplicants)); } }
3.3 @Test
annotation
This might not be the case all the time and I think any java developer would figure out what’s wrong before they even reach this page. The @Test annotation is a crucial piece of configuration for the JUnit Test case. It marks the method as a JUnit Test case.
testInvoke()
@Test public void testInvoke() { assertFalse( "Is total number of applicants acceptable?", (this.totalNumberOfApplicants != this.totalNumberOfAcceptableApplicants)); }
3.4 All else fail? Just clean up your workspace and make sure all configurations are set.
I’ve seen and heard many stories of how they encounter this error. Some suggests that the best solution is to restart your IDE. But I do think it’s just that. When you restart your IDE, the whole configuration will be refreshed automatically, and this might be the solution to that problem you’re encountering while running JUnit Test case!
Download the Eclipse project
This was an example of “No JUnit Tests Found” Error Solution.
You can download the full source code of this example here: junit-no-test-found