ActiveObjectsJUnitRunner - EntityManager is not injected into tests.
Asked by Plamen Petkov:

Hello everyone.  
I am developing a plugin for Confluence and following this guide <https://developer.atlassian.com/server/framework/atlassian-sdk/getting-started-with-active-objects/>, but I am having trouble with testing.  
The problem is that the ActiveObjectsJUnitRunner.class class doesn't inject EntityManager and throw NullPointerException at setUp () {} method. According to this guide ActiveObjectJUnitRunner.class should inject EntityManager.  
Surely I do something wrong or something remains to be done.

Thanks in advance of your help.

<br />

Those are my pom test dependencies:

```
<!-- WIRED TEST RUNNER DEPENDENCIES -->
<dependency>
    <groupId>com.atlassian.plugins</groupId>
    <artifactId>atlassian-plugins-osgi-testrunner</artifactId>
    <version>${plugin.testrunner.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>net.java.dev.activeobjects</groupId>
    <artifactId>activeobjects-test</artifactId>
    <version>${ao.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.atlassian.activeobjects</groupId>
    <artifactId>activeobjects-test</artifactId>
    <version>${ao.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.5.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.6.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>1.8.0.10</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.8.5</version>
    <scope>test</scope>
</dependency>
```

and that is my test class:

```
package com.cis.confluence.plugins.persistence;

import com.atlassian.activeobjects.test.TestActiveObjects;
import net.java.ao.EntityManager;
import net.java.ao.test.jdbc.Data;
import net.java.ao.test.jdbc.DatabaseUpdater;
import net.java.ao.test.junit.ActiveObjectsJUnitRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import static org.junit.jupiter.api.Assertions.*;


@RunWith(ActiveObjectsJUnitRunner.class)
@Data(ConfluencerPersistenceImplTest.ConfluencerPersistenceImplTestDatabaseUpdater.class)
//@Jdbc(DerbyEmbedded.class)
//@NameConverters
class ConfluencerPersistenceImplTest {

    private ConfluencerPersistenceImpl objectToTest;

    private EntityManager entityManager;

    @Test
    void getAll() {
    }

    @Test
    void save() {
    }

    @Test
    void remove() {
    }

    @BeforeEach
    public void setUp() {
        assertNotNull(entityManager);
        objectToTest = new ConfluencerPersistenceImpl(new TestActiveObjects(entityManager));

    }

    public static final class ConfluencerPersistenceImplTestDatabaseUpdater implements DatabaseUpdater {
        @Override
        public void update(EntityManager entityManager) throws Exception {
            entityManager.migrate(EventUserServ.class);
        }
    }
}
```

the error message:

```
org.opentest4j.AssertionFailedError: expected: not <null>
```


Answers:
author: victor_martinez
body: shouldnt this be an issue?


author: Plamen Georgiev Petkov
body: If someone have this problem , here is a solution. (although this should not be a solution, if there is a new version of junit)  
I make it work with downgrading of junit. I have no problems with junit4 (org.junit.\*), EntityManager was injected by ActiveObjectsJUnitRunner, but I don't understand why with junit5 (org.junit.jupiter.api.\*) EntityManager is not injected.  
Is there another way to test persistence with JUnit5?  
Is there another way to inject the EntityManager with JUnit5 ?  
All my tests are writed on junit5 , except this one.

worked test class:

```
package com.cis.confluence.plugins.persistence;

import com.atlassian.activeobjects.external.ActiveObjects;
import com.atlassian.activeobjects.test.TestActiveObjects;
import net.java.ao.EntityManager;
import net.java.ao.test.junit.ActiveObjectsJUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.jupiter.api.Assertions.*;


@RunWith(ActiveObjectsJUnitRunner.class)
public class ConfluencerPersistenceImplTest {

    private ConfluencerPersistenceImpl objectToTest;

    private EntityManager entityManager;

    private ActiveObjects ao;

//changed from org.junit.jupiter.api.Test to org.junit.Test
    @Test
    public void getAll() {

    }

//changed from org.junit.jupiter.api.Test to org.junit.Test
    @Test
    public void save() {
    }

//changed from org.junit.jupiter.api.Test to org.junit.Test
    @Test
    public void remove() {
    }

//changed from org.junit.jupiter.api.BeforeEach to org.junit.Before
    @Before
    public void setUp() {
        assertNotNull(entityManager);

        ao = new TestActiveObjects(entityManager);
        objectToTest = new ConfluencerPersistenceImpl(ao);
    }  
}
```

