Can Mocktio mock a persistence provider, so that jenkins (or other pipeline tools) can execute JUnit tests without a database?
So I wrote JUnit tests, including Mockito, for an API that directly uses DAOs. The mocking of the EntityManager
and EntityTransaction
is no problem and the tests are running fine on a machine, where the persistence provider can connect to the database. (EclipseLink is used)
Jenkins however, which is also running these tests, has no access to the database in question.
Everytime Jenkins executes the tests I get:
No Persistence provider for EntityManager named XYZ
I do understand that this occurs due to the fact that jenkins can not establish a real connection to the database. This behavior is intentional and should not change.
My question therefore is: is it possible to mock (with Mockito?), or in antoher way fake, the connection so that a fake EntityManagerFactory
/ persistence provider can be used?
java jenkins junit mockito eclipselink
add a comment |
So I wrote JUnit tests, including Mockito, for an API that directly uses DAOs. The mocking of the EntityManager
and EntityTransaction
is no problem and the tests are running fine on a machine, where the persistence provider can connect to the database. (EclipseLink is used)
Jenkins however, which is also running these tests, has no access to the database in question.
Everytime Jenkins executes the tests I get:
No Persistence provider for EntityManager named XYZ
I do understand that this occurs due to the fact that jenkins can not establish a real connection to the database. This behavior is intentional and should not change.
My question therefore is: is it possible to mock (with Mockito?), or in antoher way fake, the connection so that a fake EntityManagerFactory
/ persistence provider can be used?
java jenkins junit mockito eclipselink
Are you mocking the Entity Manager while sill connecting to the real database? Does it mean that you don't actually run queries against the database while testing but build the persistence unit anyway?
– briadeus
Nov 14 '18 at 13:13
That describes the problem quite well. The unit that is testes uses DAOs. For this the constructor of the API connects to the database and instantiates an EntityManager.
– Felix
Nov 14 '18 at 15:30
1
You have two options: 1) refactor your dependencies and use simple unit tests to test what should be tested (e.g. no database access) 2) configure some in memory database (like Derby), fill it with test data, initialize PU and run tests against it (if this is what is supposed to be tested).
– briadeus
Nov 14 '18 at 17:48
I think I'm going for the latter, since the API design is approved the way it is right now. Thanks for the hints.
– Felix
Nov 15 '18 at 9:07
We are using the spring container in couple of projects, you can find a simple example here: logicbig.com/tutorials/spring-framework/spring-core/…
– briadeus
Nov 15 '18 at 9:46
add a comment |
So I wrote JUnit tests, including Mockito, for an API that directly uses DAOs. The mocking of the EntityManager
and EntityTransaction
is no problem and the tests are running fine on a machine, where the persistence provider can connect to the database. (EclipseLink is used)
Jenkins however, which is also running these tests, has no access to the database in question.
Everytime Jenkins executes the tests I get:
No Persistence provider for EntityManager named XYZ
I do understand that this occurs due to the fact that jenkins can not establish a real connection to the database. This behavior is intentional and should not change.
My question therefore is: is it possible to mock (with Mockito?), or in antoher way fake, the connection so that a fake EntityManagerFactory
/ persistence provider can be used?
java jenkins junit mockito eclipselink
So I wrote JUnit tests, including Mockito, for an API that directly uses DAOs. The mocking of the EntityManager
and EntityTransaction
is no problem and the tests are running fine on a machine, where the persistence provider can connect to the database. (EclipseLink is used)
Jenkins however, which is also running these tests, has no access to the database in question.
Everytime Jenkins executes the tests I get:
No Persistence provider for EntityManager named XYZ
I do understand that this occurs due to the fact that jenkins can not establish a real connection to the database. This behavior is intentional and should not change.
My question therefore is: is it possible to mock (with Mockito?), or in antoher way fake, the connection so that a fake EntityManagerFactory
/ persistence provider can be used?
java jenkins junit mockito eclipselink
java jenkins junit mockito eclipselink
asked Nov 14 '18 at 10:08
FelixFelix
813
813
Are you mocking the Entity Manager while sill connecting to the real database? Does it mean that you don't actually run queries against the database while testing but build the persistence unit anyway?
– briadeus
Nov 14 '18 at 13:13
That describes the problem quite well. The unit that is testes uses DAOs. For this the constructor of the API connects to the database and instantiates an EntityManager.
– Felix
Nov 14 '18 at 15:30
1
You have two options: 1) refactor your dependencies and use simple unit tests to test what should be tested (e.g. no database access) 2) configure some in memory database (like Derby), fill it with test data, initialize PU and run tests against it (if this is what is supposed to be tested).
– briadeus
Nov 14 '18 at 17:48
I think I'm going for the latter, since the API design is approved the way it is right now. Thanks for the hints.
– Felix
Nov 15 '18 at 9:07
We are using the spring container in couple of projects, you can find a simple example here: logicbig.com/tutorials/spring-framework/spring-core/…
– briadeus
Nov 15 '18 at 9:46
add a comment |
Are you mocking the Entity Manager while sill connecting to the real database? Does it mean that you don't actually run queries against the database while testing but build the persistence unit anyway?
– briadeus
Nov 14 '18 at 13:13
That describes the problem quite well. The unit that is testes uses DAOs. For this the constructor of the API connects to the database and instantiates an EntityManager.
– Felix
Nov 14 '18 at 15:30
1
You have two options: 1) refactor your dependencies and use simple unit tests to test what should be tested (e.g. no database access) 2) configure some in memory database (like Derby), fill it with test data, initialize PU and run tests against it (if this is what is supposed to be tested).
– briadeus
Nov 14 '18 at 17:48
I think I'm going for the latter, since the API design is approved the way it is right now. Thanks for the hints.
– Felix
Nov 15 '18 at 9:07
We are using the spring container in couple of projects, you can find a simple example here: logicbig.com/tutorials/spring-framework/spring-core/…
– briadeus
Nov 15 '18 at 9:46
Are you mocking the Entity Manager while sill connecting to the real database? Does it mean that you don't actually run queries against the database while testing but build the persistence unit anyway?
– briadeus
Nov 14 '18 at 13:13
Are you mocking the Entity Manager while sill connecting to the real database? Does it mean that you don't actually run queries against the database while testing but build the persistence unit anyway?
– briadeus
Nov 14 '18 at 13:13
That describes the problem quite well. The unit that is testes uses DAOs. For this the constructor of the API connects to the database and instantiates an EntityManager.
– Felix
Nov 14 '18 at 15:30
That describes the problem quite well. The unit that is testes uses DAOs. For this the constructor of the API connects to the database and instantiates an EntityManager.
– Felix
Nov 14 '18 at 15:30
1
1
You have two options: 1) refactor your dependencies and use simple unit tests to test what should be tested (e.g. no database access) 2) configure some in memory database (like Derby), fill it with test data, initialize PU and run tests against it (if this is what is supposed to be tested).
– briadeus
Nov 14 '18 at 17:48
You have two options: 1) refactor your dependencies and use simple unit tests to test what should be tested (e.g. no database access) 2) configure some in memory database (like Derby), fill it with test data, initialize PU and run tests against it (if this is what is supposed to be tested).
– briadeus
Nov 14 '18 at 17:48
I think I'm going for the latter, since the API design is approved the way it is right now. Thanks for the hints.
– Felix
Nov 15 '18 at 9:07
I think I'm going for the latter, since the API design is approved the way it is right now. Thanks for the hints.
– Felix
Nov 15 '18 at 9:07
We are using the spring container in couple of projects, you can find a simple example here: logicbig.com/tutorials/spring-framework/spring-core/…
– briadeus
Nov 15 '18 at 9:46
We are using the spring container in couple of projects, you can find a simple example here: logicbig.com/tutorials/spring-framework/spring-core/…
– briadeus
Nov 15 '18 at 9:46
add a comment |
1 Answer
1
active
oldest
votes
A way to test the closed API without a real database access is to build and fill & tear-down an in-memory database for tests. A simple setup with Spring & Derby for Eclipselink would be
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@ContextConfiguration(classes = AppConfig.class)
@TransactionConfiguration(defaultRollback = false)
public abstract class AbstractContainer
@PersistenceUnit(unitName = "PERSISTENT_UNIT_NAME")
protected EntityManagerFactory factory;
@Autowired
protected ApplicationContext applicationContext;
and
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import java.util.Properties;
import javax.sql.DataSource;
@Configuration
public class AppConfig
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
LocalContainerEntityManagerFactoryBean em =
new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPersistenceUnitName("PERSISTENT_UNIT_NAME");
em.setPackagesToScan(new String "package.name.to.scan" );
JpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
// here some additional properties for the PU
em.setJpaProperties(additionalProperties());
return em;
Properties additionalProperties()
Properties properties = new Properties();
properties.setProperty("eclipselink.weaving", "false");
properties.setProperty("eclipselink.query-results-cache", "false");
properties.setProperty("eclipselink.cache.shared.default", "false");
properties.setProperty("javax.persistence.jdbc.driver",
"org.apache.derby.jdbc.EmbeddedDriver");
properties.setProperty("javax.persistence.jdbc.url",
"jdbc:derby:memory:NAME;create=true");
properties.setProperty("javax.persistence.jdbc.url", "jdbc:derby:PATH");
properties.setProperty("javax.persistence.jdbc.user", "");
properties.setProperty("javax.persistence.jdbc.password", "");
properties.setProperty("javax.persistence.sql-load-script-source",
"META-INF/sql/createDB.sql");
properties.setProperty("eclipselink.deploy-on-startup", "true");
properties.setProperty("eclipselink.target-database", "Derby");
return properties;
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try
Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
catch (ClassNotFoundException cnfe)
cnfe.printStackTrace();
catch (InstantiationException inste)
inste.printStackTrace();
catch (IllegalAccessException iace)
iace.printStackTrace();
dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
dataSource.setUrl("jdbc:derby:memory:NAME;create=true");
dataSource.setUsername("");
dataSource.setPassword("");
return dataSource;
The SQL Script lies under test/resources/META-INF/sql/createDB.sql.
Finally your Test Class extends the Abstract Container using the SpringJUnit4ClassRunner.class Runner and starts a local transaction.
@RunWith(SpringJUnit4ClassRunner.class)
public class DAOTest extends AbstractContainer
@Test
public void testDAO()
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
The persistence unit for tests is per default expected under test/resources/META-INF/.
Dependencies using Maven
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>[2.0.8]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297632%2fcan-mocktio-mock-a-persistence-provider-so-that-jenkins-or-other-pipeline-tool%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A way to test the closed API without a real database access is to build and fill & tear-down an in-memory database for tests. A simple setup with Spring & Derby for Eclipselink would be
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@ContextConfiguration(classes = AppConfig.class)
@TransactionConfiguration(defaultRollback = false)
public abstract class AbstractContainer
@PersistenceUnit(unitName = "PERSISTENT_UNIT_NAME")
protected EntityManagerFactory factory;
@Autowired
protected ApplicationContext applicationContext;
and
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import java.util.Properties;
import javax.sql.DataSource;
@Configuration
public class AppConfig
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
LocalContainerEntityManagerFactoryBean em =
new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPersistenceUnitName("PERSISTENT_UNIT_NAME");
em.setPackagesToScan(new String "package.name.to.scan" );
JpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
// here some additional properties for the PU
em.setJpaProperties(additionalProperties());
return em;
Properties additionalProperties()
Properties properties = new Properties();
properties.setProperty("eclipselink.weaving", "false");
properties.setProperty("eclipselink.query-results-cache", "false");
properties.setProperty("eclipselink.cache.shared.default", "false");
properties.setProperty("javax.persistence.jdbc.driver",
"org.apache.derby.jdbc.EmbeddedDriver");
properties.setProperty("javax.persistence.jdbc.url",
"jdbc:derby:memory:NAME;create=true");
properties.setProperty("javax.persistence.jdbc.url", "jdbc:derby:PATH");
properties.setProperty("javax.persistence.jdbc.user", "");
properties.setProperty("javax.persistence.jdbc.password", "");
properties.setProperty("javax.persistence.sql-load-script-source",
"META-INF/sql/createDB.sql");
properties.setProperty("eclipselink.deploy-on-startup", "true");
properties.setProperty("eclipselink.target-database", "Derby");
return properties;
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try
Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
catch (ClassNotFoundException cnfe)
cnfe.printStackTrace();
catch (InstantiationException inste)
inste.printStackTrace();
catch (IllegalAccessException iace)
iace.printStackTrace();
dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
dataSource.setUrl("jdbc:derby:memory:NAME;create=true");
dataSource.setUsername("");
dataSource.setPassword("");
return dataSource;
The SQL Script lies under test/resources/META-INF/sql/createDB.sql.
Finally your Test Class extends the Abstract Container using the SpringJUnit4ClassRunner.class Runner and starts a local transaction.
@RunWith(SpringJUnit4ClassRunner.class)
public class DAOTest extends AbstractContainer
@Test
public void testDAO()
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
The persistence unit for tests is per default expected under test/resources/META-INF/.
Dependencies using Maven
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>[2.0.8]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
add a comment |
A way to test the closed API without a real database access is to build and fill & tear-down an in-memory database for tests. A simple setup with Spring & Derby for Eclipselink would be
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@ContextConfiguration(classes = AppConfig.class)
@TransactionConfiguration(defaultRollback = false)
public abstract class AbstractContainer
@PersistenceUnit(unitName = "PERSISTENT_UNIT_NAME")
protected EntityManagerFactory factory;
@Autowired
protected ApplicationContext applicationContext;
and
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import java.util.Properties;
import javax.sql.DataSource;
@Configuration
public class AppConfig
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
LocalContainerEntityManagerFactoryBean em =
new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPersistenceUnitName("PERSISTENT_UNIT_NAME");
em.setPackagesToScan(new String "package.name.to.scan" );
JpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
// here some additional properties for the PU
em.setJpaProperties(additionalProperties());
return em;
Properties additionalProperties()
Properties properties = new Properties();
properties.setProperty("eclipselink.weaving", "false");
properties.setProperty("eclipselink.query-results-cache", "false");
properties.setProperty("eclipselink.cache.shared.default", "false");
properties.setProperty("javax.persistence.jdbc.driver",
"org.apache.derby.jdbc.EmbeddedDriver");
properties.setProperty("javax.persistence.jdbc.url",
"jdbc:derby:memory:NAME;create=true");
properties.setProperty("javax.persistence.jdbc.url", "jdbc:derby:PATH");
properties.setProperty("javax.persistence.jdbc.user", "");
properties.setProperty("javax.persistence.jdbc.password", "");
properties.setProperty("javax.persistence.sql-load-script-source",
"META-INF/sql/createDB.sql");
properties.setProperty("eclipselink.deploy-on-startup", "true");
properties.setProperty("eclipselink.target-database", "Derby");
return properties;
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try
Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
catch (ClassNotFoundException cnfe)
cnfe.printStackTrace();
catch (InstantiationException inste)
inste.printStackTrace();
catch (IllegalAccessException iace)
iace.printStackTrace();
dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
dataSource.setUrl("jdbc:derby:memory:NAME;create=true");
dataSource.setUsername("");
dataSource.setPassword("");
return dataSource;
The SQL Script lies under test/resources/META-INF/sql/createDB.sql.
Finally your Test Class extends the Abstract Container using the SpringJUnit4ClassRunner.class Runner and starts a local transaction.
@RunWith(SpringJUnit4ClassRunner.class)
public class DAOTest extends AbstractContainer
@Test
public void testDAO()
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
The persistence unit for tests is per default expected under test/resources/META-INF/.
Dependencies using Maven
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>[2.0.8]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
add a comment |
A way to test the closed API without a real database access is to build and fill & tear-down an in-memory database for tests. A simple setup with Spring & Derby for Eclipselink would be
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@ContextConfiguration(classes = AppConfig.class)
@TransactionConfiguration(defaultRollback = false)
public abstract class AbstractContainer
@PersistenceUnit(unitName = "PERSISTENT_UNIT_NAME")
protected EntityManagerFactory factory;
@Autowired
protected ApplicationContext applicationContext;
and
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import java.util.Properties;
import javax.sql.DataSource;
@Configuration
public class AppConfig
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
LocalContainerEntityManagerFactoryBean em =
new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPersistenceUnitName("PERSISTENT_UNIT_NAME");
em.setPackagesToScan(new String "package.name.to.scan" );
JpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
// here some additional properties for the PU
em.setJpaProperties(additionalProperties());
return em;
Properties additionalProperties()
Properties properties = new Properties();
properties.setProperty("eclipselink.weaving", "false");
properties.setProperty("eclipselink.query-results-cache", "false");
properties.setProperty("eclipselink.cache.shared.default", "false");
properties.setProperty("javax.persistence.jdbc.driver",
"org.apache.derby.jdbc.EmbeddedDriver");
properties.setProperty("javax.persistence.jdbc.url",
"jdbc:derby:memory:NAME;create=true");
properties.setProperty("javax.persistence.jdbc.url", "jdbc:derby:PATH");
properties.setProperty("javax.persistence.jdbc.user", "");
properties.setProperty("javax.persistence.jdbc.password", "");
properties.setProperty("javax.persistence.sql-load-script-source",
"META-INF/sql/createDB.sql");
properties.setProperty("eclipselink.deploy-on-startup", "true");
properties.setProperty("eclipselink.target-database", "Derby");
return properties;
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try
Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
catch (ClassNotFoundException cnfe)
cnfe.printStackTrace();
catch (InstantiationException inste)
inste.printStackTrace();
catch (IllegalAccessException iace)
iace.printStackTrace();
dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
dataSource.setUrl("jdbc:derby:memory:NAME;create=true");
dataSource.setUsername("");
dataSource.setPassword("");
return dataSource;
The SQL Script lies under test/resources/META-INF/sql/createDB.sql.
Finally your Test Class extends the Abstract Container using the SpringJUnit4ClassRunner.class Runner and starts a local transaction.
@RunWith(SpringJUnit4ClassRunner.class)
public class DAOTest extends AbstractContainer
@Test
public void testDAO()
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
The persistence unit for tests is per default expected under test/resources/META-INF/.
Dependencies using Maven
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>[2.0.8]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
A way to test the closed API without a real database access is to build and fill & tear-down an in-memory database for tests. A simple setup with Spring & Derby for Eclipselink would be
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@ContextConfiguration(classes = AppConfig.class)
@TransactionConfiguration(defaultRollback = false)
public abstract class AbstractContainer
@PersistenceUnit(unitName = "PERSISTENT_UNIT_NAME")
protected EntityManagerFactory factory;
@Autowired
protected ApplicationContext applicationContext;
and
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import java.util.Properties;
import javax.sql.DataSource;
@Configuration
public class AppConfig
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
LocalContainerEntityManagerFactoryBean em =
new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPersistenceUnitName("PERSISTENT_UNIT_NAME");
em.setPackagesToScan(new String "package.name.to.scan" );
JpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
// here some additional properties for the PU
em.setJpaProperties(additionalProperties());
return em;
Properties additionalProperties()
Properties properties = new Properties();
properties.setProperty("eclipselink.weaving", "false");
properties.setProperty("eclipselink.query-results-cache", "false");
properties.setProperty("eclipselink.cache.shared.default", "false");
properties.setProperty("javax.persistence.jdbc.driver",
"org.apache.derby.jdbc.EmbeddedDriver");
properties.setProperty("javax.persistence.jdbc.url",
"jdbc:derby:memory:NAME;create=true");
properties.setProperty("javax.persistence.jdbc.url", "jdbc:derby:PATH");
properties.setProperty("javax.persistence.jdbc.user", "");
properties.setProperty("javax.persistence.jdbc.password", "");
properties.setProperty("javax.persistence.sql-load-script-source",
"META-INF/sql/createDB.sql");
properties.setProperty("eclipselink.deploy-on-startup", "true");
properties.setProperty("eclipselink.target-database", "Derby");
return properties;
public DataSource dataSource()
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try
Class.forName("org.eclipse.persistence.jpa.PersistenceProvider");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
catch (ClassNotFoundException cnfe)
cnfe.printStackTrace();
catch (InstantiationException inste)
inste.printStackTrace();
catch (IllegalAccessException iace)
iace.printStackTrace();
dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
dataSource.setUrl("jdbc:derby:memory:NAME;create=true");
dataSource.setUsername("");
dataSource.setPassword("");
return dataSource;
The SQL Script lies under test/resources/META-INF/sql/createDB.sql.
Finally your Test Class extends the Abstract Container using the SpringJUnit4ClassRunner.class Runner and starts a local transaction.
@RunWith(SpringJUnit4ClassRunner.class)
public class DAOTest extends AbstractContainer
@Test
public void testDAO()
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
The persistence unit for tests is per default expected under test/resources/META-INF/.
Dependencies using Maven
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>[4.1.7.RELEASE]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>[2.0.8]</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.11.1.1</version>
<scope>test</scope>
</dependency>
answered Nov 16 '18 at 8:29
briadeusbriadeus
43448
43448
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297632%2fcan-mocktio-mock-a-persistence-provider-so-that-jenkins-or-other-pipeline-tool%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you mocking the Entity Manager while sill connecting to the real database? Does it mean that you don't actually run queries against the database while testing but build the persistence unit anyway?
– briadeus
Nov 14 '18 at 13:13
That describes the problem quite well. The unit that is testes uses DAOs. For this the constructor of the API connects to the database and instantiates an EntityManager.
– Felix
Nov 14 '18 at 15:30
1
You have two options: 1) refactor your dependencies and use simple unit tests to test what should be tested (e.g. no database access) 2) configure some in memory database (like Derby), fill it with test data, initialize PU and run tests against it (if this is what is supposed to be tested).
– briadeus
Nov 14 '18 at 17:48
I think I'm going for the latter, since the API design is approved the way it is right now. Thanks for the hints.
– Felix
Nov 15 '18 at 9:07
We are using the spring container in couple of projects, you can find a simple example here: logicbig.com/tutorials/spring-framework/spring-core/…
– briadeus
Nov 15 '18 at 9:46