Использую Spring Boot 2.1.2, pom.xml:
Код: Выделить всё
org.springframework.boot
spring-boot-starter-parent
2.1.2.RELEASE
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-devtools
true
mysql
mysql-connector-java
runtime
com.h2database
h2
runtime
org.springframework.boot
spring-boot-starter-hateoas
org.springframework.boot
spring-boot-starter-test
test
com.github.springtestdbunit
spring-test-dbunit
1.3.0
test
org.dbunit
dbunit
2.5.4
test
com.jcabi
jcabi-matchers
1.3
test
Код: Выделить всё
@Entity
@Table(name="employee")
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
@NotNull(message = "Please provide name")
@Column(name = "name")
private String name;
@Column(name = "role")
private String role;
public Employee() {}
public Employee(String name, String role) {
this.name = name;
this.role = role;
}
}
@RestController
@RequestMapping(EmployeeController.PATH)
public class EmployeeController {
public final static String PATH = "/employees";
@Autowired
private EmployeeService service;
@PostMapping("")
public Employee newEmployee(@RequestBody @Valid Employee newEmployee) {
return service.save(newEmployee);
}
}
@Service
public class EmployeeService {
@Autowired
private EmployeeRepository repository;
public Employee save(Employee entity) {
return getRepository().save(entity);
}
}
@Repository
public interface EmployeeRepository extends JpaRepository {
}
Код: Выделить всё
@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!!!
}
}
Код: Выделить всё
Но проверка работает, если тестировать на реально работающем приложении, то все работает.
Подробнее здесь: https://stackoverflow.com/questions/550 ... with-valid
Мобильная версия