Spring & Testing Annotations - Definitions and Examples
Core Spring Boot
@SpringBootApplication
Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan. It serves as the entry point
of a Spring Boot application.
This annotation is commonly used in scenarios where @SpringBootApplication helps simplify the
configuration or enhance the readability and maintainability of the code. It's an essential tool for developers
working in Spring or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with
the Spring ecosystem or testing tools.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@Component
Used to mark a Java class as a component so that Spring can detect it during component scanning.
This annotation is commonly used in scenarios where @Component helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@Component
public class MyComponent {
public void doWork() {
System.out.println("Working...");
}
}
@Autowired
Marks a constructor, field, or setter method to be autowired by Spring's dependency injection.
This annotation is commonly used in scenarios where @Autowired helps simplify the configuration or
Spring & Testing Annotations - Definitions and Examples
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@Autowired
private MyComponent myComponent;
@Bean
Used on methods to define Spring-managed beans within a @Configuration class.
This annotation is commonly used in scenarios where @Bean helps simplify the configuration or enhance the
readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Bean
public MyService myService() {
return new MyServiceImpl();
}
@Configuration
Indicates that the class contains @Bean definitions for the Spring context.
This annotation is commonly used in scenarios where @Configuration helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
Web Annotations (Spring MVC)
@RestController
Spring & Testing Annotations - Definitions and Examples
A convenience annotation that combines @Controller and @ResponseBody. It is used to build RESTful web
services.
This annotation is commonly used in scenarios where @RestController helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@RestController
public class MyRestController {
@GetMapping("/hello")
public String sayHello() {
return "Hello World";
}
}
@RequestMapping
Maps HTTP requests to handler methods of MVC and REST controllers.
This annotation is commonly used in scenarios where @RequestMapping helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@RequestMapping("/users")
public class UserController { }
@GetMapping
Shortcut for @RequestMapping(method = RequestMethod.GET).
This annotation is commonly used in scenarios where @GetMapping helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@GetMapping("/users")
public List<User> getAllUsers() { return userService.findAll(); }
Spring & Testing Annotations - Definitions and Examples
@PostMapping
Shortcut for @RequestMapping(method = RequestMethod.POST).
This annotation is commonly used in scenarios where @PostMapping helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@PostMapping("/users")
public User createUser(@RequestBody User user) {
return userService.save(user);
}
@PutMapping
Shortcut for @RequestMapping(method = RequestMethod.PUT).
This annotation is commonly used in scenarios where @PutMapping helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@PutMapping("/users/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User user) {
return userService.update(id, user);
}
@DeleteMapping
Shortcut for @RequestMapping(method = RequestMethod.DELETE).
This annotation is commonly used in scenarios where @DeleteMapping helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable Long id) {
userService.delete(id);
}
Spring & Testing Annotations - Definitions and Examples
@RequestBody
Indicates a method parameter should be bound to the body of the HTTP request.
This annotation is commonly used in scenarios where @RequestBody helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@PostMapping("/add")
public ResponseEntity<User> addUser(@RequestBody User user) {
return ResponseEntity.ok(userService.save(user));
}
@PathVariable
Binds a method parameter to a URI template variable.
This annotation is commonly used in scenarios where @PathVariable helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@GetMapping("/users/{id}")
public User getUser(@PathVariable Long id) {
return userService.findById(id);
}
Spring Data (MongoDB/JPA)
@Document
Marks a class as a domain object that will be persisted to MongoDB.
This annotation is commonly used in scenarios where @Document helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@Document(collection = "users")
public class User {
Spring & Testing Annotations - Definitions and Examples
@Id
private String id;
}
@Id
Defines the primary key field for MongoDB or JPA entities.
This annotation is commonly used in scenarios where @Id helps simplify the configuration or enhance the
readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Id
private String id;
@Indexed
Indicates that the field should be indexed in the MongoDB collection.
This annotation is commonly used in scenarios where @Indexed helps simplify the configuration or enhance
the readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Indexed(unique = true)
private String email;
@DBRef
Used to declare references between documents in MongoDB.
This annotation is commonly used in scenarios where @DBRef helps simplify the configuration or enhance
the readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@DBRef
private List<Role> roles;
@Transactional
Spring & Testing Annotations - Definitions and Examples
Specifies that a method or class should be executed within a transaction.
This annotation is commonly used in scenarios where @Transactional helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@Transactional
public void updateData() {
repository.save(data);
}
@EnableTransactionManagement
Enables Spring's annotation-driven transaction management capability.
This annotation is commonly used in scenarios where @EnableTransactionManagement helps simplify the
configuration or enhance the readability and maintainability of the code. It's an essential tool for developers
working in Spring or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with
the Spring ecosystem or testing tools.
@Configuration
@EnableTransactionManagement
public class TransactionConfig { }
Lombok
@Data
Generates getters, setters, equals, hashCode, and toString methods.
This annotation is commonly used in scenarios where @Data helps simplify the configuration or enhance the
readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Data
public class User {
private String name;
private int age;
Spring & Testing Annotations - Definitions and Examples
@Builder
Generates a builder pattern for the class.
This annotation is commonly used in scenarios where @Builder helps simplify the configuration or enhance
the readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Builder
public class User {
private String name;
private int age;
}
@NonNull
Generates null-checks for annotated fields or parameters.
This annotation is commonly used in scenarios where @NonNull helps simplify the configuration or enhance
the readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
public User(@NonNull String name) {
this.name = name;
}
@NoArgsConstructor
Generates a no-argument constructor.
This annotation is commonly used in scenarios where @NoArgsConstructor helps simplify the configuration
or enhance the readability and maintainability of the code. It's an essential tool for developers working in
Spring or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@NoArgsConstructor
public class User { }
Spring & Testing Annotations - Definitions and Examples
@Slf4j
Adds a static SLF4J logger to the class.
This annotation is commonly used in scenarios where @Slf4j helps simplify the configuration or enhance the
readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Slf4j
public class MyClass {
public void logSomething() {
log.info("Logging with Lombok!");
}
}
JUnit & Testing
@SpringBootTest
Used to test Spring Boot applications with a full context.
This annotation is commonly used in scenarios where @SpringBootTest helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@SpringBootTest
public class MyAppTests { }
@Test
Marks a method as a test case in JUnit 5.
This annotation is commonly used in scenarios where @Test helps simplify the configuration or enhance the
readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Test
void testSum() {
Spring & Testing Annotations - Definitions and Examples
assertEquals(4, 2 + 2);
}
@ParameterizedTest
Allows a test method to be executed multiple times with different arguments.
This annotation is commonly used in scenarios where @ParameterizedTest helps simplify the configuration
or enhance the readability and maintainability of the code. It's an essential tool for developers working in
Spring or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testWithMultipleValues(int value) {
assertTrue(value > 0);
}
@Disabled
Disables a test method or class.
This annotation is commonly used in scenarios where @Disabled helps simplify the configuration or enhance
the readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Disabled
@Test
void skippedTest() { }
@CsvSource
Provides CSV data for @ParameterizedTest.
This annotation is commonly used in scenarios where @CsvSource helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@ParameterizedTest
Spring & Testing Annotations - Definitions and Examples
@CsvSource({"1,one", "2,two"})
void testCsv(int number, String word) {
assertNotNull(word);
}
@ValueSource
Provides a simple source of literal values for @ParameterizedTest.
This annotation is commonly used in scenarios where @ValueSource helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@ParameterizedTest
@ValueSource(strings = {"Hello", "JUnit"})
void testStrings(String input) {
assertNotNull(input);
}
@EnumSource
Supplies enum constants for @ParameterizedTest.
This annotation is commonly used in scenarios where @EnumSource helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@ParameterizedTest
@EnumSource(TimeUnit.class)
void testEnum(TimeUnit unit) {
assertNotNull(unit);
}
@BeforeEach
Executed before each test method.
This annotation is commonly used in scenarios where @BeforeEach helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
Spring & Testing Annotations - Definitions and Examples
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@BeforeEach
void setup() {
// initialize before each test
}
@AfterEach
Executed after each test method.
This annotation is commonly used in scenarios where @AfterEach helps simplify the configuration or
enhance the readability and maintainability of the code. It's an essential tool for developers working in Spring
or testing frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring
ecosystem or testing tools.
@AfterEach
void teardown() {
// clean up
}
@BeforeAll
Executed once before all test methods.
This annotation is commonly used in scenarios where @BeforeAll helps simplify the configuration or enhance
the readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@BeforeAll
static void init() {
// setup before all tests
}
@AfterAll
Executed once after all test methods.
This annotation is commonly used in scenarios where @AfterAll helps simplify the configuration or enhance
Spring & Testing Annotations - Definitions and Examples
the readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@AfterAll
static void cleanup() {
// cleanup after all tests
}
@Mock
Creates a mock instance of a class using Mockito.
This annotation is commonly used in scenarios where @Mock helps simplify the configuration or enhance the
readability and maintainability of the code. It's an essential tool for developers working in Spring or testing
frameworks, as it abstracts away boilerplate code and integrates seamlessly with the Spring ecosystem or
testing tools.
@Mock
private UserService userService;