Eclipse keep saying "No tests found with test runner JUnit 5" - eclipse

I am using Eclipse Oxygen.3 Release (4.7.3). The following is my JUnit test class:
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
class MyMathTest {
MyMath myMath = new MyMath();
#Before
public void before() {
System.out.println("Before");
}
#After
public void after() {
System.out.println("After");
}
#Test
public void testSum_with3numbers() {
System.out.println("Test1");
int result = myMath.sum(new int[] {1,2,3});
int expected = 6;
assertEquals(expected, result);
}
#Test
public void testSum_with1numbers() {
System.out.println("Test2");
int result = myMath.sum(new int[] {3});
int expected = 3;
assertEquals(expected, result);
}
#BeforeClass
public static void beforeClass() {
System.out.println("Before class");
}
#AfterClass
public static void afterClass() {
System.out.println("After class");
}
}
When I run this Junit test, eclipse keeps popping up dialog telling "No tests found with test runner 'JUnit 5'". Why?

Your test class is currently based on JUnit 4 since it uses annotations from the org.junit package.
Thus, to get it to run as a JUnit 4 test class in Eclipse, you need to select the JUnit 4 Runner in the "Run Configuration". Click on the tiny arrow (pointing down) next to the green circle with a white arrow in it (at the top of the Eclipse IDE). There you should see your test class, and by selecting that you can switch between the JUnit 4 and JUnit 5 runners.
On the other hand, if your goal is to write a test using JUnit Jupiter (i.e., the programming model for JUnit 5), you'll need to switch from annotations in org.junit to org.junit.jupiter.api.

I had the same error, after trying everything, I have realized that the JUnit library wasn't added. So after adding it, tests worked as intended.

This happened to me because my test method was declared as private and JUnit could not detect it. After I made it public it worked as expected, of course with #Test annotation.

In Eclipse 2019-09 (4.13) is a bug that can cause the "No tests found with test runner 'JUnit 5'" error:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=551298
I added the following dependency to my project and it worked for me:
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.5.2</version><!--$NO-MVN-MAN-VER$-->
<scope>test</scope>
</dependency>

the test class is not public, make it public and it should work

Take a look back to your imports.
You imports import org.junit.Test; (Used for run test cases with JUnit 4)
You need to import import org.junit.jupiter.api.Test; to run tast case with JUnit5.

You are using JUnit 4 annotations with JUnit 5 dependencies.
If you want to use JUnit 5 you should replace:
#Before with #BeforeEach
#After with #AfterEach
#BeforeClass with #BeforeAll
#AfterClass with #AfterAll
#Ignore with #Disabled
Here is more about JUnit 5 annotations https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations/

Right click the java file and choose:
Build Path -> Configure Build Path -> Java Build Path.
Choose the tab Libraries in Java Build Path, On the right side tab choose Add Library.
Select JUnit and click Next.
Choose JUnit 5 in JUnit Library version dropdown and click finish.
Now run the Test with Junit Test, it will work.
This procedure worked for me.

right click and run as -> config-> make this change and run it will works cheers!!.

My 2 cents:
Right click on project -> Configure -> Add module-info -> Give some random name.
It should automatically add there a requires in the JUnit package, if not, then manually add this:
requires org.junit.jupiter.api;
And it will magically work!
* tested on Eclipse 2018-12

My issue was that, Run works fine, debug throws this error.
I was getting this merely because of low memory in the system. Ideally, eclipse couldn't launch the debug session due to low memory in the system. Error message thrown is confusing.
Hope it helps someone!

In my current case, I encountered this error when testing the pact verification on the Service Provider side.The error was thrown because the Pact Broker doesn't have any pact to test against the Service Provider.
Visit the pact broker and ensure that there is at least one pact to test with.

I had the same problem with an Eclipse (2020-09) non-Maven project. None of the proposed solutions worked, but changing the compiler from 9.0.4 to 15.0.1 did.
I did that by selecting Preferences > Java > Installed JREsand checking the box next to jdk-15.01. You may need to install a recent JDK if none are shown.

Adding the JUnit vintage dependency fixed the issue for me.
https://junit.org/junit5/docs/current/user-guide/#migrating-from-junit4-running
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>

Related

Could not find main method from given launch configuration

I've a simple Java project that works when I execute it at Eclipse environment. But when I try to export it to a Runnable Jar, I get the following error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: JavaSwing/src.main.java/com/cansoft/GUIProgram.java
Exported with compile warnings: JavaSwing/src.main.java/com/util/Util.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
I read other posts which suggest to create a MANIFEST.MF file specifying the main-class which I did. It is placed at MyProjectFolder/META-INF/MANIFEST.MF and it contains the following information:
Manifest-Version: 1.0
Class-Path: resources
main-class: com.cansoft.GUIProgram
My main class is as follows:
public class GUIProgram {
private JFrame folderCreationSubappFrame;
private Color color;
private String home;
private final static Logger LOG_MONITOR = Logger.getLogger("com.cansoft");
public static void main(String[] args) {
try {
new GUIProgram();
} catch (Exception e) {
LOG_MONITOR.log(Level.INFO,e.getMessage());
}
}
public GUIProgram() throws InterruptedException, SecurityException, IOException {
home = System.getProperty("user.home") + File.separator + "Documents";
startLogSystem();
if(isFirstRun()) {
showWelcomeFrame();
} else {
initialize();
}
} .... More and more code
Does anybody know what am I missing? Any help much appreciated.
Thank you.
It is not enough to create the manifest file, you need to explicitly choose it in the Eclipse jar export dialog.
Answer to Comment
If you use "runnable jar", make sure that you chose the correct launch configuration and that the launch configuration successfully runs when chosing "Run As" -> "Run Configurations" -> "Java Application" -> Your Configuration -> "Run"
I finally find out where the problem was, it was quite simple btw. I had created my GUIProgram within a src.main.java package, but that package was created (my bad) as resources instead of folders, so Eclipse was smart enought to run it but when trying to generate the JAR which expected a correct java project structure, it was failing because truly there were not GUIProgram java class at src path (src was not folder type but resources).
Hope I succeed explaining.

ExpectedException with #Rule in junit test on eclipse IDE does not work

I need to make a test in junit that passes if an exception is thrown, but fail time and again.
I read a bunch of questions and answers on the topic here in stackoverflow and on other sources. Eventually I came across this page, that explains the usage of Class ExpectedException, by junit.org.
Since I could not get my own test working I copied their bare-bones example and it still did not work.
here is my code:
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;
class AssertExceptionTest {
#Rule
public ExpectedException thrown= ExpectedException.none();
#Test
public void throwsNothing() {
// no exception expected, none thrown: passes.
}
#Test
public void throwsExceptionWithSpecificType() {
thrown.expect(NullPointerException.class);
throw new NullPointerException();
}
}
Citing from the page I mentioned above, the explanation goes "...After specifiying the type of the expected exception your test is successful when such an exception is thrown and it fails if a different or no exception is thrown...
Problem is that the test still fails, no matter what I do, and it fails because of what I am trying to verify: throwing NullPointerException.
I thought that maybe, because I am using junit 5, my test fails. However, this question from stackoverflow suggests otherwise: the guy asking the question mentions he is using junit 5 in eclipse the same way as in my code, successfully.
Technical details:
eclipse version: 2019-12 (4.14.0)
junit version: junit 5
working on Ubuntu, version: 18.04.2 LTS.
Update:
I used assertThrows(), and it worked for me. However, I am still puzzled over the reason I didn't succeed with the methods described above, which many people here suggest.
Thanks in advance!
JUnit 5 does not support JUnit 4 Rules out of the box.
To make your code working:
Add the following dependency (version might change over time, of course)
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
Next, put #EnableRuleMigrationSupport on top of your test class.
That's it. See this for more information.

How to update JUnit5 jupiter.api_5.3.1 to jupiter.api_5.4.2?

i want to know that if "MethodOrderer" class is available in JUnit5 Library FOR ECLIPSE or not, because i am unable to find it.
If not, how can i shift jupiter.api_5.3.1 to jupiter.api_5.4.2 in eclipse JUnit5 library?
Will be thankful to see your reply.
I downloaded JUnit5 jar file from "https://search.maven.org/artifact/name.remal.tools.test/junit5/1.26.97/jar" and this jar does have "MethodOrderer" class but when i add this to project dependency and run the testclass, eclipse shows up this error "No tests found with test runner 'JUnit5'."
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class JunitCalculatorV4 {
#BeforeAll
static void setUpBeforeClass() throws Exception {
System.out.println("Before All");
}
#AfterAll
static void tearDownAfterClass() throws Exception {
System.out.println("After All");
}
#Test
#Order(1)
void addTest() {
System.out.println("Add test");
}
#Test
#Order(2)
void divideTest() {
System.out.println("Divide Test");
}
}
Actually this annotation #TestMethodOrder(MethodOrderer.OrderAnnotation.class) is from jupiter.api_5.4.2 which i added as an external jar, and that might be causing conflict with the existing JUnit5 library.
My problem would be solved if the JUnit5 library is updated as a whole, or atleast the jarfile inside the library is updated.
Project > Properties: Java Build Path, tab Libraries:
You are using Eclipse 2018-12 (4.10) with JUnit 5.3.1 instead of Eclipse 2019-03 (4.11) with JUnit 5.4.0 (the screenshot shows JAR file names containing _5.3.1.v20181005- instead of _5.4.0.v20190212-).
Please upgrade.

spring junits are not reconized in eclipse

I have a test case which looks like the following
#ContextConfiguration(locations = { "classpath:chartContext-test.xml" })
#Component("allocationChartTest")
public class ChartServiceUnitTest extends AbstractTestNGSpringContextTests {
and I have imports which look like the following
org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
so basically i am using spring junits it is nothing to do with junit 4 or 3
When i right click on the class it does not give an option called run as junit
not sure what needs to be added scr/mian/java and src/main/test both are included in my source..
Can some one give an idea why it is is giving me an option ..
Why does your test class have the #Component annotation? Anyhow, you need to install TestNG in Eclipse. Are you familiar with tutorials such as this one: http://java.dzone.com/articles/spring-testing-support-testng?

Build Path issue with Java Android project when starting Eclipse

I booted up eclipse to start on a project, and without typing in a word I already have several errors.
package department.namespace;
import android.app.Activity;
import android.os.Bundle;
public class DepartmentActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
At package department.namespace, it says:
multiple marks at this line and the type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
The import android.os cannot be resolved.
Override cannot be resolved as a type
The method OnCreate(Bundle) is undefined for the type Activity
R. cannot be resolved as a variable.
Since this JUST came up when I started Eclipse, how do I fix this?
First, you should start by cleaning and building the project. This can be done by selecting the project of interest and then selecting the appropriate option from the project menu.
If that doesn't resolve the issue, then I would recommend checking the projects build path to ensure that your expected dependencies are present and accounted for. If I remember correctly when I have had this issue in the past, it helps to remove and re-add the JRE of choice.
To look into this issue further, you might check some of the following links:
Android Dev Specific - http://kyleclegg.com/eclipse-android-error-type-cannot-be-resolved/
http://dev-answers.blogspot.com/2009/06/eclipse-build-errors-javalangobject.html
http://www.adriancourreges.com/articles/the-type-java-lang-object-cannot-be-resolved-with-eclipse/