This webinar by Oleksandr Navka (Lead Software Engineer, Consultant, GlobalLogic) was delivered at Java Community Webinar #2 on September 17, 2020.
Webinar agenda:
- tools for testing,
- features of creating a context for testing Spring-applications,
- context caching to speed up integration testing
More details and presentation: https://www.globallogic.com/ua/about/events/java-community-webinar-2/
5. 5
1. We’ve got very fast tests that test next to nothing
2. We’ve got tests that very slow and nobody run them
3. Developers don’t test their code
4. We’ve got a very special project that hard to test
5. The tests only required if you not sure in your code
Top testing problems
9. 9
class A {
private B dependency1;
private C dependency2;
public Result getResult() {
//code need to test
}
}
Unit tests
10. 10
class A {
private B dependency1;
private C dependency2;
public Result getResult() {
//code need to test
}
}
Unit tests
need to inject
dependencies
15. 15
• they are extremely fast
• do not hesitate to write one more unit test
• do not use any spring features
• do not use field injection in Spring
• all dependencies should be injected manually as mocks
• start to use JUnit 5 and AssertJ
Unit tests summary
23. 23
@ContextConfiguration
• set the classes that we need
• set configuration
@ContextHierarchy - for context caching
@SpringBootTest - for Spring boot
How to start spring context in test
32. 32
1. @SpringBootTest - scan project ↑↑↑ until
@SpringBootConfiguration
2. @SpringBootTest with configuration do not scan
3. @SpringBootTest with test configuration scan project ↑↑↑
Spring boot test summary
37. 37
1. @ContextConfiguration - starts spring context for tests
2. @SpringBootTest - the same but in Spring boot
3. Be aware about different modes of SpringBootTest
4. @WebMvcTest and @DataJpaTest start context partially
Component test summary
39. 39
- What if I want to test all application(microservice)?
- If you want to test all application use SpringBootTest and
don’t forget about 3rd party application mocks
41. 41
1. The most important skill is to understand how to divide tests
into levels
2. Keep eye on spring context
3. Spring context configuration should be clear for all team
4. The best option to move context configuration into a
separate class isolated from tests
Summary