0

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?

user3764893
  • 667
  • 4
  • 15
  • 32

1 Answers1

1

You're using Mockito to inject mock service beans into your TestController (in particular, ITestService). All mocks by definition have no behaviour until you specify it, by default all operations you perform will either do nothing or return null. You can easily confirm that by setting a breakpoint inside TestController.delete method, executing the test in debug mode and inspecting values of test and testService variables.

Mockito is used for unit-level tests that replace SUT's collaborators with a mock that you set up to behave in a certain verifiable way. Once you call a method on your SUT (in your case that's TestController) you can assert whether it adheres to its contract or not.

It's actually a big no-no to allow your automated tests to modify a real instance of a database.

kryger
  • 11,746
  • 8
  • 41
  • 60
  • Well I inspected values of `test` and `testService` and only the value of `test` was null, the other one wasn't. Is there any way to delete that instance of database? If not then what is the purpose of having this kind of testing? It even return `OK` for non-existing entities in the database. – user3764893 Jul 23 '15 at 11:25
  • Your further questions are quite broad and already answered in many places, for instance http://stackoverflow.com/q/3622455/1240557 – kryger Jul 23 '15 at 11:45
  • `It's actually a big no-no to allow your automated tests to modify a real instance of a database.` I tested the services with `Junit` for spring and all operations were successful including delete. Does that mean we cannot delete an entity in database using mockito? – user3764893 Jul 23 '15 at 12:04