I'm developing the application by using Spring Data Rest. As you know, after creating simple repository interfaces, rest-endpoints are created by library.
Do I need to test these endpoints by integration tests? If yes, please provide any examples
I'm developing the application by using Spring Data Rest. As you know, after creating simple repository interfaces, rest-endpoints are created by library.
Do I need to test these endpoints by integration tests? If yes, please provide any examples
here is the code snippet. read the full tutorial here
@Entity
@Table(name = "person")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(min = 3, max = 20)
private String name;
// standard getters and setters, constructors
}
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
public Employee findByName(String name);
}
@RunWith(SpringRunner.class)
@SpringBootTest(
SpringBootTest.WebEnvironment.MOCK,
classes = Application.class)
@AutoConfigureMockMvc
@TestPropertySource(
locations = "classpath:application-integrationtest.properties")
public class EmployeeRestControllerIntegrationTest {
@Autowired
private MockMvc mvc;
@Autowired
private EmployeeRepository repository;
@Test
public void givenEmployees_whenGetEmployees_thenStatus200()
throws Exception {
createTestEmployee("bob");
mvc.perform(get("/api/employees")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content()
.contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$[0].name", is("bob")));
}
}
Aside from regular testing you can do with Spring (using MockMvc, RestAssured, RestTemplate etc), Traverson is a great API for specifically testing Spring HATEOAS. You can use it to test the validity of the links you are returning to your clients, see this example
With https://github.com/Cosium/hal-mock-mvc, you can test them as follow:
@AutoConfigureHalMockMvc
@SpringBootTest
class MyTest {
@Autowired
private HalMockMvc halMockMvc;
@Test
void test() {
halMockMvc
.follow("current-user")
.get()
.andExpect(status().isOk())
.andExpect(jsonPath("$.alias").value("jdoe"));
}
}