Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Last active February 9, 2024 10:50
Show Gist options
  • Save aoudiamoncef/8c6b474a4c2a93a60bf87b311a1f9de5 to your computer and use it in GitHub Desktop.
Save aoudiamoncef/8c6b474a4c2a93a60bf87b311a1f9de5 to your computer and use it in GitHub Desktop.
Spring Boot Custom Test Annotations
package com.maoudia;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.TestPropertySource;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation for integration tests. Marks a test class as an integration test and configures it for Spring Boot testing.
* Integration tests focus on testing the integration of various components of an application.
*
* <p>Example usage:</p>
* <pre>{@code
* &#64;IntegrationTest
* class MyIntegrationTest {
* // Integration test methods
* }
* }</pre>
*
* <p>Integration tests marked with this annotation are loaded in the Spring application context
* with the profile "it" activated. They are also annotated with Spring's {@code @SpringBootTest}
* with a random web environment port, {@code @Import} to import additional configurations,
* and {@code @TestPropertySource} to specify property sources for the test environment.</p>
*
* @see org.springframework.boot.test.context.SpringBootTest
* @see org.springframework.context.annotation.Import
* @see org.springframework.context.annotation.Profile
* @see org.springframework.test.context.TestPropertySource
* @see IntegrationTestConfig
*/
@Profile("it")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import(IntegrationTestConfig.class)
@TestPropertySource(locations = "classpath:application-it.yml")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IntegrationTest {
}
package com.maoudia;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
/**
* Test configuration class for integration tests. Configures security settings to disable OAuth2 security for testing purposes.
* This configuration is intended to be used in integration tests to simplify security configuration.
*
* <p>Example usage:</p>
* <pre>{@code
* &#64;IntegrationTest
* class MyIntegrationTest {
* // Integration test methods
* }
* }</pre>
*
* <p>This configuration class is annotated with {@code @TestConfiguration}, indicating that it provides
* configuration specifically for tests. It defines a bean {@code disableOAuth2Security} that disables
* CSRF protection and permits all HTTP requests for testing purposes.</p>
*
* @see org.springframework.boot.test.context.TestConfiguration
* @see org.springframework.context.annotation.Bean
* @see org.springframework.security.config.annotation.web.builders.HttpSecurity
* @see org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer
* @see org.springframework.security.web.SecurityFilterChain
*/
@TestConfiguration
public class IntegrationTestConfig {
/**
* Configures a security filter chain to disable OAuth2 security for integration tests.
*
* @param http the {@link HttpSecurity} to configure
* @return the {@link SecurityFilterChain} configured to disable OAuth2 security
* @throws Exception if an error occurs while configuring security
*/
@Bean
public SecurityFilterChain disableOAuth2Security(HttpSecurity http) throws Exception {
return http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorize -> authorize
.anyRequest().permitAll())
.build();
}
}
package com.maoudia;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.context.annotation.Profile;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation for unit tests. Used to mark a test class as a unit test and configure it for Mockito testing.
* Unit tests focus on testing individual units of code in isolation.
* This annotation is intended to be used in conjunction with JUnit 5 and Mockito.
*
* <p>Example usage:</p>
* <pre>{@code
* &#64;UnitTest
* class MyUnitTest {
* // Unit test methods
* }
* }</pre>
*
* <p>Unit tests marked with this annotation are loaded in the Spring application context
* with the profile "test" activated. They are also annotated with Mockito's {@code @ExtendWith}
* to enable Mockito support in JUnit 5, and {@code @MockitoSettings} to configure the strictness
* of Mockito mocks.</p>
*
* @see org.junit.jupiter.api.extension.ExtendWith
* @see org.mockito.junit.jupiter.MockitoExtension
* @see org.mockito.junit.jupiter.MockitoSettings
* @see org.mockito.quality.Strictness
*/
@Profile("test")
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UnitTest {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment