Skip to content

Spring Framework Blog

Menu
  • Home
  • Spring
  • Spring Boot
  • About
Menu

Tests

Posted on June 7, 2024June 18, 2024 by Armando Marques

Table of Contents

Toggle
  • Spring Junit
  • Dependencies
  • Mock vs MockBean
  • Annotations

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

  • Spring
  • Documentation
  • References
  • Toc
  • Books
  • Certification
  • AOP
  • Config
  • Java
  • Java core
  • JDBC
  • JPA
  • Rest
  • Security
  • Spring
  • Spring Boot
  • Spring Core
  • Spring Data
  • Spring MVC
  • Spring Rest
  • Spring Security
  • Tests
  • Transactions
  • Uncategorized

Recent Posts

  • Spring Annotations
  • Java Tests
  • Java operators
  • Java versions
  • Java Oracle Licenses
  • Configuration properties
  • MockMvc
  • Spring Security III
  • MVC Controller Method Params
  • JPA Methods
  • Transaction propagation and isolation
  • Spring JDBC
  • Spring Boot Auto-Configuration
  • Spring Resource interface
  • JSR 330 Standard Annotations
  • Spring Aware Interfaces
  • Spring Transactions
  • Spring Boot Core
  • MVC Rest
  • Spring Boot JPA
©2025 Spring Framework Blog | Built using WordPress and Responsive Blogily theme by Superb