Spring Junit
Dependencies
Dependencies include in Spring Boot Tests
- AssertJ
- Mockito
- Junit
- JsonPath
- JsonAssert
- Spring Test and Spring boot Test
Mock vs MockBean
@Mock
- uses MockitoJUnitRunner
- can be uses in fields and parameters
@MockBean
- Spring Boot framework
- add mocks to Spring Application context
- can be used in classes and fields
Annotations
@ActiveProfiles
@SpringJunitConfig
- compose annotation of @ExtendsWith(SpringExtension.class) and @ContextConfiguration
JDBC
@Rollback
- indicates whether the transaction for a transactional test method should be rolled back after the test method has completed.
@Commit
- indicates that the transaction for a transactional test method should be committed after the test method has complete.
@BeforeTransaction @AfterTransaction
@DirtiesContext
- indicates that the underlying Spring ApplicationContext has been dirtied during the execution of a test (that is, the test modified or corrupted it in some manner — for example, by changing the state of a singleton bean) and should be closed. When an application context is marked as dirty, it is removed from the testing framework’s cache and closed.
@Sql @SqlConfig
@Test
@Sql(
scripts = "/test-user-data.sql",
config = @SqlConfig(commentPrefix = "`", separator = "@@")
)
void userTest() {
// run code that relies on the test data
}
https://docs.spring.io/spring-framework/reference/testing/annotations/integration-spring.html