Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
Info

Help is available by sending an email to csf-support@mit.edu
Have any suggestion on how improve this wiki?  Please give us your feedback at csf-support@mit.edu

Quick

Links

to:

{

Panel
Wiki Markup
Table of Contents
:
minLevel
=
3
}
Panel

Definition

In our applications and framework, we have used the term "integration test" to describe a written JUnit test case that requires a full Spring context (including all the framework classes), and actually talks to the MITSIS Oracle database.  An integration test is disctinct from a pure unit test, which typically uses mock objects for dependencies, and does not talk to the database.  We mainly use integration tests for testing DAO classes, but they could possibly be used also for testing service classes and MVC controllers.

...

Panel

The CSF AbstractIntegrationTestBase.java unit test base class

CSF now has a new abstract class, AbstractIntegrationTestBase class, found in the csf-test module. This class uses JUnit and Spring annotations to gain access to functionality previously provided by extending the deprecated Spring class AbstractTransactionalDataSourceSpringContextTests.

This CSF abstract class is to be used with all integration tests.  It replaces the following deprecated classes

  • BaseTransactionalIntegrationTest
  • AbstractTransactionalDataSourceSpringContextTests
  • BaseSecurityTransactionalIntegrationTest

that were used in csf-common-legacy.

h3.

About

the

AbstractIntegrationTestBase

base

class

Below

is

a

complete

listing

of

the

AbstractIntergrationTestBase

class.  Of particular interest is the usage of the @Transactional and the @ContextConfiguration annotations.

Panel
Wiki Markup
Note

It is important that you read and understand the class comments regarding the @Transactional annotation.  It is reproduced here for emphasis.

  Of particular interest is the usage of the *@Transactional* and the *@ContextConfiguration* annotations. {note} It is important that you read and understand the class comments regarding the *@Transactional* annotation.  It is reproduced here for emphasis. {code}
Code Block

 * The class is marked as @Transactional, which means test methods in derived
 * classes will be run in a transaction and will be automatically rolled back on
 * completion.  Normally our "integration" tests should use a transaction and do 
 * rollback, so the defaults should always be what you want.
 

The locations element of the @ContextConfiguration only specifies one xml file.  This xml file is used to set the default test application context for ALL unit tests which extends AbstractIntegrationTestBase.java class.  At the bottom of this page is more information on how to configure and how to override the default test application context.

Code Block
{code}
{note}

