DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. JUnit & Spring – What You Don’t Know

JUnit & Spring – What You Don’t Know

Gordon Dickens user avatar by
Gordon Dickens
·
Jan. 14, 11 · Interview
Like (1)
Save
Tweet
Share
47.22K Views

Join the DZone community and get the full member experience.

Join For Free
When using JUnit in Spring there are several features added that many developers are not aware of.

First, if you are including the Spring Context in your tests, it becomes an Integration Test, no longer a Unit Test.

1. Default Searching of Context File(s)

To instruct Spring to load beans for the tests in the class, we annotate the class with @ContextConfiguration

1a. No File Specified

@ContextConfiguration – with no parameters, (by Default) looks for the config file as the same name as the class with the suffix “-context.xml“. For example,

Using

package com.gordondickens.sample;

@ContextConfiguration
public class UtilsTest {
...
}
...

Is equivalent to

...
@ContextConfiguration("classpath:/com/gordondickens/sample/UtilsTest-context.xml")
...
Will look for a file called UtilsTest-context.xml in the Classpath under the same package as the test. If we are using the standard Maven structure, Spring will search for:

 

  • src/test/java/com/gordondickens/sample/UtilsTest-context.xml
  • src/test/resources/com/gordondickens/sample/UtilsTest-context.xml
  • src/main/java/com/gordondickens/sample/UtilsTest-context.xml
  • src/main/resources/com/gordondickens/sample/UtilsTest-context.xml

1b. File specified (without a starting Slash)

Using

package com.gordondickens.sample;

@ContextConfiguration("utils-context.xml")
public class UtilsTest {
...
}
...

Is equivalent to

 ...
@ContextConfiguration("classpath:/com/gordondickens/sample/utils-context.xml")
...

 

Spring searches for utils-context.xml in each classpath’s directory for package and does NOT traverse subdirectories (packages).

1c. File specified with a Starting Slash

One Simple change can ruin your whole day! Add a Starting Slash to the file name.

Using 

  ...
@ContextConfiguration(value = "/META-INF/spring/utils-context.xml")
...

is equivalent to 

...
@ContextConfiguration("classpath:/META-INF/spring/utils-context.xml")
...

1d. Multiple Files

Pulling multiple configuration files into the application context for your tests.

...
@ContextConfiguration(locations = {"utils-context.xml", "app-context.xml"})
...

 

Best Practice Tip

Create an XML file per test that imports only the application’s context files that are needed.
This can save test execution time, where we only load beans necessary for these tests.

2. Spring Aware Test Options

We can load configuration files for the tests, but we want the added benefit of using Spring Annotations.

NOTE: This requires JUnit 4.5 or later.

Annotate the test class with @RunWith(SpringJUnit4ClassRunner.class).

2a. Autowiring Beans

@Autowired Autowire (inject) in dependencies

Tip: If you want access to the ApplicationContext you can simply autowire this into your test class.

 ...
@Autowired
ApplicationContext applicationContext;
...

2b. Transactional Test Methods

@Transactional At the method level, method is transactional. Class level, all methods are transactional
@TransactionConfiguration Define default transaction parameters for the class
@Rollback Change rollback settings for methods
@BeforeTransaction Method to execute before a transaction
@AfterTransaction Method to execute after a transaction

NOTE: Transactions are rolled back by default in tests. Allowing repeat execution to perform the same db actions.

2c. Profiles - Evaluating Environment

@ProfileValueSourceConfiguration Class level settings for all tests, defaults to system properties
@IfProfileValue Specify name and value(s) that must match to execute the test method

2d. Timeout, Repeating & Invalidating Context

@Timed Defined maximum time that method has to execute
@Repeat Specify the number of times the test method will repeat
@DirtiesContext At class or method level, mark the app context as dirty and should be closed. The app context will be recreated for subsequent test

From http://gordondickens.com/wordpress/2011/01/07/junit-spring-what-you-dont-know-about/

Spring Framework Testing JUnit

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Seamless Integration of Azure Functions With SQL Server: A Developer's Perspective
  • What To Know Before Implementing IIoT
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?
  • Unlocking the Power of Elasticsearch: A Comprehensive Guide to Complex Search Use Cases

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: