user21597761
user21597761

Reputation:

Java Mockito Can't mock the result of method

I have a problem with testing (mocking the value for method) my delete method from controller . In ordinary mode it works fine but not when I test.

Here is my code.

My Controller

@RestController
@RequestMapping("/")
public class MainController {

    @DeleteMapping(value = "/deletePost/{id}")
public ResponseEntity<String> deletePost(@PathVariable int id) throws SQLException {
    boolean isRemoved = postsService.deletePost(connection, id);

    if (!isRemoved)
        return new ResponseEntity<>("Post with given id was not found", HttpStatus.NOT_FOUND);
    else {
        modifiedPostsService.insertModificationData(connection, new ModificationData(id, "deletion"));
        return new ResponseEntity<>("Post with given id has been deleted.", HttpStatus.OK);
    }
}
}

My PostsService

    public boolean deletePost(Connection connection, int id) throws SQLException {
    return postsDao.deletePost(connection, id);
}

My PostsDao

    @Override
public boolean deletePost(Connection connection, int id) throws SQLException {
    boolean isPostExists = isPostExist(connection, id);
    PreparedStatement ps;
    ps = connection.prepareStatement("delete from POSTS where ID = " + id);
    ps.executeUpdate();
    return isPostExists;
}

And finally my tests

@WebMvcTest(MainController.class)
class MainControllerTests {

@Autowired
private MockMvc mockMvc;

@MockBean
private Connection connection;

@MockBean
private PostsService mockPostsService;

@Test
void testIfDeletePostUrlIsOk() throws Exception {
    Mockito.when(mockPostsService.deletePost(connection, 1)).thenReturn(true);
    mockMvc.perform(MockMvcRequestBuilders
            .delete("/deletePost/1")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk());
}

}

testIfDeletePostUrlIsOk() returns 404 instead of 200 ( I guess mocking value - true not works, is false instead). Why and how to solve that?

Upvotes: 2

Views: 1308

Answers (2)

Krzysztof K
Krzysztof K

Reputation: 784

@SpringBootTest
@AutoConfigureMockMvc
public class TestingWebApplicationTest {

 @Autowired
 private MockMvc mockMvc;

 @MockBean     
 Connection mockConnection;

 @MockBean
 PostsService mockPostsService;

 @Test
 void testIfDeletePostUrlIsOk() throws Exception {
 Mockito.when(mockPostsService.deletePost(any(), anyInt())).thenReturn(true);
 mockMvc.perform(MockMvcRequestBuilders
        .delete("/deletePost/1")
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk());
 }
}

You need to inject your mocks into controller, annotation @SpringBootTest and @MockBean will do the work

Upvotes: 1

MSH
MSH

Reputation: 91

when you use:

Mockito.mock(...)

you are creating a mock object on your local scope, you have to inject it to your SUT or use:

@MockBean

MockBean will make your object accessible by spring's ApplicationContext so spring can pick your mock.

Upvotes: 0

Related Questions