0

I create UnitTest (without database operations). I want to write a test for https://github.com/JonkiPro/popcorn/blob/develop/popcorn-core/src/main/java/com/jonki/popcorn/core/jpa/service/MoviePersistenceServiceImpl.java#L238

I created a field in the test class

private EntityManager entityManager;

and I added the setup method

@Before
public void setup() {
     this.entityManager = Mockito.mock(EntityManager.class);
}

after I created a test

@Test
public void canUpdateOtherTitle() throws ResourceException {
    final MovieOtherTitleEntity movieOtherTitleEntity = new MovieOtherTitleEntity();
    final OtherTitle otherTitle = new OtherTitle.Builder(
            UUID.randomUUID().toString(),
            CountryType.USA
    ).build();
    Mockito
            .when(this.entityManager.find(MovieOtherTitleEntity.class, 1L))
            .thenReturn(movieOtherTitleEntity);
    this.moviePersistenceService.updateOtherTitle(otherTitle, 1L, movieEntity);
}

I get an exception after running the test NullPointerException

java.lang.NullPointerException
at com.jonki.popcorn.core.jpa.service.MoviePersistenceServiceImpl.updateOtherTitle(MoviePersistenceServiceImpl.java:191)
at com.jonki.popcorn.core.jpa.service.MoviePersistenceServiceImplUnitTests.canUpdateOtherTitle(MoviePersistenceServiceImplUnitTests.java:216)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

NULL is returned in a line

final MovieOtherTitleEntity movieOtherTitle = this.entityManager.find(MovieOtherTitleEntity.class, otherTitleId);

Why is NULL returned, since I added

Mockito
        .when(this.entityManager.find(MovieOtherTitleEntity.class, 1L))
        .thenReturn(movieOtherTitleEntity);

?

asfsdgfsd
  • 11
  • 1
  • 3

1 Answers1

-1

Try doing something like this and see what happens:

Mockito
        .when(this.entityManager.find(any(), any()))
        .thenReturn(movieOtherTitleEntity);

Also check if your class is annotated properly using @RunWith annotation for Mockito. Use @RunWith(MockitoJUnitRunner.class) instead of @RunWith(PowerMockRunner.class).

Those topics might help in solving your problem as well - 1 and 2.

MattIT
  • 316
  • 2
  • 15
  • A guess is not an answer. A "_try this_" is not an explanation of what the OP did wrong. If you need to seek clarification use the comments. One of the things I really dislike is people posting answers that suggest adding black magic without any explanation as to why. For example, `MockitoJUnitRunner` is used to enable `@Mock` and `@InjectMocks` in the test - I don't see those being used. It's not clear that the OP is even using Power Mockito. – Boris the Spider Jun 15 '18 at 06:23