Skip to content

Database Requirement Extension

jbdevstudioqajenkins edited this page Aug 19, 2016 · 51 revisions

Database requirement can help you utilize external database configuration (like JDBC string, driver location) in your tests. Steps are similar to the general custom requirement extension:

  • annotate tests and utilize requirement and it's configuration
  • create configuration xml file following xsd template
  • run test with given RedDeer configuration file

Test class annotation

package org.jboss.reddeer.snippet.test;

import org.jboss.reddeer.junit.requirement.inject.InjectRequirement;
import org.jboss.reddeer.junit.runner.RedDeerSuite;
import org.jboss.reddeer.requirements.db.DatabaseConfiguration;
import org.jboss.reddeer.requirements.db.DatabaseRequirement;
import org.jboss.reddeer.requirements.db.DatabaseRequirement.Database;
import org.junit.Test;
import org.junit.runner.RunWith;

@Database(name = "dbtest")
@RunWith(RedDeerSuite.class)
public class DatabaseTest {

	@InjectRequirement
	DatabaseRequirement dbRequirement;

	@Test
	public void testMyH2Database() {
		DatabaseConfiguration cfg = dbRequirement.getConfiguration();
		String jdbc = cfg.getJdbcString();
		// Here goes further logic
	}
}

source code

Example of Database configuration file

<?xml version="1.0" encoding="UTF-8"?>
<reddeer xmlns="http://www.jboss.org/NS/Req" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:db="http://www.jboss.org/NS/db-schema"
	xsi:schemaLocation="http://www.jboss.org/NS/Req http://www.jboss.org/schema/reddeer/v1/RedDeerSchema.xsd                             http://www.jboss.org/NS/db-schema http://www.jboss.org/schema/reddeer/DatabaseRequirement.xsd">
	<requirements>
		<db:database-requirement name="h2-sakila">
			<db:driverName>My H2 Driver</db:driverName>
			<db:driverType>Generic JDBC</db:driverType>
			<db:driverTypeVersion>1.0</db:driverTypeVersion>
			<db:driverPath>/opt/sakila-db/h2-1.3.161.jar</db:driverPath>
			<db:driverClass>org.h2.Driver</db:driverClass>
			<db:profileName>dbProfile</db:profileName>
			<db:jdbcString>jdbc:h2:db://localhost/sakila</db:jdbcString>
			<db:username>sa</db:username>
			<db:password></db:password>
		</db:database-requirement>
	</requirements>
</reddeer>

source code

Set location of RedDeer configuration

-Drd.config=/opt/rd-conf/

For more details you can follow more comprehensive tutorial article

Clone this wiki locally