JUnit succeeds when run as "debug as junit test" but fails as "debug as gradle test".

I'm doing a simple hibernate test and the code all works fine when run from Eclipse normally as well as when I create a junit to test it. But I'm now trying to run as ./gradlew test (from bash or within eclipse) and I'm having a problem with: em.persist(jazz); causing: org.opentest4j.AssertionFailedError: Unexpected exception thrown: java.lang.IllegalArgumentException: Unable to locate persister: entities.Genre There must be some config or context which is missing from the gradle test which is generally present.
19 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @Dave12345! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2mo ago
I assume you imported it into Eclipse as a Gradle project? Are there any other logs?
Dave12345
Dave12345OP2mo ago
It was created as a gradle project. I can clean/build just fine. I can run it on the command line by copying the command line params from eclipse.
What logs should I be looking for? I've added log4j logging which is how I know about this error.
dan1st
dan1st2mo ago
the full output of ./gradlew test
dan1st
dan1st2mo ago
Can you attach a debugger?
Dave12345
Dave12345OP2mo ago
Not sure. I mean, I can try anything you suggest! I can run the test from within eclipse if that's easier.
dan1st
dan1st2mo ago
Well as long as you can debug it in Eclipse and use breakpoints but still run the test in a way that fails
Dave12345
Dave12345OP2mo ago
org.hibernate.internal.SessionImpl.firePersist(PersistEvent) L760: fastSessionServices.eventListenerGroup_PERSIST .fireEventOnEachListener( event, PersistEventListener::onPersist ); MappingException I mean, I'm guessing, though, that this is an example of a wider problem, right? If any code works in junit but not via gradlew then I'm missing some setup/config code, right? I think tests can see stuff in main/resources so it's not that
dan1st
dan1st2mo ago
probably Is there any file that's in both main/resources and test/resources like the same filename/path Can you show a screenshot of the project structure? typically there's something with your setup but there are a few things Eclipse does differently
Dave12345
Dave12345OP2mo ago
there is nothing at all in test/resources (I tried copying stuff there earlier but it made no diff)
dan1st
dan1st2mo ago
ok Does the issue also happen locally or is it a CI thing? What files do you have in your resources? Can you show your persistence.xml?
Dave12345
Dave12345OP2mo ago
there's no CI here - just me experimenting with stuff on my laptop. from the junit class, which has two tests, i can debug as junit and both tests pass. one test literally is just assertequals(1,1) to check I was running tests properly from gradle. Running just that tests (with the other commented out) I can run it via junit and gradle (gradle both from within eclipse and from the command line). Persistance.xml:
<persistence
version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">

<persistence-unit name="my-persistence-unit"
transaction-type="RESOURCE_LOCAL">
<description>descr</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>

</persistence>
<persistence
version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">

<persistence-unit name="my-persistence-unit"
transaction-type="RESOURCE_LOCAL">
<description>descr</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>

</persistence>
my config is in hibernate.properties but that's just db setup
dan1st
dan1st2mo ago
Are you using plain hibernate? What's the package for the entities? Where are your tests?
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Dave12345
Dave12345OP2mo ago
Yes, this is my first hibernate project - I have 3 classes each with a couple of fields. tests are in src/test/java entities are in src/main/java
dan1st
dan1st2mo ago
which packages?
Dave12345
Dave12345OP2mo ago
everything is in the package "hibernate" just diffing a log from both environments (junit and gradle). noticed: PU root URL: file:/home/anon/dev/projects/hibernate/lib/build/resources/test/ vs: PU root URL: file:/home/anon/dev/projects/hibernate/lib/bin/main/ for gradle/junit
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?