Integration Testing - jamongx/twitter-clone GitHub Wiki

What is Integration Testing

  • As the name suggests, integration tests focus on integrating different layers of the application. That also means no mocking is involved.
  • Basically, we write integration tests for testing a feature which may involve interaction with multiple components.

Controller Layer

  • integration testing is built based on Controller layer
  • Below are the annotations used in unit testing for each layer and their descriptions.

Spring Boot Test

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

  • This annotation is used in Spring Boot applications for integration testing.
  • It starts the entire application context.
  • The webEnvironment attribute specifies how the web environment for the test should be configured, with RANDOM_PORT meaning the application starts at a random port, useful for avoiding port conflicts in tests.

@AutoConfigureMockMvc(addFilters = false)

  • This annotation automatically configures MockMvc, which is essential for testing web applications in Spring.
  • The addFilters = false attribute disables the automatic addition of filters like Spring Security filters, which can be useful when you want to focus on testing the MVC layer without security constraints.

@MockBean(JpaMetamodelMappingContext.class)

  • This is used to add a mock version of a specific bean to the Spring application context.
  • Here, JpaMetamodelMappingContext.class is being mocked, which can be useful to avoid issues related to JPA entity mapping in tests where the JPA layer isn't needed.

@MockBean

  • This annotation is used to add mock implementations of beans to the Spring application context.
  • It can be used without specifying a class, in which case it will mock the return type of the annotated field or method.

Spring Test

@Sql("/data.sql")

  • This annotation is used for executing SQL scripts against a database.
  • It's often used in tests to set up a database state before running a test.
  • The provided path ("/data.sql") is where the SQL script is located.

Spring Security Test

@WithMockUser("Tester")

  • This annotation is used to simulate a logged-in user during testing.
  • It sets up a mock security context for a user named "Tester".
  • This is particularly useful for controller tests that require a user context.

JUnit

@BeforeEach

  • This annotation is used to specify a method that should be executed before each test method in the class.
  • It's often used for setup tasks that are common to all tests in the class.

@Test

  • This annotation denotes a method as a test method.
  • This is a fundamental JUnit annotation used to mark methods that should be executed by the test runner as tests.