Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

java - Start the IntegrationTest by calling REST endpoint in SpringBoot

I am assuming this is possible and is not against standards / Bad Practice.

We are writing end to end tests in SpringBoot @SpringBootTest for Micro Services and this end to end tests are in a separate module / project (like a microservice).

Plan is to write a controller with an endpoint and takes a profile as param, to run the test @SpringBootTest programmatically.

Questions:

  1. How do we run the test the in the way specified above.
  2. Should we still keep the tests in src/test instead of src/main, since in the end to end spring boot application, all that it has is just a Test class (@SpringBootTest) with some helper classes?
  3. Any other better ways/approaches/standards to organize this?
question from:https://stackoverflow.com/questions/65837481/start-the-integrationtest-by-calling-rest-endpoint-in-springboot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...