I have a simple controller defined as below:
@Controller
@RequestMapping("/rest/tests")
public class TestController {
@Autowired
private ITestService testService;
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void delete(@PathVariable Integer id)
{
Test test = testService.getById(id);
testService.delete(test);
}
}
I have been trying to test the delete method, and have not succeeded so far. The test I have written is pretty simple too.
public class MockmvcTest {
@InjectMocks
private TestController test;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(test).build();
}
@Test
public void myTest() throws Exception {
this.mockMvc.perform(delete("/rest/tests/{id}", new Integer(4)))
.andExpect(status().isOk()
}
}
I have tested the method using "advanced rest client" extension in chrome and it works as expected.
By working I mean that the entity with the given id
is deleted from database.
When myTest()
is executed the status code is still 200
, but the entity is not removed from the database.
What might be the reason behind this behavior?