模拟 mvc 放置方法始终返回 204 状态



我正在为我的 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();
}

模拟MVC:

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");
}

我认为这种行为发生有两个潜在原因。

首先,您正在执行 PUT,而不是尝试在数据库中创建新对象,它只是使用修改后的值更新现有对象。 因此,不会违反唯一约束,因为以前的值只是被新值覆盖。

第二个可能的原因是,当您使用 mockmvc 运行测试时,它将使用内存中的数据源。 当您使用 Postman 运行测试时,它可能使用不同的数据源(取决于您的配置(。 因此,mockmvc 测试将从一个新的数据库开始,其中包含来自 Postman 测试数据库的不同数据。 因此,测试可能不会触发约束冲突,除非您确保 mockmvc 测试使用的内存数据库中已有正确的数据。 希望这是有道理的! 是否确定内存中数据源包含触发预期完整性约束所需的数据?

最新更新