JerseyTest将空实体传递给球衣REST资源

时间:2016-04-26 10:43:20

标签: java junit jersey jetty hibernate-validator

测试:

public class OrderResourceTest extends JerseyTest {
    @Override
    public Application configure() {
        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);
        return new ResourceConfig(OrderResource.class);
    }
    @Test
    public void testCreate(){
        Order order = new Order(0, new Customer(0, "CompanyName", "Street", "123456", "City", "UK"), Arrays.asList(new OrderLine(0, "s345664lkdLDSDf", "Samsung Galaxy 4", 1)));
        Response output = target("/orders")
                .request()
                .post(Entity.entity(order, MediaType.APPLICATION_JSON));
        assertEquals("Should return status 201", 201, output.getStatus());
    }
}

JUnit使用org.glassfish.jersey.test.jetty.JettyTestContainerFactory$JettyTestContainerHibernate Validator 5.1.3.Final

运行测试

JUnit输出:

INFO: 1 * Sending client request on thread main
1 > POST http://localhost:9998/orders
1 > Content-Type: application/json
{"customer":{"customerId":0},"orderId":0,"orderLines":[{}]}

org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL []; NULL not allowed for column "COMPANY_NAME"; SQL statement

org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Client response received on thread main
1 < 201
1 < Content-Type: application/json
1 < Location: http://localhost:8080/api/orders/0
1 < Server: Jetty(9.1.1.v20140108)
1 < Transfer-Encoding: chunked
{"customer":{"customerId":0},"orderId":0,"orderLines":[{}]}

OrderResource

@Path("/orders")
public class OrderResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response createOrder(Order order) {
        Order savedOrder = new OrderService().saveOrder(order); 
        return Response.status(Response.Status.CREATED)
                .entity(savedOrder)
                .header("Location",
                        "http://localhost:8080/api/orders/"
                                + String.valueOf(savedOrder.getOrderId()))
                .build();
    }
}

使用Hibernate验证器注释的客户类:

public class Customer {
    private long customerId;
    @NotNull
    private String companyName;
    @NotNull
    private String street;
    @NotNull
    private String postalCode;
    @NotNull
    private String city;
    @NotNull
    private String country;

    public Customer(long customerId, String companyName, String street, String postalCode, String city, String country) {
        this.customerId = customerId;
        this.companyName = companyName;
        this.street = street;
        this.postalCode = postalCode;
        this.city = city;
        this.country = country;
    }

   // gettes, setters and empty constructor
}

为什么测试将空对象传递/发布到OrderResource?如何配置Hibernate Validator以便它返回适当的错误响应(目前它将错误的数据传递给数据库,因此有DataIntegrityViolationException)?如何使一切工作?

应用程序非常像这样: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/griz_jersey_intro/Grizzly-Jersey-Intro.html

更新 似乎编组或解组都是错误的。

发送: enter image description here接收: enter image description here

0 个答案:

没有答案