我有一个接受输入流的 put 方法。我想在 JUnit 中使用放心调用此方法。
这是我用的:
with().body(inpustream).put("/service/1"); // i got error 404 forbidden.
最佳答案
POST 将返回状态码 201,PUT 将返回 200,POST 将创建一个新资源,而 PUT 将更新现有资源。这意味着我们必须像下面这样在 URI 本身中提及我们希望更新的资源。
import io.restassured.RestAssured;
import static io.restassured.RestAssured.*;
import java.util.HashMap;
import java.util.Map;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;
public class PUTMethod {
public static Map<String, String> map = new HashMap<String, String>();
@BeforeTest
public void putdata(){
map.put("userId", "2");
map.put("id", "19");
map.put("title", "this is projectdebug.com");
map.put("body", "i am testing REST api with REST-Assured and sending a PUT request.");
RestAssured.baseURI = "http://jsonplaceholder.typicode.com";
RestAssured.basePath = "/posts/";
}
@Test
public void testPUT(){
given()
.contentType("application/json")
.body(map)
.when()
.put("/100")
.then()
.statusCode(200)
.and()
.body("title", equalTo("this is projectdebug.com"));
}
}
访问http://www.projectdebug.com/send-put-request-using-rest-assured/ 获取更多信息。
关于java - 如何在 REST assured 中调用 put 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24574648/