org.testng.TestNGException: Cannot find class in classpath - java

I have tried to create the testing framework using this tutorial
While executing the code, I am getting the below exception. Please tell me what I need to do with my code and Java configuration.
org.testng.TestNGException:
Cannot find class in classpath: sampletest

Do you use Eclipse? Try Project->Clean and run again. I have seen similar behaviour before for various users.

Related

Error: Could not Find or load main class 0 - TestNG Error

When I run my class as a TestNG Im getting below error,
Error:
Could not find or load main class 0.
But when i run the class with normal java main method it is working fine.
Can someone please help me.
After installing the m2e plugin from eclipse it worked.

How to execute JUnitTests in Runtime-Workspace (JUnit-Plugin-Test)?

I'm facing a problem with JUnit tests. I have written an JUnitRunner which is used to execute the WrapperTest.
The WrapperTest generates a plain JUnit-Test and a needed file. If I want to execute the methods of the generated test, my Runner searchs in the Developement-Workspace for the "NeededClass".
I'm generating the needed class in the JUnit-Workspace and i want the tests to use this generated class file, so i can delete this file in my Develop-Workspace.
So, how do I execute the generated test in the JUnit-Workspace? (He shall look in the JUnit-Workspace for the needed file)
edit: OK, i found out, it's a ClassLoader problem... The Develop Workspace got another ClassLoader than the JUnit-Workspace, this causes weired errors, for example that a "class isn't the identical class Exception" (java.lang.ClassCastException: org.junit.runner.JUnitCore cannot be cast to org.junit.runner.JUnitCore). Looks like i have to fix this problem by reflection, what is very dirty.
Look into Maven and its build lifecycle. You can wire the code generation you are doing into the generate-test-sources phase and then have it participate as normal in the test phase.
See this question for an example.

java junit and the exception java.lang.NoClassDefFoundError

I'm really confused. I have a project "PROJ" and test project "PROJ.TEST"
PROJ is using classes from external.jar
In PROJ.TEST I write a test that is testing class from PROJ but this class is using class from external.jar.
And here is the problem. I get an error:
java.lang.NoClassDefFoundError
Caused by: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
I have searched all the Internet to find the answer but couldn't find it. Even this great post http://blog.js-development.com/2010/06/android-instrumentation-test.html doesn't help me.
I tried many combinations with Java Build Path and nothing :(
Regards
The post Android Testing: External libraries probably contains the answer to your question and a step-by-step example.

JPA Hibernate Maven testing --> unknown class

I'm trying to run a simple dataImport class which is using JPA and Hibernate.
If I run my class, i always have the following error:
Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: ch.itartis.relman.entities.code.ReferenceCode
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:675)
at ch.itartis.relman.service.test.dataimport.DataImport.doSave(DataImport.java:111)
at ch.itartis.relman.service.test.dataimport.DataImport.main(DataImport.java:43)
My Class is located in the src/test/java/ folder, I have a service-config.xml in src/test/resources/ and I also have a persistence.xml in src/test/resources/META-INF/.
If I run the class in in the src/main/java/... folder, it works. But if I want to have the class in src/test/java/, it doesn't.
What am I doing wrong?
Thanks a lot!
You are running the code from your own main method, which I am guessing means it isn't being run by maven. The code in test is not included as part of the artifact generated by maven, it is only included during mavens test phase for running unit tests.
If you are using maven, why not simply create JUnit tests that maven will run as part of the build process instead of rolling your own.
To complete Robin's answer: if you are running the class using exec:java set classpathScope to test and everything will be fine.

java.lang.NoClassDefFoundError while running JUnit test in Netbeans

I am building an Android hello world application in Netbeans. It's building properly and I am able to run in the emulator also.
But when creating and running the Junit test I get a java.lang.NoClassDefFoundError.
How can I fix this problem?
Check the manifest of your test project, for example HelloWorldTest/AndroidManifest.xml. The android:targetPackage attribute of the instrumentation markup should target the package in your application, for example, com.example.helloworld. Not the test package of your test project (eg. com.example.helloworld.test).
Simply AndroidManifest.xml -> manifest -> package of main application should match AndroidManifest.xml -> manifest -> instrumentation -> android:targetPackage of the test application.
The full error message contains the name of the class, that wasn't found on the classpath. Double check if the classpath that is used for running the test includes all required classes and libraries (your class files, junit.jar, android specific libaries).
I find that a rebuild usually finds the classes (as it is a classpath issue). Netbeans seems to aggressively compile existing tests, but the main source code (from a maven setting at least) is not always available.
I've started running full builds to try address this. There might be a plugin that addresses this but I haven't found it yet.
EDIT: This might help.
I don't know about netbeans specifics, but the problem is probably the same.
FragmentActivity can not be tested via ActivityInstrumentationTestCase2
Your test project can be using a different android compatibility library than your main project, and that causes this weird errors.

Categories