JUnit & Spring – What You Don’t Know

by
Tags: ,
Category:

Gordon Dickens, Chariot architect and one of our education trainers, recently wrote this blog post on JUnit and Spring.

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
1
2
3
4
5
6
7
package com.gordondickens.sample;
@ContextConfiguration
public class UtilsTest {
  ...
}
...
Is equivalent to
1
2
3
  ...
@ContextConfiguration("classpath:/com/gordondickens/sample/UtilsTest-context.xml")
  ...

 Read more…