Nuxeo Core Developer Guide

Testing a Datasource

Updated: January 18, 2019 Page Information Edit on GitHub

To use a datasource from a unit test, you need to set up the datasource from the unit test itself.

The nuxeo-runtime-datasource bundle provides some framework for you to register datasource in JNDI through Nuxeo contributions instead of relying on a container like Tomcat to do it for you.

Depending on Nuxeo-Runtime-Datasource and a Few Others

In your POM, add dependencies to:

  • org.nuxeo.runtime:nuxeo-runtime-datasource
  • org.nuxeo.runtime:nuxeo-runtime-test
  • org.nuxeo.runtime:nuxeo-runtime-jtajca
  • org.nuxeo.ecm.core:nuxeo-core-storage-sql-test

Use Nuxeo IDE "Synchronize POM" feature to properly update your POM with all required dependencies, without relying on uncertain transitive dependency.

Contributing a Datasource Definition

You first need to define your datasource. For instance in the file OSGI-INF/datasource-contrib.xml add:

<?xml version="1.0"?>
<component name="org.nuxeo.project.sample.datasource.test">
  <extension target="org.nuxeo.runtime.datasource" point="datasources">
    <datasource name="jdbc/foo" driverClassName="org.h2.Driver"
      maxActive="20" maxIdle="5">
      <property name="url">jdbc:h2:${ds.test.home}/db</property>

Deploying the Datasource from the Unit Test Setup

As our example datasource contains a variable for the storage path itself, we initialize this variable from the test code before reading the contribution. Of course you could hardcode things if you prefer.

Using JUnit4 and the Nuxeo FeaturesRunner

You may want to use a Feature. See

import org.nuxeo.ecm.core.test.CoreFeature;
import org.nuxeo.ecm.core.test.TransactionalFeature;
import org.nuxeo.runtime.test.runner.Deploy;
import org.nuxeo.runtime.test.runner.Features;
import org.nuxeo.runtime.test.runner.FeaturesRunner;
import org.nuxeo.runtime.test.runner.LocalDeploy;
import org.nuxeo.runtime.test.runner.SimpleFeature;

@Features({ TransactionalFeature.class, CoreFeature.class })
@Deploy({ "org.nuxeo.runtime.datasource", "org.nuxeo.project.sample.tests" })
public class DatasourceFeature extends SimpleFeature {

    private static final String DIRECTORY = "target/test/h2";

    private static final String PROP_NAME = "ds.test.home";

    private File dir;

    private String oldValue;

    public void initialize(FeaturesRunner runner) throws Exception {
        dir = new File(DIRECTORY);
        oldValue = System.setProperty(PROP_NAME, dir.getPath());

    public void stop(FeaturesRunner runner) throws Exception {
        if (oldValue != null) {
            System.setProperty(PROP_NAME, oldValue);

Using the Old SQLRepositoryTestCase

This is not supported anymore since Nuxeo 7.3.

import java.sql.Connection;
import javax.sql.DataSource;
import org.junit.Test;
import org.nuxeo.runtime.api.DataSourceHelper;
import org.nuxeo.runtime.api.Framework;
import org.nuxeo.runtime.jtajca.NuxeoContainer;

public class TestDatasourceJUnit3 extends SQLRepositoryTestCase {
    private static final String DIRECTORY = "target/test/h2";

    private static final String PROP_NAME = "ds.test.home";

    public void setUp() throws Exception {
        // Required since 5.6
        File dir = new File(DIRECTORY);
        Framework.getProperties().put(PROP_NAME, dir.getPath());
        // Required since 5.7

    public void tearDown() throws Exception {
        // Required since 5.6
        if (NuxeoContainer.isInstalled()) {

Using the Datasource in the Test


public void test() throws Exception {
    DataSource ds = DataSourceHelper.getDataSource("foo");
    Connection conn = ds.getConnection();
    try {
        // ... use the connection ...
    } finally {

a month ago Loubna Benzaama Update src/studio/nuxeo-studio/administering-your-project/
3 years ago Bertrand Chauvin 16 | emoved link - does not match code in do
3 years ago Bertrand Chauvin 15 | Updated link
3 years ago Solen Guitter 14
3 years ago Solen Guitter 13 | Fix TOC
4 years ago Solen Guitter 12
4 years ago Florent Guillaume 11
6 years ago Julien Carsique 10
6 years ago Julien Carsique 9
6 years ago Julien Carsique 8 | update datasource test sample following 5.6 changes
6 years ago Julien Carsique 7
6 years ago Julien Carsique 6
7 years ago Thierry Martins 4
7 years ago Thierry Martins 5 | Migrated to Confluence 4.0
8 years ago Florent Guillaume 3
8 years ago Florent Guillaume 2
8 years ago Florent Guillaume 1
History: Created by Florent Guillaume

We'd love to hear your thoughts!

All fields required