From 9f31e46b0333dafbdab0e8d133a3ab3188ce2f97 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 26 Jul 2022 23:31:25 +0530 Subject: [PATCH] JAVA-13629 Added missing tests from Mockito ArgumentMatchers article --- .../baeldung/app/rest/FlowerController.java | 2 +- .../domain/service/FlowerService.java | 2 +- .../app/rest/FlowerControllerUnitTest.java | 80 ++++++++++++++----- .../app/rest/MessageControllerUnitTest.java | 51 +++++++----- 4 files changed, 96 insertions(+), 39 deletions(-) diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/FlowerController.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/FlowerController.java index b3d8888cec..10f602088c 100644 --- a/testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/FlowerController.java +++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/app/rest/FlowerController.java @@ -17,7 +17,7 @@ public class FlowerController { @PostMapping("/isAFlower") public String isAFlower (@RequestBody String flower) { - return flowerService.analize(flower); + return flowerService.analyze(flower); } @PostMapping("/isABigFlower") diff --git a/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/FlowerService.java b/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/FlowerService.java index 3c76cb5d76..74a9308890 100644 --- a/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/FlowerService.java +++ b/testing-modules/mockito-simple/src/main/java/com/baeldung/domain/service/FlowerService.java @@ -10,7 +10,7 @@ public class FlowerService { private List flowers = Arrays.asList("Poppy", "Ageratum", "Carnation", "Diascia", "Lantana"); - public String analize(String name) { + public String analyze(String name) { if(flowers.contains(name)) { return "flower"; } diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java index aeec57d136..df3561d646 100644 --- a/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java +++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/FlowerControllerUnitTest.java @@ -1,14 +1,20 @@ package com.baeldung.app.rest; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.AdditionalMatchers.or; import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.endsWith; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.exceptions.misusing.InvalidUseOfMatchersException; import org.mockito.junit.MockitoJUnitRunner; import com.baeldung.app.api.Flower; @@ -17,29 +23,67 @@ import com.baeldung.domain.service.FlowerService; @RunWith(MockitoJUnitRunner.class) public class FlowerControllerUnitTest { - @Mock - private FlowerService flowerService; + @InjectMocks + private FlowerController flowerController; - @InjectMocks - private FlowerController flowerController; + @Mock + private FlowerService flowerService; - @Test - public void isAFlower_withMockito_OK() { - when(flowerService.analize(eq("violetta"))).thenReturn("Flower"); + @Test + public void givenPoppyFlower_whenUsingDoReturn_thenCorrect() { + doReturn("Flower").when(flowerService).analyze("poppy"); - String response = flowerController.isAFlower("violetta"); + String response = flowerController.isAFlower("poppy"); + assertThat(response).isEqualTo("Flower"); + } - Assert.assertEquals("Flower", response); - } + @Test + public void givenAnyString_whenUsingArgumentMatcher_thenCorrect() { + when(flowerService.analyze(anyString())).thenReturn("Flower"); - @Test - public void isABigFlower_withMockito_OK() { - when(flowerService.isABigFlower(eq("violetta"), anyInt())).thenReturn(true); + String response = flowerController.isAFlower("violetta"); + assertThat(response).isEqualTo("Flower"); + } - Flower flower = new Flower("violetta", 15); + @Test(expected = InvalidUseOfMatchersException.class) + public void whenIncorrectMatchers_thenThrowsError() { + when(flowerService.isABigFlower("poppy", anyInt())).thenReturn(true); - Boolean response = flowerController.isABigFlower(flower); + Flower flower = new Flower("poppy", 15); - Assert.assertTrue(response); - } + Boolean response = flowerController.isABigFlower(flower); + assertThat(response).isTrue(); + } + + @Test + public void whenCorrectMatchers_thenCorrect() { + when(flowerService.isABigFlower(eq("poppy"), anyInt())).thenReturn(true); + + Flower flower = new Flower("poppy", 15); + + Boolean response = flowerController.isABigFlower(flower); + assertThat(response).isTrue(); + } + + @Test(expected = InvalidUseOfMatchersException.class) + public void whenUsingMatchersAsReturnValue_thenThrowsError() { + flowerController.isAFlower("poppy"); + + String orMatcher = or(eq("poppy"), endsWith("y")); + verify(flowerService).analyze(orMatcher); + } + + @Test + public void whenUsingMatchersAsOngoingStubbing_thenCorrect1() { + flowerController.isAFlower("poppy"); + + verify(flowerService).analyze(or(eq("poppy"), endsWith("y"))); + } + + @Test + public void whenUsingMatchersAsOngoingStubbing_thenCorrect2() { + flowerController.isAFlower("lily"); + + verify(flowerService).analyze(or(eq("poppy"), endsWith("y"))); + } } diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java index 15e91d86b5..ae368c4c34 100644 --- a/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java +++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/app/rest/MessageControllerUnitTest.java @@ -1,13 +1,14 @@ package com.baeldung.app.rest; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentMatchers; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import com.baeldung.app.api.MessageApi; @@ -18,26 +19,38 @@ import com.baeldung.domain.util.MessageMatcher; @RunWith(MockitoJUnitRunner.class) public class MessageControllerUnitTest { - @Mock - private MessageService messageService; + @Mock + private MessageService messageService; - @InjectMocks - private MessageController messageController; + @InjectMocks + private MessageController messageController; - @Test - public void createMessage_NewMessage_OK() { - MessageApi messageApi = new MessageApi(); - messageApi.setFrom("me"); - messageApi.setTo("you"); - messageApi.setText("Hello, you!"); + @Test + public void givenMsg_whenVerifyUsingAnyMatcher_thenOk() { + MessageApi messageApi = new MessageApi(); + messageApi.setFrom("me"); + messageApi.setTo("you"); + messageApi.setText("Hello, you!"); - messageController.createMessage(messageApi); + messageController.createMessage(messageApi); - Message message = new Message(); - message.setFrom("me"); - message.setTo("you"); - message.setText("Hello, you!"); + verify(messageService, times(1)).deliverMessage(any(Message.class)); + } - Mockito.verify(messageService, times(1)).deliverMessage(ArgumentMatchers.argThat(new MessageMatcher(message))); - } + @Test + public void givenMsg_whenVerifyUsingMessageMatcher_thenOk() { + MessageApi messageApi = new MessageApi(); + messageApi.setFrom("me"); + messageApi.setTo("you"); + messageApi.setText("Hello, you!"); + + messageController.createMessage(messageApi); + + Message message = new Message(); + message.setFrom("me"); + message.setTo("you"); + message.setText("Hello, you!"); + + verify(messageService, times(1)).deliverMessage(argThat(new MessageMatcher(message))); + } }