JUnit5 tests work fine with maven but not when run through Eclipse, "No tests found with test runner 'JUnit 5'." - eclipse

I'm getting a pop-up window with title "Could not run test" and message "No tests found with test runner 'JUnit 5'." when I try to run JUnit 5 tests with Eclipse via Run As > JUnit Test.
I have two test-related files. One is a test suite:
...
import org.junit.jupiter.api.BeforeAll;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;
#RunWith(JUnitPlatform.class)
#SelectPackages("com.foo.stufftotest")
public class TestSuite {
#BeforeAll
public static void setup() {
...
The other contains the "actual" tests:
package com.foo.stufftotest;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import com.foo.TestSuite;
import com.foo.business.mechanics.LogicStuff1;
public class BusinessTest {
#Test
public void testLogic1() {
...
}
#Test
public void testLogic2() {
...
}
...
All the testLogicN() methods depend on setup stuff done in TestSuite.setup(). If setup() doesn't run, there are a lot of null values, and it's no surprise that things fail. When I try to run JUnit from the project's context menu, all the tests are triggered and they all fail; the suite doesn't seem to be recognized. When I try to run JUnit specifically from TestSuite.java's context menu, I end up with the error I mentioned at the top of the question.
However, when I run maven test on the project, the suite is triggered properly, and all the tests pass. Therefore, the code itself doesn't seem to be the problem.
I don't remember having this issue with JUnit 4, although I never used JUnit 4 with this particular project.
Am I using Eclipse wrong, or am I using JUnit5 wrong? What's the fix here?

I encountered the same issue in Eclipse Oxygen 4.7.1 despite the fact that my unit tests were properly annotated with org.junit.jupiter.
I used the Maven > Update Project and selected Update project configuration from pom.xml but that did nothing even though I thought it would pick up the fact that I had JUnit5 dependencies in the pom.
My solution:
Open Java Build Path, select Libraries tab and Add Library.
Select JUnit.
Select JUnit5 for JUnit library version.
Once added I was able to manually execute tests from Eclipse. However I'm still not certain why this was not added automatically.

I'm getting a pop-up window with title "Could not run test" and message "No tests found with test runner 'JUnit 5'." when I try to run JUnit 5 tests with Eclipse via Run As > JUnit Test.
That's because TestSuite is in fact not a JUnit 5 test class. Since it is annotated with #RunWith (from JUnit 4) it is a JUnit 4 test class. To avoid the pop-up window in Eclipse, simply click on "Run Configurations" and select JUnit 4 instead of JUnit 5 for running the test class.
The other issue you have is that #BeforeAll is an annotation from JUnit Jupiter. Thus, it is simply not supported in a class annotated with #RunWith(JUnitPlatform.class) (unless that class also happens to contain #Test methods for JUnit Jupiter). Thus you will have to find an alternative approach to executing your "set up" code.

Related

How do I run a JUnit class annotated with #RunWith on Eclipse?

Usually, I would run my test classes by right clicking the class -> Run As -> JUnit Test. But I get errors if the class is annotated with #RunWith
For example, for a test class with the following structure:
import org.junit.Test;
import org.junit.runner.RunWith;
import mockit.integration.junit4.JMockit;
#RunWith(JMockit.class)
public class SwiftResourceIT {
}
I get the following error:
Caused by: java.lang.IllegalStateException: Running on JDK 9 requires -javaagent:<proper path>/jmockit-1.n.jar or -Djdk.attach.allowAttachSelf
As a Maven project that is configured to run tests when I do a build, the tests run fine when I clean install. I just don't know how to run this class by itself.
Thanks in advance
This is something you can configure in the "Run Configuration". Right after failing to run the test class, bring up the "Run Configurations" dialog (multiple ways to get there), the one you just ran should be selected. Click on the "Arguments" tab, put the required "javaagent" command-line arguments into that field and store them.
If you don't like having to edit this for every test class, you might consider changing your mocking technology. Mockito has no need for something like this.

junit test cannot be resolved to a type after adding junit jar file

I want to create a simple Junit test. However, after I buildpath using Eclipse for Junit4, it still give me an error: junit test cannot be resolved to a type for:
JunitTesting class
Please see my screenshot for setting:

Getting "File not found exception" while running citrus tests during project build

I have a method like this which executes xml files located in a project package
#Test
#CitrusXMlTest(packageScan="com.something.xxxx")
public void citrusTest()
{
}
while running normally using testng it was executing fine but while building with maven during test phase tests are failing because of file not found exception. But while mentioning the path relate windows file system the tests were executing fine like this
#CitrusXMlTest(packageScan="file:D:\\xxx\\xyz")
How to make citrus to execute xml test cases that are in class path. your help is really appreciated.
You have to create the package name mentioned in the package scan under sec/test/resources not in the src/test/java

Eclipse Run As doesn't show Junit or testNG option

I am trying to execute a simple test case using Junit4 in Eclipse (version 3.7.0)
1) I click on the Run icon (next to the debug icon), then click on the Run As option, it only shows Java Application as an option
2) when I click on Run Configuration option, within the Create, Manage and run configuration window, I saw Junit and TestNG listed in the left pane
3)click on Window>show views>I can see Junit and TestNG listed as an option
Please advise me how to resolve this issue. Thanks!
Project Name: webDriverMethods
below is my code
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
import org.testng.annotations.*;
import org.testng.annotations.Test; //added 5 imports for Junit and testNG
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class webDriverMethodsTest
{
#Test
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Does you add Testng and Junit .Jar file in your current project, if you add TestNG and Junit jar file in your project then you can run your project with testng and Junit
you check here how to install Testng in Ecplise
You can download Junit form Here after download import jar file in ecplise and then check
You can't add #Test on a static method with arguments, it will be ignored. This could possibly work with TestNG if you define a data provider but it's most likely not what you want. Test methods are regular methods on the class, see http://testng.org for examples.
You need to have a method annotated with #Test for the plugins to recognize it as a TestNG test. Add an import of the testng test annotation to see run as-> TestNG as an option or impprt #Test from junit to see run as -> Junit, TestNG as an option.

Run tests in broken project using SBT

When doing a serious refactor in a Java Eclipse project I will often break the build, but focus on getting one test to pass at a time. When running the tests Eclipse warns that the project cannot be compiled, but it will still run the tests it can compile.
Now I'm using SBT and would like to achieve the same thing with 'test-only', but it tries to compile the whole project, fails, and doesn't run the tests. How can I tell it to just compile the bits it can and run the tests.
You should add the following task to your project definition:
import sbt._
class Project(info: ProjectInfo) extends DefaultProject(info) {
lazy val justTest = testTask(testFrameworks, testClasspath, testCompileConditional.analysis, testOptions)
}
This is the same as the ordinary test task, but has no dependencies attached at the end. If you'd like it to have dependencies, call dependsOn on the testTask(...) expression and provide the tasks you want it to depend on.
testTask(testFrameworks, testClasspath, testCompileConditional.analysis, testOptions).dependsOn(testCompile, copyResources, copyTestResources)