Integration Testing and Test Data Builders
This training covers integration testing principles, using Spring TestContext framework, and creating test data builders both by-hand and by using Make-It-Easy.
Contents
Prerequisites
- Current release of the LDSTech IDE, or an IDE you are familiar with
- Java JDK 1.6+
- Unit Testing with TestNG
- Test Doubles, Mocks, and Matchers
Slide Deck
Section 1: Introduction to Integration Test with the Spring TestContext framework
Presentation
Lab 1
- Download the training project template
- Unzip the artifact where you typically keep training or other projects
- The integration-training/integration directory should be the root of your project. There a variety of ways to open this, depending on your IDE:
- For LDS Tech IDE, open the integration-training folder as a workspace in your LDS Tech IDE, and then import integration-training/integration as the root directory.
- NOTE: there are three Maven modules in the project. Please import all three if you would like to see example code from the training. The work you will be doing for the labs is in the lab module.
- For this first lab, you will be configuring a test class to use the Spring TestContext framework, as well as completing a couple of integration test methods. The class to modify is org.lds.stack.training.integration.lab.service.Lab1ContactServiceIT. I have partially completed the test code and provided test fixtures. Each item that you need to do in order to complete the lab is marked with a TODO comment.
- The first comment prompts you to add the @ContextConfiguration annotation to the class, along with the correct context locations. Try to figure this out yourself. If you get stuck, the following code will help you to move forward:
@ContextConfiguration(locations = {
"classpath*:META-INF/spring/*Context.xml",
"classpath*:META-INF/spring/*Context-test.xml"})
- There is a total of seven items to modify in the class. Refer to the slide deck if you get stuck. Remember to run the tests to verify that they are actually working now.
Lab 1 Solution
Section 2: Advanced TestContext - Context, Configuration Classes, @DirtiesContext
Presentation
Lab 2
- For this lab, you will be modifying a clone of the test we worked on in the first lab to use the new Spring Java configuration classes to load the application context. Additionally, you will be completing a test by replacing the MailService with a test double. The class to modify is org.lds.stack.training.integration.lab.service.Lab2ContactServiceIT. As in the prior lab, all changes are marked with TODO comments.
- The first comment prompts you to modify the @ContextConfiguration annotation to use a configuration class instead. One way to do this is change the annotation to the following:
@ContextConfiguration(classes=AppConfig.class)
- Then, create a class called AppConfig that contains the Java configuration.
- There are three items to complete in this lab, including creating the configuration class. Use the instructions in the TODO comments, along with the slide deck, to determine how best to complete the test class.
Lab 2 Solution
Section 3: Creating Test Data Builders Using Make-It-Easy
Presentation
Lab 3
- For this lab, you will be creating a builder to simplify the creation of test fixtures in a test class. The test class in question is org.lds.stack.training.integration.lab.service.Lab3ContactServiceIT.
- Start by completing the code in org.lds.stack.training.integration.lab.model.ContactMaker. You can use the CategoryMaker class in the same package as an example.
- Once you have completed your builder, replace the creation of the contacts and categories with code using your builders.
- Admire how short and easy-to-read your test code is.
- There are only two things to do in this test, but they are both fairly substantial. Refer to the slide deck if you get stuck!
Lab 3 Solution
This page was last modified on 17 December 2012, at 15:11.