Db4ounit Methods

Let's look through the basic API , which will help you to build your own test. This document is not a complete API reference and its intention is to give you a general idea of the methods usage and availability.

AbstractDb4oTestCase

AbstractDb4oTestCase is a base class for creating test cases.

private transient Db4oFixture _fixture; - determines an environment for the test execution and gives an access to the test database. The environment can be local (derived from AbstractSoloDb4oFixture) or client/server (AbstractClientServerDb4oFixture).

You can always access the fixture from your test class using

Java:

public void fixture(Db4oFixture fixture)

Methods for working with a database:

Java:

public ExtObjectContainer db()

Returns an instanse of object container for the current environment.


Java:

protected void reopen() throws Exception

This function will close the database and open it again. It also performs an implicit commit on close.


Java:

protected Reflector reflector()

Returns current reflector.


Java:

protected Transaction trans()

protected Transaction systemTrans()

protected Transaction newTransaction()

Methods to get transaction object for the current environment.

Various methods to work with persistent objects:

Java:

protected Query newQuery()

protected Query newQuery(Class clazz)

protected Query newQuery(Transaction transaction, Class clazz)

protected Query newQuery(Transaction transaction)

Checks if only one object of a class is stored in the database


Java:

protected int countOccurences(Class clazz)

Returns the amount of objects of the specified class in the database.


Java:

protected void foreach(Class clazz, Visitor4 visitor)

This method goes through the ObjectSet of the specified class objects in the database calling Visitor4.visit() method. Visitor4 is an interface specifiing a visit method:


Java:

public void visit(Object obj);


Java:

protected void deleteAll(Class clazz)

Deletes all the instances of the specified class in the database.


Java:

protected ReflectClass reflectClass(Class clazz)

Returns a ReflectClass instance for the specified class.


Java:

protected void indexField(Configuration config,Class clazz, String fieldName)

Adds field index into specified configuration.


Java:

public final void setUp() throws Exception

This method:


More details about the mentioned above methods:

Java:

protected void configure(Configuration config)

Use this method to create your custom configuration for a test. Config parameter is the current default test configuration, which can be modified.


Java:

protected void db4oSetupBeforeStore() throws Exception

This method is a placeholder for any custom setup actions that need to be taken before filling up the database with objects.

Java:

protected void store() throws Exception {}

This method is supplied for creating and storing the objects, which you are going to use for your test.


Java:

protected void db4oSetupAfterStore() throws Exception

This method is a placeholder for any custom setup actions that need to be taken after the database is filled up with objects.


Methods for running tests:

Java:

public int runSoloAndClientServer()

Will run the test in both modes


Java:

public int runSolo()

Only local mode.


Java:

public int runClientServer()

Db4ounit.Assert

Db4ounit.Assert class - provides a variety of methods for controlling code execution. Some of the methods are presented below. For more information please refer to the source code.


Java:

public static void expect(Class exception, CodeBlock block)

This method runs a specified method (block parameter) and throws an exception if the block runs without any exception.


Java:

public static void isTrue(boolean condition)

public static void isTrue(boolean condition, String msg)

This method checks the condition and throws an exception if the condition is false. Msg parameter can be used to customize exception message.


Java:

public static void areEqual(Object expected, Object actual)

Checks if the supplied parameters are equal and throws an exception otherwise.

Similar methods are provided for null, lesser, greater and other checkings, please refer to Assert class code for full information.


FrameworkTestCase

FrameworkTestCase class provides methods to run your test suite and check if its results.

Java:

public static void runTestAndExpect(Test test,int expFailures)

This method will run the test specified and throw an exception if the number of expected failures (expFailures parameter) is not equal to the number of experienced failures.


For more information please refer to the source code of FrameworkTestCase class.