Java @MockMvc不适用于带有@Valid注释的验证
这些解决方案在这里都没有帮助:。添加了所有这些依赖项,没有任何帮助 我使用Spring Boot 2.1.2,pom.xml:Java @MockMvc不适用于带有@Valid注释的验证,java,spring-boot,hibernate-validator,spring-boot-test,spring-mvc-test,Java,Spring Boot,Hibernate Validator,Spring Boot Test,Spring Mvc Test,这些解决方案在这里都没有帮助:。添加了所有这些依赖项,没有任何帮助 我使用Spring Boot 2.1.2,pom.xml: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</versio
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
employee.xml:
<dataset>
<Employee id="1" name="John" role="admin"/>
<Employee id="2" name="Mike" role="user"/>
</dataset>
当我通过@MockMvc进行测试时,我无法理解为什么它不起作用。我做错了什么?状态正确,但没有错误内容,响应为空
但是,如果在真正运行的应用程序上进行测试,验证就会起作用,那么一切都会起作用。您能更新您的问题以使其完整吗?目前缺少
ID
和EmployeeRepository
类。您的测试以及employee.xml
文件中也缺少PATH
常量。如果我尝试填充示例中缺少的部分,调用控制器失败,出现MethodArgumentNotValidException
,消息为请提供名称
@AndyWilkinson您可以在my github@AndyWilkinson上看到整个项目。我在代码中添加了请求的修改。正在验证响应,并在您的示例中引发异常。您的测试失败是因为您确实在尝试测试Spring引导错误基础结构,而不是您自己的控制器。如果您想在测试中包括错误基础结构,您应该使用完整的web环境开始测试,并使用testrestemplate
而不是MockMvc
。
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
@DatabaseSetup("/employee.xml")
@TestExecutionListeners({
TransactionalTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class
})
public class EmployeeControllerWebApplicationTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
private static String employeeRouteWithParam = EmployeeController.PATH + "/{id}";
@Before
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.build();
}
@Test
public void create_WithoutName_ShouldThrowException() throws Exception {
String role = "admin";
Employee expectedEmployee = new Employee(null, role);
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(expectedEmployee);
ResultActions resultActions = this.mockMvc.perform(post(PATH)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(json))
.andDo(print());
String contentAsString = resultActions.andReturn().getResponse().getContentAsString();
System.out.println("content: " + contentAsString); // empty body
resultActions
.andExpect(status().isBadRequest())
.andExpect(jsonPath("error").exists()) // not exist!!!
.andExpect(jsonPath("timestamp").exists()); // not exist!!!
}
}
<dataset>
<Employee id="1" name="John" role="admin"/>
<Employee id="2" name="Mike" role="user"/>
</dataset>