Как правильно протестировать защищенную конечную точку?
Разве аннотации @WithMockUser недостаточно ?
Я все еще получаю 401.
Код: Выделить всё
package com.joaogoncalves.recipes.controller;
import com.joaogoncalves.recipes.model.RecipeCreate;
import com.joaogoncalves.testcontainers.EnableTestContainers;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.security.test.context.support.WithMockUser;
import java.util.List;
import static io.restassured.RestAssured.given;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableTestContainers
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class RecipeControllerIT {
@LocalServerPort
private Integer port;
@BeforeEach
void setUp() {
RestAssured.baseURI = "http://localhost:" + port;
}
@Test
@Order(1)
@WithMockUser
public void testRecipeCreateOk() {
final RecipeCreate recipeCreate = new RecipeCreate(
"tomato soup",
"tomato soup with anchovies",
List.of("tomato", "water", "anchovies"),
List.of("peel the tomatoes", "add water", "boil for 30 minutes", "add the anchovies"),
"soup"
);
given()
.contentType(ContentType.JSON)
.body(recipeCreate)
.when()
.post("/api/recipe/new")
.then()
.statusCode(HttpStatus.OK.value());
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... etting-401