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.
Related
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.
I create the simplest maven project in eclipse. I add one JUnit test in src/test/java. Then I create a simple application (in src/main/java) that tries to do Class.forName("package.MyTestClass");. The class is not found even though the eclipse project is defined to export the src/test/java as a source folder.
What is going on? How can I fix this?
I wanted to do the following with TestNG
public static void main(String[] args) throws ClassNotFoundException {
// Class.forName("package.MyTestClass");
TestNG.main(new String[]{"testng.xml"});
}
It fails because it cannot find the test classes with:
[TestNG] [ERROR] Cannot find class in classpath: package.MyTestClass
If I have understood you correctly, your project is like this
/src/main/java/Main.java (or whatever you have called it)
/src/test/java/package/MyTest.java
In your Main class you are trying to create an instance of a test class. This will not work. The classes in your main directory are available to the classes in your test directory, not the other way around, which makes sense. Your test classes must know about your application to test it, but your application should not know about the test classes
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.
Currently I am running eclipse and play (with ~run) at the same time. When I change a file it will be compiled by play and by eclipse.
Is it possible to avoid one of those two compilation steps?
As far as I know, the eclipse plugin also uses sbt to build the project so maybe there is a way to execute the play "run" command inside eclipse?
(I am asking because my laptop is not very fast and compilation takes some time, and I would like to have the "Fast turnaround" as advertised on the play webpage ;)
You can turn off Build Automatically from the Project menu without losing any of the IDE functionality. Binaries will be built only by Sbt (on the command line).
A detailed guide for setting-up Play 2 with Scala IDE can be found on the Scala IDE website: http://scala-ide.org/docs/tutorials/play20scalaide20/index.html
I have not yet tried to run the play run sbt task in eclipse.
BUT you can run the server directly from eclipse.
Add "target/scala-2.9.1/classes" to your class path, use filters to include only your assets. (Project Properties, Java Build Path)
Choose "Run Configurations..." from the Run-Button-Menu.
Create a new "Java Application" configuration with your favourite name.
Main Tab: Use "DebugStart" as your main class
Arguments Tab: Configure any "-Dconfig.file=..." "-Dlogger.file" options you might need in VM arguments
Classpath Tab: Add the conf directory to the classpath (Advanced/Add Folders)
Create DebugStart.scala with:
import play.core.server.NettyServer
import java.io.File
import play.core.SBTLink
import play.core.TestApplication
import play.api.test.FakeApplication
import play.api.test.TestServer
object DebugStart {
def main(args: Array[String]) {
val app = FakeApplication()
val server = TestServer(9000, app)
server.start()
}
}
You can now start the app with run or debug. If you use debug, you can perform some code changes without any restart.
My version of DebugStart.scala actually contains some platform dependent hackish code to kill any running process, so that I can just hit F11 or CTRL+F11 to restart the application.
To ensure that your assets/sources are up to date run:
> sbt
...
[your project] $ ~ ;play-copy-assets;sources
IntelliJ Idea 12 (Leda) is coming soon. I'm using 11 for a while and there's no such problems but new version will offer much better Play 2.0 integration.
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)