如何使用MockMvc更新多对多关系的实体?

时间:2017-02-23 19:37:08

标签: java spring-boot spring-test mockmvc

我正在编写简单的spring-boot应用程序,并希望对其进行测试。我有User课程:

@Entity
@Table(name = "users")
@NoArgsConstructor
@AllArgsConstructor
public class User extends AbstractEntity {
    public enum Type {
        ADMIN, USER
    }

    @Getter
    @Setter
    private Type type;

    @Getter
    @Setter
    private String username;

    @Getter
    @Setter
    private String password;

    @ManyToMany(mappedBy = "managers")
    @Getter
    @Setter
    private Set<Label> label;
}

Label类(认为它是一个组织,其中User有效):

@Entity
@Table(name = "labels")
@NoArgsConstructor
@AllArgsConstructor
public class Label extends AbstractEntity {
    @Getter
    @Setter
    private String title;

    @OneToOne
    @Getter
    @Setter
    private User creator;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "label_managers")
    @Getter
    @Setter
    private Set<User> managers;
}

这些实体由@RepositoryRestResource s。

管理

我应该在MockMvc的测试中写一下UserLabel之间的关系?

1 个答案:

答案 0 :(得分:0)

&#34;使用&#34; Content-Type:text / uri-list&#34;而不是JSON,可以&#34;添加&#34;带有PUT的集合的资源并传入URI。您可以使用DELETE删除资源。&#34;

查看以下问题的答案:How to add elements in a many-to-many relationship via Spring's @RepositoryRestResource REST API?