我正在为spring数据库创建测试用例:
@RepositoryRestResource(collectionResourceRel = "teams", path = "teams")
public interface TeamRepo extends JpaRepository<Team, Long> {}
一切正常但是当我用put方法更新无效资源时,mockmvc总是返回状态204。 代码如下:
@Test
@Transactional
public void updateInvalidResourceFail_thenReturn400() throws Exception {
final Team resource = createNewResource();
invalidate(resource); // Some properties violate uniqueness constraints
final String resourceAsString = marshaller.encode(resource);
mvc.perform(
put("/api/teams/1")
.contentType(marshaller.getMime())
.content(resourceAsString)
).andExpect(status().isBadRequest()).andDo(print());
TestTransaction.flagForCommit();
TestTransaction.end();
}
mockmvc:
protected MockMvc mvc;
@Autowired
private WebApplicationContext context;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}
错误:
java.lang.AssertionError: Status
Expected :400
Actual :204
然而,PostMan使用相同的无效数据返回正确的结果:
{
"status": "BAD_REQUEST",
"instant": "2018-06-13T06:16:20.975Z",
"message": "Database error, Constraint Violation",
"debug": "could not execute statement"
}
当我将期望状态更改为204时,
.andExpect(status().isNoContent()).andDo(print());
我得到了预期的例外:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
原因:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: #23000
看起来那个mockmvc总是返回204.我哪里错了?
实体团队:
@Entity
@ToString
@DynamicUpdate
@DynamicInsert
@Table(name = "bus_team", schema = "test", uniqueConstraints = {
@UniqueConstraint(columnNames = {
"name"
})
})
public class Team{
private Long id;
private String name;
private String description;
private String division;
// ... getter, setter
}
方法无效(团队团队):
void invalidate(Team team){
team.setName("name already exists");
}
答案 0 :(得分:0)
我认为这种行为发生的原因有两个。
首先,您正在进行PUT,而不是尝试在数据库中创建新对象,而只是使用修改后的值更新现有对象。因此,不会违反唯一约束,因为之前的值只是用新的值覆盖了。
第二个可能的原因是,当您使用mockmvc运行测试时,它将使用内存中的数据源。当您使用Postman运行测试时,它可能使用不同的数据源(取决于您的配置)。因此,mockmvc测试将从一个新的数据库开始,包含Postman测试数据库中的不同数据。因此,测试可能不会触发约束违规,除非您确保mockmvc测试使用的内存数据库中已存在正确的数据。希望这是有道理的!您确定内存数据源包含触发您期望的完整性约束所需的数据吗?