Spring Test Flashcards

1
Q

What is the difference between unit and integration testing?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False

The Spring TestContext creates an ApplicationContext for your tests

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False

The Spring TestContext caches an ApplicationContext between @Test methods by default

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a test suite?

A

In the context of Spring, a test suite is a test class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True or False

The TestContext rolls back a transaction for each @Test method by default

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What bean must be defined in the test’s ApplicationContext to provide transactional support?

A

PlatformTransactionManager

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which annotation can be used to persist database transactions performed by @Test methods?

A

@Commit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which annotation can be used to specify a properties resource location for test classes?

A

@TestPropertySource

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which annotation can be used to create a new ApplicationContext after a @Test method executes?

A

@DirtiesContext

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

True or False

Although field injection is discouraged in production code, field injection is actually quite natural in test code

A

True

The rationale for the difference is that you will never instantiate your test class directly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

@BeforeTransaction and @AfterTransaction

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

@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
} }
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which annotation can be used to repeat an arbitrary amount of test methods?

A

@RepeatedTest

This annotation should replace @Test when used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

True or False

@WebMvcTest autoconfigures MockMvc

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When should you use @Mock or @MockBean?

A

@Mock should be used when no ApplicationContext is loaded. @MockBean should be used when an ApplicationContext is loaded

17
Q

The underlying component scan configuration of @SpringBootApplication defines exclude filters that are used to make sure slicing works as expected

A

True

18
Q

By default, @SpringBootTest will not call your main method, and instead the class itself is used directly to create the ApplicationContext

A

True

19
Q

Which Spring Boot starter is required to be on the classpath to test Spring MVC with WebTestClient?

A

Spring Boot Webflux

20
Q

What is the purpose of @ContextConfiguration?

A

To load and configure an ApplicationContext for tests

21
Q

What 2 annotations make up @SpringJunitConfig?

A
  1. @ExtendWith(SpringExtension.class)
  2. @ContextConfiguration