Spring Test Flashcards
What is the difference between unit and integration testing?
Unit testing is the testing of the smallest units of code in isolation. Dependencies are often mocked
Integration testing is the testing of units of code working together. Dependencies are rarely mocked
True or False
The Spring TestContext creates an ApplicationContext for your tests
True
True or False
The Spring TestContext caches an ApplicationContext between @Test methods by default
True
What is a test suite?
In the context of Spring, a test suite is a test class
True or False
The TestContext rolls back a transaction for each @Test method by default
True
What bean must be defined in the test’s ApplicationContext to provide transactional support?
PlatformTransactionManager
Which annotation can be used to persist database transactions performed by @Test methods?
@Commit
Which annotation can be used to specify a properties resource location for test classes?
@TestPropertySource
Which annotation can be used to create a new ApplicationContext after a @Test method executes?
@DirtiesContext
True or False
Although field injection is discouraged in production code, field injection is actually quite natural in test code
True
The rationale for the difference is that you will never instantiate your test class directly
Method-level lifecycle methods — for example, methods annotated with JUnit Jupiter’s @BeforeEach or @AfterEach — are run within a test-managed transaction. On the other hand, suite-level and class-level lifecycle methods — for example, methods annotated with JUnit Jupiter’s @BeforeAll or @AfterAll and methods annotated with TestNG’s @BeforeSuite, @AfterSuite, @BeforeClass, or @AfterClass — are not run within a test-managed transaction.
@BeforeTransaction and @AfterTransaction
@SpringJUnitConfig(TestConfig.class)
class OrderServiceIntegrationTests {
@RepeatedTest(10) void placeOrderRepeatedly(RepetitionInfo repetitionInfo, @Autowired OrderService orderService) { // use orderService from the test's ApplicationContext // and repetitionInfo from JUnit Jupiter } }
Which annotation can be used to repeat an arbitrary amount of test methods?
@RepeatedTest
This annotation should replace @Test when used
True or False
@WebMvcTest autoconfigures MockMvc
True