JPA Hibernate Maven testing --> unknown class - java

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.

Related

IntelliJ: Setting different working directories per class in multiple class tests

I want to run a suite of tests in IntelliJ within a single run configuration,
and I want each class of Junit tests to have it's own work directory.
What I tried to do was to set the Working directory to $MODULE_WORKING_DIR$\$FileClass$\
However, I'm getting the error "Cannot start process, the working directory 'c:/...' does not exist".
I would even settle on having the same workdir per class and long as there's a way to make sure it's cleaned after each class.
Any suggestions?

Unable to run JUnit test in IntelliJ

I wanted to start project using TDD. Created the test directory and later changed it to package that is integrated with src direcotry. In both cases I get the same error:
Class not found: "tests.objectsTest"
I tried different techniques of importing JUnit jar and none solved problem. Also I tried to rename my test class but it gives no solutons whatsoever.
It seems that IntelliJ or JUnit changes name of the test class. Shouldn't it be objectsTest.tests?
I am using JUnit version 4.12 and latest IntelliJ EAP.
This is my project structure:
Project:
-.idea
-src
-logic
-objects
-tests
-test
-test.java
src and tests are directories marked as Source and Test. Every package except test is empty. On my other PC with IntelliJ Community Edition everything works fine but on EAP there is this bug. Unfortunatelly I have to use EAP.
test.java code:
package test;
import org.junit.Test;
public class test {
#Test
public void canCreateInhabitant(){
}
}
Have you checked if you have the JUnit plugin enabled? I (foolishly, shame!) disabled it at some point and was unable to get IDEA to run my tests until I remembered to turn the plugin back on...
Check the root directory of your classes. It must be marked as source (for java classes) or test (for java test classes).
It seems that your directory is not well marked in IntelliJ.
I did a simple test and put it on github.
It's the absolute simplest of tests but it works great, standing inside the test class pressing shift+ctrl+t will run the test.
Go ahead and clone it and try it out.
Easiest way is:
Open Class in Intellij and press Ctrl+Shift+T
Select "Create New Test"
Now, a new pop up will be opened where you can select Unit Test Library (For your case its Junit4)
Select the methods which you want to include in test
And there you go !
Sometimes I find that this happens when I try and launch "All tests" from the project folder in the structure view. Launching all tests by right clicking on the test root folder and selecting "all tests" from there seems to solve it.
Just had this happen to me. When I built via Maven it had a problem. When I fixed the problem, it would run the junits again. Goofy.
I had the same problem, I solved it by clicking File-> Invalidate chaces and Invalidate an Restart

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.

Gradle test tasks with webcontext fails to use entities

I have an issue with some gradle task that should run tests. Since this is some legacy from ant tasks we do not want to include them into our test suite. Especially considering that those ant ones are in testng, and those made by us, and used on regular basis are made using spock and junit.
The problem is that those tests are using some context which works pretty well when I run those tests under eclipse IDE, but it fails if I try to do sth like:
task testNgTesting(type: Test, dependsOn: testClasses){
useTestNG()
includes = ["**/*IT*"]
}
But when I try to use that task I get errors like "org.hibernate.MappingException: Unknown entity:" or "java.lang.IllegalArgumentException: No query defined for that name"
Actually the problem is deeper. Gradle is trying to be smart and from whatever folder it has defined it puts classes files into classes folder, and all the other files into resources. When persistence.xml is loaded it scans for annotated entities starting with classpath root for folder it is present in (i.e. build/resources/main). Since all those classes are in build/classes/main it fails to find them. The workaround I've made is introduce two copy tasks. one named is copying persistence.xml into classes folder, and another is moving this file back to resources after the tests are finished. You might want to use something like
testNgTesting.finalizedBy(cleanAfterIntegrationTests) to make sure the cleanup occurs even if there are some tests that fails.

Can't run JUnit in Eclipse Indigo

I create simple Java Application project with src and test folders. src folder consists of the class which I test and test folder consists of the class where I describe test. When I start testing, using JUnit 4 I get: Could not find the main class: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. Program will exit. error. All JAR files are presented in my project.
What did I miss in my project?
That is a class that is part of the Eclipse plugin, it sounds like you have a corrupted installation of Eclipse. Try with a fresh installation.
Far shot: is the test-folder containing the .java-files for your tests also declared as a source-folder in the project's Build Path settings?
All JUnit tests should be written as follows:
#Test //This is very very important
public void testAbc()
{
//Test Abc() here....
}

Categories