我在Windows 10上有Ubuntu服务器实例。我有在Ubuntu上的localhost上运行的应用程序。 当我在Ubuntu终端上创建请求时:
curl http://localhost:8000/test \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Basic cJJdlJ26p62JG23j34==" \
-d '{
"test": {
"id": "564624232443234",
"type": "book"
}
}'
它可以工作并插入数据库中。
当我在Eclipse中使用上面相同的JSON值进行JUnit测试时:
public class Test extends Connection {
.......
@Test
public void test_Insert() {
Map<Object, String> mapRequest = new HashMap<>();
mapRequest.put("id", "564624232443234");
mapRequest.put("type", "book");
given().
contentType(ContentType.JSON).
header("Authorization", "Basic "+"cJJdlJ26p62JG23j34==").
body(mapRequest).
when().
post("test").
then().
statusCode(200);
}
它不起作用。返回我:
java.lang.AssertionError:1个期望失败。 预期状态代码为<200>,但为<422>。
如果仅按如下所示ping服务器,则响应正确(200)。
@Test
public void basicPingTest() {
given().when().get("/").then().statusCode(200);
}
对这个问题有什么想法吗? 谢谢
答案 0 :(得分:1)
您不会通过curl和RestAssured发送相同的请求。 卷曲:
{
"test": {
"id": "564624232443234",
"type": "book"
}
}
放心:
{
"id": "564624232443234",
"type": "book"
}
将地图添加为test
对象
public class Test extends Connection {
.......
@Test
public void test_Insert() {
Map<Object, String> mapRequest = new HashMap<>();
mapRequest.put("id", "564624232443234");
mapRequest.put("type", "book");
given().
contentType(ContentType.JSON).
header("Authorization", "Basic "+"cJJdlJ26p62JG23j34==").
body(Collections.singletonMap("test",mapRequest)).
when().
post("test").
then().
statusCode(200);
}
}