Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用MockMVC测试Spring RESTful put方法失败_Java_Spring_Rest_Unit Testing - Fatal编程技术网

Java 使用MockMVC测试Spring RESTful put方法失败

Java 使用MockMVC测试Spring RESTful put方法失败,java,spring,rest,unit-testing,Java,Spring,Rest,Unit Testing,我想对基于Spring框架的RESTful API使用单元测试,我用mysql保存数据,用RESTful API实现,这是我的测试代码: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringMvcApplication.class) @WebAppConfiguration public class CustomerRepositoryTes

我想对基于Spring框架的RESTful API使用单元测试,我用mysql保存数据,用RESTful API实现,这是我的测试代码:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = SpringMvcApplication.class)
    @WebAppConfiguration
    public class CustomerRepositoryTests {


        private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
        SUBTYPE);

        private static final String SUBTYPE = "hal+json";

        private MockMvc mockMvc;
        @Autowired
        private WebApplicationContext webApplicationContext;

        @Autowired
        private CustomerRepository customerRepository;

        private long setupId;

        @Before
        public void setup() {
            this.mockMvc = webAppContextSetup(webApplicationContext)
            .apply(springSecurity())
            .build();
            customerRepository.deleteAll();
            Customer customer = customerRepository.save(new Customer("userId", "my mobile", "my address", "my contactName"));
            setupId = customer.getId();
        }


        @Test
        //// FIXME: 6/26/16 Status Code is always 204, not 200!
        public void changeCustomer() throws Exception {
            mockMvc.perform(put("/api" + "/customers/{id}", setupId)
            .content(TestUtil.objToJson(new Customer("my new userId", "my new mobile", "my new address", "my new contactName")))
            .contentType(contentType))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.userId", is("my new userId")))
            .andExpect(jsonPath("$.contactName", is("my new contactName")))
            .andExpect(jsonPath("$.mobile", is("my new mobile")))
            .andExpect(jsonPath("$.address", is("my new address")));
        }
}
我的测试总是失败,并说:

    java.lang.AssertionError: Status 
    Expected :200
    Actual   :204
但当我运行应用程序并使用以下命令时:

     curl -X PUT -H "Content-Type:application/hal+json" -d '{ "userId": "Bilbo", "mobile": "Baggins", "contactName":"my new contact", "address":"new address" }' http://localhost:8080/api/customers/1
我的服务器返回状态代码200并成功更新数据。 我搜索了一会儿,但对此一无所知

已编辑: 这是我的客户地址:

    public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> {
    }

看起来测试使用的是默认配置,该配置将根据需要返回
204 No Content

对于公开的资源,我们使用一组默认状态代码:

  • 200 OK-用于普通GET请求

  • 201已创建-用于创建新资源的POST和PUT请求

  • 204无内容-如果配置设置为不返回资源的响应主体,则用于PUT、PATCH和DELETE请求 更新(RepositoryRestConfiguration.returnBodyOnUpdate)。如果 配置值设置为包括PUT的响应,200 OK 将为更新返回,将为资源返回创建的201 通过PUT创建。

如果配置值 (RepositoryRestConfiguration.returnBodyOnUpdate和 RepositoryRestConfiguration.returnBodyCreate)显式设置为 null,则将使用HTTP Accept标头的存在来确定 响应代码


看起来测试使用的是默认配置,该配置将根据需要返回
204 No Content

对于公开的资源,我们使用一组默认状态代码:

  • 200 OK-用于普通GET请求

  • 201已创建-用于创建新资源的POST和PUT请求

  • 204无内容-如果配置设置为不返回资源的响应主体,则用于PUT、PATCH和DELETE请求 更新(RepositoryRestConfiguration.returnBodyOnUpdate)。如果 配置值设置为包括PUT的响应,200 OK 将为更新返回,将为资源返回创建的201 通过PUT创建。

如果配置值 (RepositoryRestConfiguration.returnBodyOnUpdate和 RepositoryRestConfiguration.returnBodyCreate)显式设置为 null,则将使用HTTP Accept标头的存在来确定 响应代码


你能添加你的rest端点实现吗?@niekname我从这里提到过,我想我还没有对我的rest端点实现做些什么。也许我使用它的默认实现?你能添加你的rest端点实现吗?@niekname我从这里提到过,我想我还没有对我的rest端点实现做些什么。也许我使用它的默认实现?
@Data
@Entity
@Table(name = "customer")
public class Customer {
    @Id
    @GeneratedValue()
    private Long id;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created", nullable = false)
    private Date created;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated", nullable = false)
    private Date updated;

    @Version
    @JsonIgnore
    private Long version;

    @NotNull
    private String userId;

    //TODO: Limit type to 1, 2, 3 or 4
    private int type;

    private String companyName;

    private String phone;

    @NotNull
    private String mobile;

    @NotNull
    private String address;

    private String zip;

    @NotNull
    private String contactName;


    private String email;


    public Customer(String userId, String mobile, String address, String contactName) {
        this(userId, 1, null, null, mobile, address, null, contactName, null);
    }

    public Customer() {}


    public Customer(Long id, String userId, String mobile, String address, String contactName) {
        this(id, userId, 1, null, null, mobile, address, null, contactName, null);
    }

    public Customer(Long id, String userId, int type, String companyName, String phone, String mobile, String address, String zip, String contactName, String email) {
        this.id = id;
        this.userId = userId;
        this.type = type;
        this.companyName = companyName;
        this.phone = phone;
        this.mobile = mobile;
        this.address = address;
        this.zip = zip;
        this.contactName = contactName;
        this.email = email;
    }


    public Customer(String userId, int type, String companyName, String phone, String mobile, String address, String zip, String contactName, String email) {
        this.userId = userId;
        this.type = type;
        this.companyName = companyName;
        this.phone = phone;
        this.mobile = mobile;
        this.address = address;
        this.zip = zip;
        this.contactName = contactName;
        this.email = email;
    }  

    public Long getId() {
        return id;
    }


    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getMobile() {
        return mobile;
    } 

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }



    @PrePersist
    protected void onCreate() {
        updated = created = new Date();
    }

    @PreUpdate
    protected void onUpdate() {
        updated = new Date();
    }



    @Override
    public String toString() {
        return "Customer{" +
            "id=" + id +
            ", creation time=" + created +
            ", userId=" + userId +
            ", type=" + type +
            ", companyName='" + companyName + '\'' +
            ", phone='" + phone + '\'' +
            ", mobile='" + mobile + '\'' +
            ", address='" + address + '\'' +
            ", zip='" + zip + '\'' +
            ", contactName='" + contactName + '\'' +
            ", email='" + email + '\'' +
            '}';
    }
}