java - 集合时如何测试json api
问题描述
我读到最好在 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 集合集合私有集合产品;
然后我尝试测试返回结果:
@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...)提供您要检查的所有元素。
像这样的东西:
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());
推荐阅读
- mulesoft - 无法安装 MULE EE 服务 指定的服务已存在 (0x411)
- java - 面向对象设计:可扩展和可维护的汽车商店系统
- kubernetes - 如何检查 Kubernetes 注释中的小于条件?
- python - 按索引选择嵌套在列表中的字典
- angular - 如何在 Angular 中的 patchValue 时防止/停止 valuechanges 事件
- html - 如何防止我的粘性导航栏在我的 div 元素下移动?
- javascript - 在 JavaScript 中单击一个按钮并在每次单击时获得不同的颜色
- python - 如何在 numpy 矩阵中转置数组?
- c# - 如何将 Xamarin.Plugin.ImageEdit 与用户界面集成?
- git - 我怎样才能在主人之前撞到一个分支?