comments: author: Vraj patel
body: Thanks


author: Vraj patel
body: .


author: Vraj patel
body: Hello   
but it's not solved,throws same error..

i shared you screen-shot of error message along with code \& maven dependency..

maven dependency :

\<dependency\>  
\<groupId\>com.atlassian.plugins\</groupId\>  
\<artifactId\>atlassian-plugins-osgi-testrunner\</artifactId\>  
\<version\>1.2.3\</version\>  
\<scope\>test\</scope\>  
\</dependency\>

\<dependency\>  
\<groupId\>net.java.dev.activeobjects\</groupId\>  
\<artifactId\>activeobjects-test\</artifactId\>  
\<version\>${ao.version}\</version\>  
\<scope\>test\</scope\>  
\</dependency\>  
\<dependency\>  
\<groupId\>com.atlassian.activeobjects\</groupId\>  
\<artifactId\>activeobjects-test\</artifactId\>  
\<version\>${ao.version}\</version\>  
\<scope\>test\</scope\>  
\</dependency\>  
\<dependency\>  
\<groupId\>hsqldb\</groupId\>  
\<artifactId\>hsqldb\</artifactId\>  
\<version\>1.8.0.10\</version\>  
\<scope\>test\</scope\>  
\</dependency\>  
\<dependency\>  
\<groupId\>org.mockito\</groupId\>  
\<artifactId\>mockito-all\</artifactId\>  
\<version\>1.8.5\</version\>  
\<scope\>test\</scope\>  
\</dependency\>  
\<dependency\>  
\<groupId\>com.google.guava\</groupId\>  
\<artifactId\>guava\</artifactId\>  
\<version\>21.0\</version\>  
\<scope\>provided\</scope\>  
\</dependency\>

code :

import static org.junit.Assert.assertNotNull;

import org.junit.Before;  
import org.junit.Test;  
import org.junit.runner.RunWith;

import com.atlassian.activeobjects.external.ActiveObjects;  
import com.atlassian.activeobjects.test.TestActiveObjects;

import net.java.ao.EntityManager;  
import net.java.ao.test.junit.ActiveObjectsJUnitRunner;

import net.java.ao.test.jdbc.Data; //added  
import net.java.ao.test.jdbc.DatabaseUpdater; //added  
import net.java.ao.test.jdbc.Hsql;  
import net.java.ao.test.jdbc.Jdbc;

@RunWith(ActiveObjectsJUnitRunner.class)  
[@Data](/t5/user/viewprofilepage/user-id/2218648)(SentinelGlobalSettingsTest.SentinelGlobalSettingsTestDataBaseUpdater.class) // added  
@Jdbc(Hsql.class)  
public class SentinelGlobalSettingsTest {

private SentinelGlobalSettings sentinelGlobalSettings;  
private EntityManager entityManager;  
private ActiveObjects ao;

@Before  
public void setUp() {  
System.out.println("setUpClass call");  
assertNotNull(entityManager);  
ao = new TestActiveObjects(entityManager);  
System.out.println(ao);  
sentinelGlobalSettings = new SentinelGlobalSettings(ao);  
}

/\*\*  
\* added  
\*/  
public static final class SentinelGlobalSettingsTestDataBaseUpdater implements DatabaseUpdater {  
@Override  
public void update(EntityManager entityManager) throws Exception {  
// you should migrade your entity interface to make tests  
System.out.println("ok");  
entityManager.migrate(ActiveObject.class);

}  
}

[@TEST](/t5/user/viewprofilepage/user-id/663)  
public void testAdd() {  
System.out.println("testAdd");  
}

}

another interface :

<br />

import com.atlassian.activeobjects.external.ActiveObjects;

import net.java.ao.RawEntity;

public interface ActiveObject extends RawEntity\<ActiveObjects\>{

}![Error.png](https://community.atlassian.com/t5/image/serverpage/image-id/134950iC248903D0445B432/image-size/large?v=v2&px=999 "Error.png")


author: Vraj Patel
body: Thank you


Tags: confluence-server

server