Basics Flashcards

1
Q

Narrow vs Broad Integration Tests

A

Narrow integration tests that exercise only part of the application and use test doubles for some components or external services. Some call these component tests or service tests to make the distinction.

Broad integration tests that need the whole application running and exercise the application through UI or network calls. Some call these system tests or end-to-end tests to make the distinction.

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

Narrow Integration Tests

A

The Spring Boot test slices like @WebMvcTest or @DataJpaTest that we saw earlier are narrow integration tests. They only load part of the application context and allow mocking of unneeded dependencies.

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

Broad Integration Tests

A

Broad integration tests that need the whole application running and exercise the application through UI or network calls.

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

@SpringBootTest:

A

Indicates that the annotated class is the Spring Boot test.
The Loads the complete application context.

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

@RunWith(SpringRunner.class):

A

The Specifies the class to run the tests.
The SpringRunner is the alias for SpringJUnit4ClassRunner.

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

@MockBean

A

The Mocks a Spring Bean for testing purposes.
Useful for the isolating the unit of the code being tested.
@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;
@MockBean
private ExternalService externalService;
// Test methods go here
}

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

Example

A

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHelloEndpoint() throws Exception {
mockMvc.perform(get(“/hello”))
.andExpect(status().isOk())
.andExpect(content().string(“Hello, World!”));
}
}

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

@TestConfiguration

A

The @TestConfiguration is used to the specify additional configuration for the tests. It is often used to define @Bean methods for the test-specific beans.

@TestConfiguration
public class TestConfig {
@Bean
public MyService myService() {
return new MyService();
}
}

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

Server Starting

A

By default, @SpringBootTest will not start a server. You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests run:

MOCK(Default) : Loads a web ApplicationContext and provides a mock web environment. Embedded servers are not started when using this annotation.
ANDOM_PORT: Loads a WebServerApplicationContext and provides a real web environment. Embedded servers are started and listen on a random port.
DEFINED_PORT: Loads a WebServerApplicationContext and provides a real web environment. Embedded servers are started and listen on a defined port (from your application.properties) or on the default port of 8080.
NONE: Loads an ApplicationContext by using SpringApplication but does not provide any web environment (mock or otherwise)

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

Testing with a mock environment

A

By default, @SpringBootTest does not start the server. If you have web endpoints that you want to test against this mock environment, you can additionally configure MockMvc

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MockMvcExampleTests {

@Autowired
private MockMvc mvc;

@Test
public void exampleTest() throws Exception {
	this.mvc.perform(get("/")).andExpect(status().isOk())
			.andExpect(content().string("Hello World"));
}

}

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

Testing with a mock environment 2 WebTestClient

A

Alternatively, you can configure a WebTestClient as shown in the following example:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebTestClient
public class MockWebTestClientExampleTests {

@Autowired
private WebTestClient webClient;

@Test
public void exampleTest() {
	this.webClient.get().uri("/").exchange().expectStatus().isOk()
			.expectBody(String.class).isEqualTo("Hello World");
}

}

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

Testing with a running server - WebTestClient

A

If you need to start a full running server, we recommend that you use random ports. If you use @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT), an available port is picked at random each time your test runs.
For convenience, tests that need to make REST calls to the started server can additionally @Autowire a WebTestClient, which resolves relative links to the running server and comes with a dedicated API for verifying responses

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class RandomPortWebTestClientExampleTests {

@Autowired
private WebTestClient webClient;

@Test
public void exampleTest() {
	this.webClient.get().uri("/").exchange().expectStatus().isOk()
			.expectBody(String.class).isEqualTo("Hello World");
}

}

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

Dependency Injection Support

A

With @SpringBootTest, beans defined in your Spring application context are injected into the test class, so you can work with @Autowired dependencies in your tests.

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

Web Environment Support

A

You can specify a web environment (e.g., MOCK, RANDOM_PORT, DEFINED_PORT, NONE) to test specific configurations:

MOCK (default): Starts a mock web environment.
RANDOM_PORT: Starts an embedded server on a random port for testing real HTTP interactions.
DEFINED_PORT: Uses the preconfigured port of the server.
NONE: No web environment setup.

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

Use Sparingly

A

@SpringBootTest tests can be slower due to loading the full context, so they’re often used sparingly in combination with lighter tests, like @WebMvcTest or @DataJpaTest, for more focused component testing.

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

Example Testing Service Layer

A

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyIntegrationTest {

@Autowired
private MyService myService;

@Test
void testServiceLayer() {
    // Test using the real service bean
    assertEquals("ExpectedResult", myService.someMethod());
} }
17
Q

Mocking the database

A

@SpringBootTest itself does not automatically mock the database. It loads the application context, which includes the real database configuration specified in your Spring Boot application. This means that if you have a database configured (e.g., MySQL, PostgreSQL), @SpringBootTest will connect to that actual database unless you set up a mock or use an in-memory database like H2 specifically for testing.

18
Q

Mocking In Memory Database

A

In-Memory Database: You can configure an in-memory database (like H2, HSQLDB) specifically for testing. Spring Boot will use it if you set it up in your application-test.properties or application.yml file and activate it in your test configuration.

19
Q

activate application-test.properties in the test configuration

A

Use @ActiveProfiles in the Test Class
@ActiveProfiles(“test”)

this is recommended approach

20
Q

WebTestClient

A

It can test endpoints in your application (when the server is running).
It can test endpoints without a running server by directly invoking controller methods in a mock environment.

21
Q

WebTestClient vs @WebMvcTest with MockMvc

A

WebTestClient - Often used in combination with @SpringBootTest to make real HTTP requests, testing the entire stack. Kind of like a rest template

@WebMvcTest - MVC Layer Testing: Primarily used to test Spring MVC controllers in isolation, without loading the entire application context.

Ideal for fast, focused tests on the controller layer without involving services, repositories, or other layers.

By default, @WebMvcTest only loads beans related to the web layer (controllers, @ControllerAdvice, filters), excluding services or repositories unless explicitly mocked.