0

I am using Junit with Mockito. I want to test EntityManager, i am getting java.lang.NullPointerException

The below is what i have tried, main class method is,

@Override
    public ReplicationPerspective buildReplicationPerspective(final String replicationDomain)
        throws ReplicationStateException {
        try {
            System.out.println("Test");
            final ReplicationPerspective localPerspective =
                this.replicationPerspectiveQuery.findReplicationPerspective(replicationDomain);

            List<String> ncdKeys = new ArrayList<String>();
            for (NodeChangeDelta ncd : this.nodeChangeDeltaQuery.findByChangeStatus(
                replicationDomain, ChangeStatus.PENDING)) {
                ncdKeys.add(ncd.getKey());
            }
            localPerspective.setPendingNodeChangeDeltaKeys(ncdKeys);

            LOGGER.debug("Local perspective is {} ", localPerspective);
            return localPerspective;
        }
        catch (Throwable t) {
            LOGGER.error("Failed to build replication perspective", t);
            throw new ReplicationStateException(t);
        }
    }

replicationPerspectiveQuery Bean file method is,

@PersistenceContext
    private EntityManager em;

@Override
    public ReplicationPerspective findReplicationPerspective(final String replicationDomain) {
        Validate.notBlank(replicationDomain);

        ReplicationPerspective perspective =
            this.em.find(ReplicationPerspective.class, replicationDomain);
        if (perspective == null) {
            this.replicationPerspectiveInitializer
                .initializeReplicationPerspective(replicationDomain);
            perspective = this.em.find(ReplicationPerspective.class, replicationDomain);
        }

        return perspective;
    }

And my test case method is,

@Test
    public void testBuildReplicationPerspective() throws ReplicationStateException {
            this.replicationStateServiceBean =
                new ReplicationStateServiceBean(null, null, null, null,
                    new ReplicationPerspectiveQueryBean(), null, null);

            this.em = Mockito.mock(EntityManager.class);
            Mockito.when(this.em.find(ReplicationPerspective.class, REPLICATION_DOMAIN))
                .thenReturn(null);

            this.replicationStateServiceBean.buildReplicationPerspective(REPLICATION_DOMAIN);
    }

I am getting NPE error in replicationPerspectiveQuery Bean file at the below line

ReplicationPerspective perspective =
            this.em.find(ReplicationPerspective.class, replicationDomain);

How to test entity manager, help me to solve.

I have also tried to mock like below but didn't work,

Mockito.when(this.replicationPerspectiveQuery.findReplicationPerspective(REPLICATION_DOMAIN)).thenReturn(null);
Arasu
  • 2,078
  • 6
  • 40
  • 67

1 Answers1

0

You are lacking the instructions to have Mockito do the actual injection. Right now you have the EntityManager mocked, but it is not used anywhere.

You can declare your bean as a member of the testclass and annotate it with @InjectMocks to have Mockito do the wiring for you.

See also the documentation for more info and examples.

Michael Heß
  • 161
  • 6