075. Annotations for Testing - dkkahm/study-springfamework5 GitHub Wiki
JUint 4 Annotations
- @Test
- Identified a method as a test method
- @Before
- Executed before each test
- It is used to prepare the test environment
- e.g., read input data, initialize the class
- JUnit5: @BeforeEach
- @After
- Executed after each test
- It is used to cleanup the test environment
- It can also save memory by cleaning up expensive memory structures.
- JUnit5: @AfterEach
- @BeforeClass
- Executed once, before the start of all tests.
- Methods marked with this annotation need to be defined as static to work with JUnit
- JUnit5: @BeforeAll
- @AfterClass
- Executed once, after all tests have been finished
- Methods marked with this annotation need to be defined as static to work with JUnit
- JUnit5: @AfterAll
- @Ignore
- marks that test should be disasbled
- Junit5: @Disabled
- @Test(expected = Foo.class)
- Fails if the method does not throw the named exception.
- JUnit5: Assertions.assertThrows(Foo.class...
- @ Test(timeout = 1)
- Fails if the method takes longer than 100miliseconds.
- Assertions.assertTimeout(Duration...)
Spring Boot Annotations
- @RunWith(SpringRunner.class)
- Run test with Spring Context
- @RunWith(SpringJUnit4ClassRunner.class)
- JUnit5: @ExtendWith(SpringExtension.class)
- @SpringBootTest
- Search for Spring Boot Application for configuration
- @TestConfiguration
- Specify a Spring configuration for your test
- @MockBean
- @SpyBean
- @JsonTest
- Creates a Jackson or Gson object mapper via Spring Boot
- @WebMvcTest
- Used to test web context without a full http server
- @DataJpaTest
- Used to test data layer with embedded database
- @JdbcTest
- Like @DataJpaTest, but does not config entity manager
- @DataMongoTeste
- Configures an embedded MongoDB for testing
- @RestClientTest
- Create a mock server for testing rest clients
- @AutoConfigureRestDocks
- Allow you to use Spring Rest Docs in tests, creating API documentation
- @BootStrapWith
- Used to configure how the TestContext is bootstrapped
- @ContextConfiguration
- Used to direct Spring how to configure the context for the test.
- @ContextHierachy
- Allows you to create a context hierachy with @ContextConfiguration
- @ActiveProfiles
- Set with Spring Profiles are active for the test
- @TestPropertySource
- Configure the property sources for the test
- @DirtiesContext
- Resets the Spring Context after the test (expensive to do)
- @WebAppConfiguration
- Idicates Spring should use a Web Application context
- @TestExecutionListeners
- Allows you to specify listeners for testing events
- @Transactional
- Run test in transaction, rollback when complete by default
- @BeforeTransaction
- Action to run before starting transaction
- @AfterTransaction
- Action to run after a transaction
- @Commit
- Specifies the transaction should be committed after the test
- @Rollback
- Transaction should be rolled back after test. (Default action)
- @Sql
- Specify SQL scripts to run before
- @SqlConfig
- Define meta data for SQL scripts
- @SqlGroup
- Group of @Sql annotations
- @Repeat
- Repeat test x number of times
- @Timed
- Similar to JUnit's timeout, but will wait for test to complete, unlike JUnit
- @IfProfileValid
- Indicates test is enabled for a specific testing environment
- @ProfileValueSourceConfiguration
- Specify a profile value source