Table of Contents | ||
---|---|---|
|
Introduction
This document describes the technical aspects of the testing integrated into Dspace. In it we describe the tools used as well as how to use them and the solutions applied to some issues found during development. It's intended to serve as a reference for the community so more test cases can be created.
...
To run the tests:
Code Block |
---|
mvn package -Dmaven.test.skip=false //builds DSpace and runs tests
or
mvn test -Dmaven.test.skip=false //just runs the tests
|
...
To run the tests execute:
Code Block |
---|
mvn test
|
The tests will also be run during a normal Maven build cycle. To skip the tests, run Maven like:
Code Block |
---|
mvn package -Dmaven.test.skip=true
|
By default we will disable running the tests, as they might slow the compilation cycle for developers. They can be activated using the command
Code Block |
---|
mvn package -Dmaven.test.skip=false
|
...
There are some common imports and structure, you can use the following code as a template:
Code Block |
---|
//Add DSpace licensing here at the top!
package org.dspace.content;
import java.sql.SQLException;
import org.dspace.core.Context;
import org.junit.*;
import static org.junit.Assert.* ;
import static org.hamcrest.CoreMatchers.*;
import mockit.*;
import org.apache.log4j.Logger;
import org.dspace.core.Constants;
/**
* Unit Tests for class <OriginalClass>Test
* @author you name
*/
public class <OriginalClass>Test extends AbstractUnitTest
{
/** log4j category */
private static final Logger log = Logger.getLogger(<OriginalClass>Test.class);
/**
* <OriginalClass> instance for the tests
*/
private <OriginalClass> c;
/**
* This method will be run before every test as per @Before. It will
* initialize resources required for the tests.
*
* Other methods can be annotated with @Before here or in subclasses
* but no execution order is guaranteed
*/
@Before
@Override
public void init()
{
super.init();
try
{
//we have to create a new community in the database
context.turnOffAuthorisationSystem();
this.c = <OriginalClass>.create(null, context);
//we need to commit the changes so we don't block the table for testing
context.restoreAuthSystemState();
context.commit();
}
catch (AuthorizeException ex)
{
log.error("Authorization Error in init", ex);
fail("Authorization Error in init");
}
catch (SQLException ex)
{
log.error("SQL Error in init", ex);
fail("SQL Error in init");
}
}
/**
* This method will be run after every test as per @After. It will
* clean resources initialized by the @Before methods.
*
* Other methods can be annotated with @After here or in subclasses
* but no execution order is guaranteed
*/
@After
@Override
public void destroy()
{
c = null;
super.destroy();
}
/**
* Test of XXXX method, of class <OriginalClass>
*/
@Test
public void testXXXX() throws Exception
{
int id = c.getID();
<OriginalClass> found = <OriginalClass>.find(context, id);
assertThat("testXXXX 0", found, notNullValue());
assertThat("testXXXX 1", found.getID(), equalTo(id));
assertThat("testXXXX 2", found.getName(), equalTo(""));
}
[... more tests ...]
}
|
...
The tests can be activated using the commands
Code Block |
---|
mvn package -Dmaven.test.skip=false //builds DSpace and runs tests
or
mvn test -Dmaven.test.skip=false //just runs the tests
|
or by changing the property "activeByDefault" at the profile (skiptests) in the main pom.xml file, at the root of the project and then running
Code Block |
---|
mvn package //builds DSpace and runs tests
or
mvn test //just runs the tests
|
Be aware that this command will launch both unit and integration tests.
Integration Tests
How to skip a test
It can occasionally happen that a commit to the master branch breaks a test. Such commit should be reverted ASAP, but sometimes it's just not possible/viable. What to do if a test is failing and you can't fix it? You can still run the other tests and temporarily disable the failing test using the Surefire exclusions:
In [dspace-src]/pom.xml, add a line excluding the test class (in this case "DSpaceServiceManagerTest"):
Code Block | ||
---|---|---|
| ||
...
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<!-- tests whose name starts by Abstract will be ignored -->
<configuration>
<excludes>
<exclude>**/Abstract*</exclude>
<exclude>**/DSpaceServiceManagerTest*</exclude>
</excludes>
... |
Integration Tests
These tests work at the API level and test the interaction of components within the system. Some examples are placing an item into a collection or creating a These tests work at the API level and test the interaction of components within the system. Some examples are placing an item into a collection or creating a new metadata schema and adding some fields. Primarily these tests operate at the API level ignoring the interface components above it.
...
There are some common imports and structure, you can use the following code as a template:
Code Block |
---|
//Add DSpace licensing here at the top!
package org.dspace.content;
import java.sql.SQLException;
import org.dspace.core.Context;
import org.junit.*;
import static org.junit.Assert.* ;
import static org.hamcrest.CoreMatchers.*;
import mockit.*;
import org.apache.log4j.Logger;
import org.dspace.core.Constants;
/**
* This is an integration test to validate the metadata classes
* @author pvillega
*/
public class MetadataIntegrationTest extends AbstractIntegrationTest
{
/** log4j category */
private static final Logger log = Logger.getLogger(MetadataIntegrationTest.class);
/**
* This method will be run before every test as per @Before. It will
* initialize resources required for the tests.
*
* Other methods can be annotated with @Before here or in subclasses
* but no execution order is guaranteed
*/
@Before
@Override
public void init()
{
super.init();
}
/**
* This method will be run after every test as per @After. It will
* clean resources initialized by the @Before methods.
*
* Other methods can be annotated with @After here or in subclasses
* but no execution order is guaranteed
*/
@After
@Override
public void destroy()
{
super.destroy();
}
/**
* Tests the creation of a new metadata schema with some values
*/
@Test
@PerfTest(invocations = 50, threads = 1)
@Required(percentile95 = 500, average= 200)
public void testCreateSchema() throws SQLException, AuthorizeException, NonUniqueMetadataException, IOException
{
String schemaName = "integration";
//we create the structure
context.turnOffAuthorisationSystem();
Item it = Item.create(context);
MetadataSchema schema = new MetadataSchema("htpp://test/schema/", schemaName);
schema.create(context);
[...]
//commit to free locks on tables
context.commit();
//verify it works as expected
assertThat("testCreateSchema 0", schema.getName(), equalTo(schemaName));
assertThat("testCreateSchema 1", field1.getSchemaID(), equalTo(schema.getSchemaID()));
assertThat("testCreateSchema 2", field2.getSchemaID(), equalTo(schema.getSchemaID())); [...]
//clean database
value1.delete(context);
[...]
context.restoreAuthSystemState();
context.commit();
}
}
|
...
The tests can be activated using the commands
Code Block |
---|
mvn package -Dmaven.test.skip=false //builds DSpace and runs tests
or
mvn test -Dmaven.test.skip=false //just runs the tests
|
or by changing the property "activeByDefault" at the profile (skiptests) in the main pom.xml file, at the root of the project and then running
Code Block |
---|
mvn package //builds DSpace and runs tests
or
mvn test //just runs the tests
|
...
The reports can be generated by launching:
Code Block |
---|
mvn site
|
from the main folder. Be aware this will take a long time, probably more than 20 minutes.