Question #1: Please see example test below.
Question #2: Best practice is to keep tests in src/test.
Question #3: No, I think you're good to go.
TestRestTemplate vs. WebTestClient
NOTE: As of 5.0 this class is in maintenance mode, with only minor
requests for changes and bugs to be accepted going forward. Please,
consider using the org.springframework.web.reactive.client.WebClient
which has a more modern API and supports sync, async, and streaming
scenarios.
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
Creating a Multi Module Project: https://spring.io/guides/gs/multi-module/
package no.mycompany.myapp.user;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.reactive.server.WebTestClient;
@ActiveProfiles("test")
@AutoConfigureWebTestClient
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LoginControllerTest {
private static final String API_1_0_LOGIN = "/api/1.0/login";
@Autowired
WebTestClient webTestClient;
@Autowired
UserRepository userRepository;
@BeforeEach
public void cleanup(){
userRepository.deleteAll();
}
@Test
public void postLogin_withoutUserCredentials_receiveUnauthorized() {
webTestClient.post()
.uri(API_1_0_LOGIN)
.exchange()
.expectStatus().isUnauthorized();
}
...
}
Please note that you'll need this dependency for WebTestClient
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…