Программисты JAVA общаются здесь
Anonymous
@WithMockUser не работает, все еще получает 401
Сообщение
Anonymous » 21 апр 2024, 14:24
Это мой весенний тест: я тестирую защищенную конечную точку, которая создает рецепты для данного пользователя.
Как правильно протестировать защищенную конечную точку?
Разве аннотации @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
1713698669
Anonymous
Это мой весенний тест: я тестирую защищенную конечную точку, которая создает рецепты для данного пользователя. Как правильно протестировать защищенную конечную точку? Разве аннотации @WithMockUser недостаточно ? Я все еще получаю 401. [code]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()); } } [/code] Заранее спасибо. Подробнее здесь: [url]https://stackoverflow.com/questions/78361301/withmockuser-not-working-still-getting-401[/url]