I want to use Unit Test for my RESTful API based on Spring Framework, I used mysql to save my data and using PagingAndSortingRepository to implemented with my RESTful API, and this is my test code:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringMvcApplication.class)
@WebAppConfiguration
public class CustomerRepositoryTests {
private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
SUBTYPE);
private static final String SUBTYPE = "hal+json";
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
private CustomerRepository customerRepository;
private long setupId;
@Before
public void setup() {
this.mockMvc = webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.build();
customerRepository.deleteAll();
Customer customer = customerRepository.save(new Customer("userId", "my mobile", "my address", "my contactName"));
setupId = customer.getId();
}
@Test
//// FIXME: 6/26/16 Status Code is always 204, not 200!
public void changeCustomer() throws Exception {
mockMvc.perform(put("/api" + "/customers/{id}", setupId)
.content(TestUtil.objToJson(new Customer("my new userId", "my new mobile", "my new address", "my new contactName")))
.contentType(contentType))
.andExpect(status().isOk())
.andExpect(jsonPath("$.userId", is("my new userId")))
.andExpect(jsonPath("$.contactName", is("my new contactName")))
.andExpect(jsonPath("$.mobile", is("my new mobile")))
.andExpect(jsonPath("$.address", is("my new address")));
}
}
my test always failed and says:
java.lang.AssertionError: Status
Expected :200
Actual :204
but when I run my application and use a command like:
curl -X PUT -H "Content-Type:application/hal+json" -d '{ "userId": "Bilbo", "mobile": "Baggins", "contactName":"my new contact", "address":"new address" }' http://localhost:8080/api/customers/1
my server returns status code 200 and updates the data successfully. I searched through for a while but have no idea about this.
Aucun commentaire:
Enregistrer un commentaire