Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JUnit ServiceTest for delete method which tells me Wanted but not invoked

I am stuck at a delete test now.

Here is the delete method of PostServiceImpl.class

@Override
public void deletePostById(Long id) {
    Post post = postRepository.findById(id)
            .orElseThrow(() -> new ResourceNotFoundException("post", "id", id));

    postRepository.delete(post);
}

PostServiceTest.class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@BeforeEach
public void setup() {
    postDto = new PostDto();
    postDto.setId(1L);
    postDto.setTitle("test title");
    postDto.setDescription("test description");
    postDto.setContent("test content");

    post = new Post();
    post.setId(1L);
    post.setTitle("test title");
    post.setDescription("test description");
    post.setContent("test content");
}

@Test
void givenPostId_whenDeletePost_thenNothing() {

    Long postId = 1L;

    BDDMockito.given(postRepository.findById(postId))
            .willReturn(Optional.of(post));

    BDDMockito.willDoNothing().given(postRepository).deleteById(postId);

    postService.deletePostById(postId);

    Mockito.verify(postRepository, Mockito.times(1)).findById(1L);
    Mockito.verify(postRepository, Mockito.times(1)).deleteById(1L);
}

and this is the output:

Wanted but not invoked:
postRepository.deleteById(1L);
-> at com.springboot.blog.service.PostServiceTest.givenPostId_whenDeletePost_thenNothing(PostServiceTest.java:148)

However, there were exactly 2 interactions with this mock:
postRepository.findById(1L);
-> at com.springboot.blog.service.PostServiceImpl.deletePostById(PostServiceImpl.java:95)

postRepository.delete(
Post(id=1, title=test title, description=test description, content=test content, createdTime=null, updatedTime=null));
-> at com.springboot.blog.service.PostServiceImpl.deletePostById(PostServiceImpl.java:98)

I do not really know what mistakes I made and any assistance would be appreciated.
Thanks!

>Solution :

You are checking whether

postRespository.deleteById(1l);

is being called once here

Mockito.verify(postRepository, Mockito.times(1)).deleteById(1L);

But your PostServiceImpl.class is not calling that method. It is calling the

postRepository.delete(post);

which takes the Post class as parameter. Change the verification statement to

Mockito.verify(postRepository, Mockito.times(1)).delete(post);

and it should work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading