【发布时间】:2015-10-18 18:49:33
【问题描述】:
我正在尝试编写一个简单的测试类来模拟 RESTful Web 服务,通过 POST 方法创建客户。以下在assertEquals 失败,我收到400 Bad Request 响应。我不能使用调试器来观察堆栈跟踪。但是控制台告诉我以下...
信息:已启动监听器绑定到 [localhost:9998]
信息:[HttpServer] 已启动。
public class SimpleTest extends JerseyTestNg.ContainerPerMethodTest {
public class Customer {
public Customer() {}
public Customer(String name, int id) {
this.name = name;
this.id = id;
}
@JsonProperty("name")
private String name;
@JsonProperty("id")
private int id;
}
@Override
protected Application configure() {
return new ResourceConfig(MyService.class);
}
@Path("hello")
public static class MyService {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public final Response createCustomer(Customer customer) {
System.out.println("Customer data: " + customer.toString());
return Response.ok("customer created").build();
}
}
@Test
private void test() {
String json = "{" +
"\"name\": \"bill\", " +
"\"id\": 4" +
"}";
final Response response = target("hello").request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(json));
System.out.println(response.toString());
assertEquals(response.getStatus(), 200);
}
}
【问题讨论】:
标签: json jersey testng jersey-client jersey-test-framework