JAVA-13629 Added missing tests from Mockito ArgumentMatchers article

This commit is contained in:
Dhawal Kapil 2022-07-26 23:31:25 +05:30
parent 48461ed6cc
commit 9f31e46b03
4 changed files with 96 additions and 39 deletions

View File

@ -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")

View File

@ -10,7 +10,7 @@ public class FlowerService {
private List<String> flowers = Arrays.asList("Poppy", "Ageratum", "Carnation", "Diascia", "Lantana");
public String analize(String name) {
public String analyze(String name) {
if(flowers.contains(name)) {
return "flower";
}

View File

@ -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")));
}
}

View File

@ -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)));
}
}