Slide 1

Slide 1 text

Testing Spring Boot Applications Andy Wilkinson @ankinson

Slide 2

Slide 2 text

@ankinson Why Bother Testing At All?

Slide 3

Slide 3 text

@ankinson

Slide 4

Slide 4 text

@ankinson Risk None YOLO Tests 0 Lots

Slide 5

Slide 5 text

@ankinson

Slide 6

Slide 6 text

@ankinson Unit Tests

Slide 7

Slide 7 text

@ankinson

Slide 8

Slide 8 text

@ankinson MockRestServiceServer OkHTTP’s MockWebServer Spring Data Repositories JdbcTemplate

Slide 9

Slide 9 text

@ankinson Testcontainers

Slide 10

Slide 10 text

@ankinson https://www.testcontainers.org

Slide 11

Slide 11 text

@ankinson @SpringBootTest @Testcontainers(disabledWithoutDocker = true) @ContextConfiguration(initializers = ExampleIntegrationTests.Initializer.class) class ExampleIntegrationTests { @Container public static CassandraContainer> cassandra = new CassandraContainer<>(); static class Initializer implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext context) { int cqlPort = cassandra.getMappedPort(CassandraContainer.CQL_PORT); TestPropertyValues.of("spring.data.cassandra.port=" + cqlPort) .applyTo(context.getEnvironment()); } } }

Slide 12

Slide 12 text

@ankinson Integration Tests

Slide 13

Slide 13 text

@ankinson @SpringBootTest

Slide 14

Slide 14 text

@ankinson Context Caching

Slide 15

Slide 15 text

@ankinson @SpringBootTest(properties="spring.jmx.enabled=true") @ActiveProfiles("standalone") @TestPropertySource(locations="standalone.properties") class ExampleIntegrationTests { @Test void contextLoads() { } } @ContextConfiguration @Import @TestPropertySource @ActiveProfiles @SpringBootTest

Slide 16

Slide 16 text

@ankinson @DirtiesContext

Slide 17

Slide 17 text

@ankinson Sliced Tests

Slide 18

Slide 18 text

@ankinson @JsonTest @WebMvcTest @WebFluxTest @DataJpaTest @JdbcTest @JooqTest @DataMongoTest @DataNeo4jTest @DataRedisTest @DataLdapTest @RestClientTest

Slide 19

Slide 19 text

@ankinson @WebMvcTest @DataJpaTest OrderRepository extends JpaRepository<…> @Controller OrderController @Controller CustomerController CustomerRepository extends JpaRepository<…> @SpringBootApplication Application

Slide 20

Slide 20 text

@ankinson @MockBean and @SpyBean

Slide 21

Slide 21 text

@ankinson @WebMvcTest class CustomerControllerIntegrationTests { @Autowired private MockMvc mockMvc; @Configuration static class RepositoryConfiguration { @Bean CustomerRepository customers() { return mock(CustomerRepository.class); } @Bean OrderRepository orders() { return mock(OrderRepository.class); } } }

Slide 22

Slide 22 text

@ankinson Customer Customer Order @WebMvcTest class CustomerControllerTests { @Autowired private MockMvc mockMvc; @MockBean private CustomerRepository customers; @MockBean private OrderRepository orders; } (controllers = CustomerController.class)

Slide 23

Slide 23 text

JUnit 5: Evolution and Innovation Sam Brannen Thursday 10:30am–11:40am Ballroom F

Slide 24

Slide 24 text

Thanks! Q&A Andy Wilkinson @ankinson #springone @s1p