Skip to content
Advertisement

Jpa unit test – Service – entity manager null

This is what I tried:

JavaScript

the error that I get is ‘ Runner org.junit.internal.runners.ErrorReportingRunner (used on class com.unibuc.AWBD_Project_v1.services.ServiceTest) does not support filtering and will therefore be run completely. Test class should have exactly one public zero-argument constructor’

Here is the LocationService:

JavaScript

Advertisement

Answer

Hello there is 3 issues in your test code.

1 you should remove the EntityManager entityManager from your test constructor to have a runnable test class.

2 if you want to use entityManager inside your test class you should @Autowired it

JavaScript

3 It’s look like you are testing entityManager and not your LocationService In an unit test you should mock dependencies like entityManager using Mockito

It’s seems like you wanted to create an integration test.

The 3 steps of one integration test of a service (exemple with findLocation())

  1. Prepare the data inside a test database Create a new location object and save it into database using the entityManager or the testEntityManager.

  2. Execute your findLocation methode on the id Don’t forget to Autowire your service class.

  3. Verify if the retrieved data is as expected Compare the retrieved Location object with the one you’ve saved.

Here’s the code

JavaScript

If you have any question I’m available to help you.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement