Eclipse Run As doesn't show Junit or testNG option - eclipse

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.

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.

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

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.

Cannot import external JAVA library using Jython in Eclipse

I'm new to Jython and to Eclipse. I'm trying to use the JGraphX JAVA library. I've created a new PyDev project and package. I've right-clicked on the project properties in Eclipse, selected "PyDev - PYTHONPATH" and added jgraphx.jar. In the init.py file, I've tried:
import jgraphx as jgx
from jgraphx import *
Neither approach works. The code is underlined as an error in Eclipse. When I run, I get "ImportError: No module named jgraphx". What am I doing wrong?
Thank you,
-david
I'm not familiar with the JGraphX library.
To import a java class
you need to use from my.project.package import someclass

Import org.junit cannot be resolved

I have a class file beginning with the following in Eclipse:
package org.jsoup.parser;
import org.junit.Assert.*;
import org.junit.Test;
import org.jsoup.nodes.Document;
import org.jsoup.parser.HtmlTreeBuilder;
However, Eclipse indicates an error saying that org.junit cannot be resolved. I have already modified the project properties to include jUnit 3 in the classpath, so wouldn't know what to do further.
Ideas?
EDIT: I have included jUnit 4 as suggested but to no avail...
If you want to use org.junit.Assert you should include JUnit 4. JUnit 3 uses junit.framework.* packages.

How can I export JUnit4 test case as runnable jar file

I am trying to export JUnit4 test case as runnable jar file through Eclipse IDE but running into problems.
The test case (exported as JUnit4 test) uses selenium webdriver to perform some GUI task.
I can launch it through Eclipse run configuration (as JUnit test) but when I try to export the same (so that it can be used independently by any third party) to runnable jar, the 'Launch Configuration' drop-down list of 'Runnable JAR File Specification' dialog box is empty.
Is there something missing? How to fix this.
try:
public static void main(String[] args) {
JUnitCore jCore = new JUnitCore();
jCore.run();
}
But then again I don't know where you would read your results and so on.
Means you would need an output file or something.