以下测试失败,并显示:org.json.JSONException: Unparsable JSON string
. 我尝试了各种模拟saveAndFlush
方法的变体
// doAnswer(returnsFirstArg()).when(this.shipwreckRepository).saveAndFlush(shipwreck);
// when(this.shipwreckRepository.saveAndFlush(shipwreck))
// .then(returnsFirstArg());
但是,弹出相同的错误MockHttpServletResponse
并且正文为空
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {}
Content type = null
Body =
如何正确模拟仓库saveAndFlush
方法返回值?
春季启动主页控制器测试.java
@RunWith(SpringRunner.class)
@WebMvcTest(ShipwreckController.class)
public class SpringBootShipwreckControllerTests {
@Autowired
private MockMvc mvc;
@MockBean
private ShipwreckRepository shipwreckRepository;
@Test
public void createShipwreck() throws Exception {
Shipwreck shipwreck = generateSampleShipwreck();
when(shipwreckRepository.saveAndFlush(shipwreck)).then(i -> i.getArgumentAt(0, Shipwreck.class));
ObjectMapper mapper = new ObjectMapper();
String shipwreckJson = mapper.writeValueAsString(shipwreck);
this.mvc.perform(post("/api/v1/shipwrecks")
.contentType(MediaType.APPLICATION_JSON)
.content(shipwreckJson)
)
.andExpect(status().isOk())
.andExpect(content().json(shipwreckJson));
verify(this.shipwreckRepository).saveAndFlush(shipwreck);
}
private Shipwreck generateSampleShipwreck() {
Integer depth = 10;
Double latitude = 1.0;
Double longitude = 1.0;
Integer yearDiscovered = 1;
String name = "name";
String description = "desc";
String condition = "cond";
Shipwreck shipwreck = new Shipwreck(1L, name, description, condition, depth, latitude, longitude, yearDiscovered);
return shipwreck;
}
}
沉船存储库.java
public interface ShipwreckRepository extends JpaRepository<Shipwreck, Long> {
}
沉船管制员.java
@RestController
@RequestMapping("/api/v1/")
public class ShipwreckController {
@Autowired
private ShipwreckRepository shipwreckRepository;
@RequestMapping(value = "shipwrecks", method = RequestMethod.POST)
public Shipwreck create(@RequestBody Shipwreck shipwreck) {
return shipwreckRepository.saveAndFlush(shipwreck);
}
}
沉船.java
package com.boot.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Shipwreck {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
String description;
String condition;
Integer depth;
Double latitude;
Double longitude;
Integer yearDiscovered;
public Shipwreck() { }
public Shipwreck(Long id, String name, String description, String condition, Integer depth, Double latitude, Double longitude, Integer yearDiscovered) {
this.id = id;
this.name = name;
this.description = description;
this.condition = condition;
this.depth = depth;
this.latitude = latitude;
this.longitude = longitude;
this.yearDiscovered = yearDiscovered;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public Integer getDepth() {
return depth;
}
public void setDepth(Integer depth) {
this.depth = depth;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Integer getYearDiscovered() {
return yearDiscovered;
}
public void setYearDiscovered(Integer yearDiscovered) {
this.yearDiscovered = yearDiscovered;
}
}
在 when() 和 verify() 指令中使用 Mockito.any() 而不是 Shipwreck 的特定实例。
测试控制器将接收的 Shipwreck 实例不会是您创建的实例,而是通过反序列化您用来调用它的 json 构建的新实例。因此,Mockito不会响应告诉使用前一个实例的指令。
替代方案:为 Shipwreck 定义适当的 equals() 和 hashCode() 方法。