The *locations* element of the *@ContextConfiguration* only specifies one xml file.  This xml file is used to set the default test application context for *ALL* unit tests which extends AbstractIntegrationTestBase.java class.  At the bottom of this page is more information on [how to configure | #configure default context] and [how to override | #override default context] the default test application context.

{code}
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author sturner AbstractIntegrationTestBase
 *
 * Based on the old BaseTransactionalIntegrationTest class: refactored to move
 * away from deprecated Spring classes and to the newer Spring TestContext
 * Framework.
 *
 * NOTE - this is now an abstract class. This does not change the usage, as the
 * old version of the class was only ever used as a base class for JUnit
 * classes.
 *
 * The list of context config locations can be overriden by coding
 * @ContextConfiguration in a base class.
 *
 * The class is marked as @Transactional, which means test methods in derived
 * classes will be run in a transaction and will be automatically rolled back on
 * completion.  Normally our "integration" tests should use a transaction and do 
 * rollback, so the defaults should always be what you want.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-csf-unit-tests-default.xml"})
@TestExecutionListeners({TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})
@Transactional
public abstract class AbstractIntegrationTestBase {

    public AbstractIntegrationTestBase() {
        super();
    }
}
{code}
h3. Usage of the AbstractIntegrationTestBase base class # The following code fragment illustrates how to use AbstractIntegrationTestBase class {code}
  1. The following code fragment illustrates how to use AbstractIntegrationTestBase class
Panel

Usage of the AbstractIntegrationTestBase base class

Panel
Wiki Markup
Code Block
  1. 
    import edu.mit.csf.test.AbstractIntegrationTestBase;
    
    public class TestHibernateAcademicTermDao extends AbstractIntegrationTestBase {
    
    }
    
{code} \\ # Add the following dependencies to your

  1. Add the following dependencies to your project's
  1. pom.xml.
{
  1. Code Block
}
  1. 
            <dependency>
                <groupId>edu.mit.ist.es.csf</groupId>
                <artifactId>csf-test</artifactId>
                <version>[0.0.0,999.999.999)</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
            </dependency>
{code}
  1. 
    
Panel

Anchor
configure default context
configure default context

How to configuration the default test application context

The annotation

Code Block
Wiki Markup

{anchor:configure default context}
h3. How to configuration the default test application context
The annotation
{code}
@ContextConfiguration(locations = {"classpath:applicationContext-csf-unit-tests-default.xml"})
{code}

is

used

to

set

the

default

test

application

context

for

all

the

junit

tests

which

extend

the

AbstractIntegrationTestBase

.java

class.

If

the

project

was

created

using

the

es-project-template

(for

either

the

jar

or

war)

then

the

applicationContext-csf-unit-tests-default.xml

file

can

be

found

it

the

project's

src/test/resources

directory.

If

the

project

was

*

NOT

*

created

using

the

es-project-template,

then

the

applicationContext-csf-unit-tests-default.xml

must

be

created

in

the

project's

src/test/resources

directory.

&nbsp;&nbsp;To download the a starter version of the

  To download the a starter version of the applicationContext-csf-unit-tests-default.xml

file,

[

right

click

on

this

link | Using the unit test base class^applicationContext-csf-unit-tests-default.xml] and select *Save target as...*.&nbsp;&nbsp;{color:red}Be sure to save the file in the project's

link and select Save target as....  Be sure to save the file in the project's src/test/resources

directory.

{color}

The

following

is

a

listing

of

the

starter

version

of

the

applicationContext-csf-unit-tests-default.xml

file:

* *

  • For
  • your
  • CSF
  • JAR
  • projects
*:&nbsp;&nbsp;Replace <projectName> with your project name. {code}
  • :  Replace <projectName> with your project name.
    Code Block
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/mvc/spring-mvc-xsd
               http://www.springframework.org/schema/util
               http://www.springframework.org/schema/util/spring-util.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx.xsd">
               
        <import resource="classpath*:applicationContext-csf-common.xml" />
        <import resource="classpath*:applicationContext-csf-webservices.xml" />
        <import resource="classpath*:applicationContext-csf-security-spring.xml" />
        <import resource="classpath*:applicationContext-csf-orm.xml" />
        <import resource="classpath*:applicationContext-csf-base.xml" />
        <!--import resource="classpath*:applicationContext-csf-email.xml" /-->
        <!--import resource="classpath*:applicationContext-csf-rules.xml" /-->
        <!--import resource="classpath*:applicationContext-csf-workflow.xml" /-->
        <import resource="classpath*:applicationContext-csf-<projectName>.xml" />
        <import resource="classpath*:applicationContext-csf-<projectName>-config-test.xml" />
     </beans>
    
{code} \\ * *For your CSF WAR project*:&nbsp;&nbsp;Replace <projectName> with your project name. {code}

  • For your CSF WAR project:  Replace <projectName> with your project name.
    Code Block
    
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/mvc/spring-mvc-xsd
               http://www.springframework.org/schema/util
               http://www.springframework.org/schema/util/spring-util.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx.xsd">
               
        <import resource="classpath*:applicationContext-csf-common.xml" />
        <import resource="classpath*:applicationContext-csf-webservices.xml" />
        <import resource="classpath*:applicationContext-csf-security-spring.xml" />
        <import resource="classpath*:applicationContext-csf-orm.xml" />
        <import resource="classpath*:applicationContext-csf-base.xml" />
        <!--import resource="classpath*:applicationContext-csf-email.xml" /-->
        <!--import resource="classpath*:applicationContext-csf-rules.xml" /-->
        <!--import resource="classpath*:applicationContext-csf-workflow.xml" /-->
        <import resource="classpath*:applicationContext-csf-<projectName>.xml" />
        <import resource="classpath*:applicationContext-<projectName>.xml" />
        <import resource="classpath*:applicationContext-<projectName>-config-test.xml" />
     </
beans> {code} \\ This starter xml file must now be customized with your
  • beans>
    

    This starter xml file must now be customized with your project's
  • application
  • context
  • xml
  • files.

The

following

is

an

example

from

the

Student

Sponsored

Billing

application

and

is

an

example

on

how

to

customize

the

applicationContext-csf-unit-tests-default.xml

file.

{
Code Block
}
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd">
           
    <import resource="classpath*:applicationContext-csf-common.xml" />
    <import resource="classpath*:applicationContext-csf-webservices.xml" />
    <import resource="classpath*:applicationContext-csf-security-spring.xml" />
    <import resource="classpath*:applicationContext-csf-orm.xml" />
    <import resource="classpath*:applicationContext-csf-base.xml" />
    <!--import resource="classpath*:applicationContext-csf-email.xml" /-->
    <!--import resource="classpath*:applicationContext-csf-rules.xml" /-->
    <!--import resource="classpath*:applicationContext-csf-workflow.xml" /-->
    <import resource="classpath*:applicationContext-csf-studentaccount.xml" />
    <import resource="classpath*:applicationContext-csf-ssb.xml" />
    <import resource="classpath*:applicationContext-ssb.xml" />
    <import resource="classpath*:applicationContext-ssb-config-test.xml" />
</beans>
{code}
{color:red}The order in which the xml files appear is important.&nbsp;&nbsp;Just remember the beans are instantiated in the order that they are processed and, if needed, overriding previous bean instantiations with identical ids.{color}

The order in which the xml files appear is important.  Just remember the beans are instantiated in the order that they are processed and, if needed, overriding previous bean instantiations with identical ids.

Panel

Anchor
override default context
override default context

How to override for the default test application context

The following code fragment illustrates how to override the default test application conxtext:

Code Block
Panel
Wiki Markup

{anchor:override default context}
h3. How to override for the default test application context
The following code fragment illustrates how to override the default test application conxtext:
{code}
import org.springframework.test.context.ContextConfiguration;
import edu.mit.csf.test.AbstractIntegrationTestBase;

@ContextConfiguration(locations = {"classpath:applicationContext-special-authorization-test.xml"})
public class TestHibernateAcademicTermDao extends AbstractIntegrationTestBase {

}
{code}

This

applicationContext-special-authorization-test.xml

must

also

exist

in

the

project's

src/test/resources

directory.  This xml file will contain a list of imports of application context xml files.  For example, the contents of this xml file might look like:

Code Block
&nbsp;&nbsp;This xml file will contain a list of imports of application context xml files.&nbsp;&nbsp;For example, the contents of this xml file might look like:
{code}
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd">
           
    <import resource="classpath*:applicationContext-special-setup.xml" />
    <import resource="classpath*:applicationContext-csf-base.xml" />
    <import resource="classpath*:applicationContext-csf-webservices.xml" />
    <import resource="classpath*:applicationContext-csf-security-spring.xml" />
    <import resource="classpath*:applicationContext-csf-common.xml" />
    <import resource="classpath*:applicationContext-csf-orm.xml" />
    <import resource="classpath*:applicationContext-special-authorizations.xml" />
    <import resource="classpath*:applicationContext-general-config-test.xml" />
</beans>
{code}