Posts

Showing posts with the label Junit

Spring Boot - Testing Spring Data JDBC application with @DataJdbcTest - Example

Image
In this section, we will learn how to test Repository layer components with @DataJdbcTest in Spring Boot application. 1.  @ DataJdbcTest Instead of bootstrapping the entire application context for every test, @DataJdbcTest allows us to initialize the Spring application context with only those beans needed to test Spring Data JDBC-based components. It will auto-configure JdbcTemplate and if an embedded database is available on the classpath, @ DataJdbcTest   will autoconfigure one for testing purposes. By default, tests annotated with @ DataJdbcTest   are transactional and roll back at the end of each test, means we do not need to clean up saved or modified table data after each test. Regular @Component , @Service or @Controller beans are not scanned when using this annotation.  This approach not only speeds up the testing process but also ensures a focused and efficient testing environment. This approach is also known as "slicing" the application context. Find the sample c

Spring Boot - Testing Spring WebFlux Controller with @WebFluxTest - Example

Image
In this section, we will learn how to test Spring WebFlux Controller with @WebFluxTest. 1.  @WebFluxTest Instead of bootstrapping the entire application context for every test, @WebFluxTest  allows us to initialize only the parts of the Application context that are necessary for our Spring WebFlux layer. This allows us to focus on testing the controllers, and related components. Following beans will be scanned while using @WebFluxTest : @Controller @ControllerAdvice @JsonComponent Converter GenericConverter Filter WebFluxConfigurer Regular @Component, @Service or @Repository beans are not scanned when using this annotation. This approach not only speeds up the testing process but also ensures a focused and efficient testing environment.  This approach is also known as "slicing" the application context. The annotation can be used to test a single controller by passing it as an attribute, for example @WebFluxTest(SomeController.class). @WebFluxTest ( StudentController . class )