Junit Test is Not working - junit4

I'm getting below exception when Debugging my application using Junit
IDE Used-Eclipse Mar
Application Details-
JDK-1.7,Spring4 and Junit4 and hamcrest-all-1.3.jar
CodeBase-
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.app.api.facade.LoginServiceFacade;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("classpath:/test-spring.xml")
public class LoginApi4JunitTest {
#Autowired
private LoginServiceFacade loginServiceFacade;
#Test
public void testIsUserExist()throws Exception{
Boolean isUserExist=loginServiceFacade.isUserExist("xyz");
Assert.assertEquals(Boolean.TRUE, isUserExist);
}
}
Exception-
java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:320)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:310)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:283)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:207)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:191)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.manipulation.Filter
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 8 more
I already added Junit4 and hamcrest-all-1.3.jar in myBuild path.

Try to remove the Eclipse launcher of the test, and run it again.
Go to Run/Run configurations/JUnit, select the launcher and click on "Remove". Then run the test again.

As have seen this when there is a broken/missing maven container.
Edit .classpath and remove the entry:
classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"

Got this error when attempting to run a spring boot test.
It turned out that the test class was mistakenly in src/main/java and not in src/test/java.

Related

java.lang.NoClassDefFoundError: org/openqa/selenium/remote/service/DriverService$Builder

I am trying to create a TestNg Maven project in Eclipse for Selenium. Here is my code :
package Edureka_Case_Study_8th.Edureka_Case_Study_8th;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CaseStudy2 {
#Test
public void basicSetup() {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Sitesh\\Selenium\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.flipkart.com/ ");
}
}
When I try to run it, I get the following error:
[RemoteTestNG] detected TestNG version 7.3.0
FAILED: basicSetup
java.lang.NoClassDefFoundError: org/openqa/selenium/remote/service/DriverService$Builder
at Edureka_Case_Study_8th.Edureka_Case_Study_8th.CaseStudy2.basicSetup(CaseStudy2.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:599)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:822)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.util.ArrayList.forEach(Unknown Source)
at org.testng.TestRunner.privateRun(TestRunner.java:764)
at org.testng.TestRunner.run(TestRunner.java:585)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1218)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.runSuites(TestNG.java:1069)
at org.testng.TestNG.run(TestNG.java:1037)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassNotFoundException:
org.openqa.selenium.remote.service.DriverService$Builder
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 29 more
Can anybody please come up with a suggestion / solution ?
Artifacts used :
Eclipse - Version: 2020-06 (4.16.0) , Selenium - v3.141.59
Its look like issue is with Selenium Libraries. Please add all selenium jars. Follow below:
Download Selenium Client & WebDriver Language Bindings for Java from (Make sure you are downloading as per your system configuration) https://www.selenium.dev/downloads/
Unzip the file.
Open your project > Right click > Build Path > Configure Built Path > Libraries > Add External Jars
Select all jars present inside above unzipped folder ( Make sure you add libs also).
Also insure Java is installed and path for JDK is pointed under execution environment.
Go to Build Path > Configure Built Path > Libraries.
Open JRE System Libraries
And if by default selected environment is not pointing to JDK. Select Alternate JRE.
Click on Installed JREs and browse to JDK folder select.
Note : Using Same Code and above mentioned SetUp I am able to run your script.

Executable Scala Jar from Eclipse using Java main class

I am trying to create an executable Jar from my Scala project. Therefore, I followed a tutorial. It suggested creating a Java main class within eclipse, which calls the Scala entry point. This works fine when executing within eclipse. After adding this class I was able to export an executable jar. However, it wont work with
java -jar myjar.jar
I made sure to activate "Package required library into jar" when exporting. My Java main class looks like this (where Driver is also located in the package core)
package core;
public class Main {
public static void main(String[] args) {
Driver.main(args);
}
}
And when executing the exported char the follwing error is thrown, which I can debug:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: scala/collection/Seq
at core.Driver$.main(Driver.scala:14)
at core.Driver.main(Driver.scala)
at core.Main.main(Main.java:5)
... 5 more
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
You could use the One-jar application to guarantee all the necessary jars are in the jarfile, I suspect the scala-lang jars are not in it or in the classpath of the command line.
Maven: https://code.google.com/p/onejar-maven-plugin/
SBT: https://github.com/sbt/sbt-onejar
If you aren't using either maven or SBT I would suggest switching to them as well as they are the main supported build systems in Scala

Creating EPStatement in Esper by using Eclipse

