When you have a legacy product that has been around for over 10 years and it is written with old technologies and has no automated tests, how do you improve quality? Sometimes the best solution is to rewrite it!
That was the situation we were in with a product written with JBOSS. It had become difficult to maintain and debug so we decided to rewrite it using the Spring Boot framework, and add unit and integration tests as we were doing it. This is not a simple undertaking and it's still a work in progress; but I feel we made the right decision.
As not many of the developers had much experience with writing unit and integration tests, I wrote up the guidelines shown below. These have been written for a Java based Spring Boot project that uses the JUnit framework to write the tests, but are general enough to apply to most languages and frameworks.
Guidelines:
- Test the behaviour of the class; not its implementation.
- Don’t test implementation details; e.g. private methods.
- Use descriptive names for your tests that explain what you are testing. Don't be afraid to use long test names. Some examples:
Not very descriptive test case name
|
More descriptive test case name
|
saveSysUserRecord()
|
newSysUserShouldBeSavedToRepository()
|
saveBadSysUserRecord()
|
newSysUserWithMissingCreateTimeShouldThrowException()
|
deleteSysUserRecord()
|
deletingSysUserShouldRemoveItFromRepository()
|
- Each test should have the format: Setup test data (Given), do the action you are testing (When), check the result (Then). See example integration test below. Note that you do not need to add in the comments when you actually create the test cases:
@Test
public void newSysUserShouldBeSavedToRepository() {
//Given
final long count = repository.count();
SysUser sysUser = new SysUser();
//When
repository.saveAndFlush(sysUser);
//Then
assertThat(repository.count()).isEqualTo(count + 1);
}
- Only test one thing per test case. You should only assert the result in the Then section of the code.
- Use the AssertJ http://joel-costigliola.github.io/assertj/index.html library to write assertions in a fluid, readable way.
- If you need to run a method before each test case (e.g. to setup test data), use the @Before annotation.
- Make sure that the test fails if the assert condition is not true. For each test you write change the data to fail the test. This way you are sure that the test will fail if there is a change.
- Test both negative and positive scenarios. For example:
- Positive scenario: findByUserIdShouldReturnOneSysUser()
- Corresponding negative scenario: findByUserIdWhenUserNonExistentShouldReturnNull()
We found the following talk and corresponding example code by Phil Webb useful:
Video on testing Spring Boot applications:
The source code for the video is in github:
Some tips from the talk:
- Use AssertJ for assertions. Spring Boot 1.4 and above has it built in.
- Constructor injection makes it easier to test.
- Prefer plain Junit when writing unit tests; i.e. try not to involve Spring.
- When you do need to use Spring (for integration tests) use helper classes such as TestEntity Manager.
- TestEntityManager is useful to setup test data when testing repositories.
Comments
Post a Comment