I'm working on a Spring MVC project, and I have unit tests for all of the various components in the source tree.
For example, if I have a controller HomeController
, which needs to have a LoginService
injected into it, then in my unit test HomeControllerTest
I simply instantiate the object as normal (outside of Spring) and inject the property:
protected void setUp() throws Exception { super.setUp(); //... controller = new HomeController(); controller.setLoginService( new SimpleLoginService() ); //... }
This works great for testing each component as an isolated unit - except now that I have a few dozen classes in the project, after writing a class and writing a successful unit test for it, I keep forgetting to update my Spring MVC context file that does the actual wiring-up in the deployed application. I find out that I forgot to update the context file when I deploy the project to Tomcat and find a bunch of NullPointers from non-wired-up beans.
So, here are my questions:
This is my first Spring project - is it normal to create unit tests for the individual beans, as I have done, and then create a second suite of tests (integration tests) to test that everything works as expected with the actual application context? Is there an established best practice for this?
In addition, how do you separate the unit tests from the integration tests? I have all of the source code in src
, the unit tests in test
- should there be a 2nd test folder (such as test-integration
) for integration test cases?
Since this is my first Spring project, I'm curious how others usually go about doing this sort of thing - and rather than re-invent the wheel I rather ask the rest of the community.
Unit tests run in isolation while integration tests bootstrap the Spring web context before execution starts. Running in isolation will sometimes require that you mock your dependencies based on the class you are testing.
While unit tests always take results from a single unit, such as a function call, integration tests may aggregate results from various parts and sources. In an integration test, there is no need to mock away parts of the application. You can replace external systems, but the application works in an integrated way.
Unit Testing is a kind of white box testing, whereas Integration Testing is a kind of black-box testing. For Unit Testing, accessibility of code is required, as it tests the written code, while for Integration Testing, access to code is not required, since it tests the interactions and interfaces between modules.
The Spring Framework provides first-class support for integration testing in the spring-test module. The name of the actual JAR file might include the release version and might also be in the long org. springframework.
I can't speak to being a best practice, but here's what I've done in the past.
Unit tests:
test
directory. Using Test
or TestCase
as a prefix or suffix to the classname seems to be widely practiced.Integration Tests:
AbstractIntegrationTestCase
that sets up a Spring
WebApplicationContext
for use in intetgration test clases.test
directory. I've used IntTest
or IntegrationTest
as a prefix or suffix for these tests.Set up three Ant test
targets:
test
seems to be the most common usage for unit testingAs noted, you can use the naming conventions that make sense for your project.
As to separating unit from integration tests into a separate directory, I don't think it matters as long as the developers and their tools can find and execute them easily.
As an example, the last Java project I worked on with Spring used exactly what is described above, with integration tests and unit tests living in the same test
directory. Grails projects, on the other hand, explicitly separate unit and integration test directories under a general test directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With