I have the maven plug in installed on my Eclipse and added Esper jar to my library.
I tried to ran a simple example with Esper but I failed on the following code:
EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
String expression = "select avg(price) from org.myapp.event.OrderEvent.win:time(30 sec)";
EPStatement statement = epService.getEPAdministrator().createEPL(expression);
And I got exception at createEPL(expression):
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/v4/runtime/tree/Tree
at com.espertech.esper.core.service.EPAdministratorHelper.<clinit> (EPAdministratorHelper.java:43)
at com.espertech.esper.core.service.EPAdministratorImpl.createEPLStmt(EPAdministratorImpl.java:116)
at com.espertech.esper.core.service.EPAdministratorImpl.createEPL(EPAdministratorImpl.java:66)
at Main.main(Main.java:57)
Caused by: java.lang.ClassNotFoundException: org.antlr.v4.runtime.tree.Tree
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 4 more
It seems like that esper failed to find org/antlr/v4/runtime but I am pretty sure that this package is in the library.
Did I miss anything to run the code?
The jar file that is not in your classpath is antlr-runtime-4.1.jar, a required Esper dependency.

Running Scala Jar

When I package my scala code into a jar using Eclipse, I am unable to run said jar file. I've got:
object Hello extends App{
println("Hello World!")
}
and:
public class Runner {
public static void main(String[] args) {
Hello.main(args);
}
}
I use eclipse to package this into a runnable jar called Hello.jar. java -jar Hello.jar then gives:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/App
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at Hello.main(Hello.scala)
at Runner.main(Runner.java:4)
Caused by: java.lang.ClassNotFoundException: scala.App
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 13 more
Your application has a dependency on the Scala runtime libraries. These need to be available when the app is run, and currently they are not.
There are two ways to fix this:
Specify the classpath when you launch the JAR - convenient as a one-off, but clunky. Run java -jar /path/to/yourapp.jar from the command line, and add a -cp parameter which includes the path to the Scala library JAR.
Package up the Scala library JAR inside your own JAR, and modify the Manifest to reference this JAR (by adding the Class-Path attribute - see here). This requires more work with your build process, but then means that the app can be launched by simply double-clicking on the resulting JAR.
That's because eclipse don't ship scala-library with your code. Even more, you should make sure that all dependencies are in manifest file for executable jar. Here's a similar question from stackoverflow:
running scala apps with java -jar

Ant JUnit Task couldn't find junit/framework/TestCase.class

I'm trying to get a JUnit 4.8.1 task to run in Ant 1.7.1. My IDE is Eclipse Helios. I've been banging my head against a brick wall for 2 days now and cannot figure this out. I'm sure from reading other posts its a classpath problem, but I can't see where I'm going wrong.
My JUnit4 test suite is defined as follows:
package mypackage.tests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* JUnit 4 Test Suite for the entire <code>mypackage</code>
* package
*/
#RunWith(Suite.class)
#Suite.SuiteClasses({ mypackage.tests.controller.AllTests.class })
public class AllTests {
}
...simple enough, but the test fails with a ClassNotFoundException
java.lang.ClassNotFoundException: mypackage.tests.AllTests
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
When I switch on the debug flag, I get the following trace from Ant:
(...)
[junit] Couldn't find junit/framework/TestCase.class
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-launcher.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-junit.jar
fileset: Setup scanner in dir C:\eclipse\plugins with patternSet{ includes: [org.junit_4.8.1.v4_8_1_v20100427-1100/junit.jar] excludes: [] }
Finding class junit.framework.Test
Loaded from C:\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar junit/framework/Test.class
(...)
Clearly the JUnit jar is on the classpath, and other classes such as Test.class are being picked up, so why am I getting the 'couldn't find TestClass.class' message?
I tried adding the JUnit.jar to the ant classpath in the Eclipse GUI, and this has the following effect:
(...)
[junit] Found C:\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-launcher.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-junit.jar
(...)
[junit] WARNING: multiple versions of ant detected in path for junit
[junit] jar:file:C:/eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
[junit] and jar:file:/C:/eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
(...)
Can someone please please please help me to get this working!
Oh, I almost forgot... the test suite runs fine from within Eclipse.
Thanks in advance!
The error is caused by the fact that your test classes cannot be loaded.
Did you put the package mypackage.tests.controller (i.e. the folder(s) containing the class files or the jar archive) on the classpath of your junit call? This can be done using a nested classpath structure. (http://ant.apache.org/manual/Tasks/junit.html).