java - 当集合设置时如何测试 json api

标签 java spring spring-test-mvc

我读到在 Hibernate 关系中最好使用 Set 而不是 List。

我创建了两个一对多关系的实体:

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, unique = true)
    private String name;

    @ManyToOne
    @JoinColumn(name = "company_id", nullable = false)
    private Company company;
}

@Entity
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, unique = true)
    private String name;

    @LazyCollection(LazyCollectionOption.TRUE)
    @OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
    private Set<Product> products;
}

关于 @OneToMany 设置集合私有(private)设置产品;

然后我尝试测试返回结果:

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class CompanyControllerTest {

    private static final String API_COMPANY = "/api/company/";

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .build();
    }

    @Test
    public void getById() throws Exception {
        int id = 1;

        this.mockMvc.perform(get(API_COMPANY + id))
                .andExpect(status().isOk())
                .andExpect(content().contentType(APPLICATION_JSON_UTF8))
                .andExpect(jsonPath("id", is(1)))
                .andExpect(jsonPath("$.name", is("Google")))
                .andExpect(jsonPath("$.products", hasSize(2)))
                .andExpect(jsonPath("$.products[0].id", is(1)))
                .andExpect(jsonPath("$.products[0].name", is("search engine")))
                .andExpect(jsonPath("$.products[0].company").doesNotExist())
                .andExpect(jsonPath("$.products[1].id", is(2)))
                .andExpect(jsonPath("$.products[1].name", is("adv.")))
                .andExpect(jsonPath("$.products[1].company").doesNotExist());
    }
}

但问题是产品列表是不断变化的,因为我使用的是Set。 事实证明,测试要么通过,要么失败,因为产品的顺序发生了变化。

我的问题是如何测试使用Set时的结果。

最佳答案

您可以使用[*]获取所有元素,并给出Matchers.containsInAnyOrder(T...)所有要检查的元素。 p>

类似这样的事情:

 this.mockMvc.perform(get(API_COMPANY + id))
            .andExpect(status().isOk())
            .andExpect(content().contentType(APPLICATION_JSON_UTF8))
            .andExpect(jsonPath("id", is(1)))
            .andExpect(jsonPath("$.name", is("Google")))
            .andExpect(jsonPath("$.products", hasSize(2)))
            .andExpect(jsonPath("$.products[*].id", Matchers.containsInAnyOrder(1, 2)))
            .andExpect(jsonPath("$.products[*].name", Matchers.containsInAnyOrder("search engine", "adv.")))
            .andExpect(jsonPath("$.products[*].company").doesNotExist());

关于java - 当集合设置时如何测试 json api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54292052/

相关文章:

spring - 将拦截器应用到作为 Spring @Component 连接的 Jersey RESTful 服务

maven - 用于 spring-test-mvc 的 jar

spring-boot - 在 Spring MVC 集成测试中处理我的自定义异常

java - 在 Java 中,如何在方法参数中正确使用抽象类?

java - 如何获得两个数字之间的最大质因数并将它们存储在数组中?

java - Spring Integration SFTP 上传非 XML 配置

java - Spring web flux WebClient : Connection rest by peers, #block 因错误而终止。在以下站点观察到错误

java - 在不写入文件的情况下将字节流作为 Java 中的进程执行

java - 如何访问 ZKoss 中的 session 范围实例