Merge pull request #12538 from dkapil/task/JAVA-13629

Task/java 13629
This commit is contained in:
kwoyke 2022-07-31 22:15:33 +02:00 committed by GitHub
commit 75db30f1c1
4 changed files with 96 additions and 39 deletions

View File

@ -17,7 +17,7 @@ public class FlowerController {
@PostMapping("/isAFlower") @PostMapping("/isAFlower")
public String isAFlower (@RequestBody String flower) { public String isAFlower (@RequestBody String flower) {
return flowerService.analize(flower); return flowerService.analyze(flower);
} }
@PostMapping("/isABigFlower") @PostMapping("/isABigFlower")

View File

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

View File

@ -1,14 +1,20 @@
package com.baeldung.app.rest; 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.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.endsWith;
import static org.mockito.ArgumentMatchers.eq; 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 static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import com.baeldung.app.api.Flower; import com.baeldung.app.api.Flower;
@ -17,29 +23,67 @@ import com.baeldung.domain.service.FlowerService;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class FlowerControllerUnitTest { public class FlowerControllerUnitTest {
@Mock
private FlowerService flowerService;
@InjectMocks @InjectMocks
private FlowerController flowerController; private FlowerController flowerController;
@Mock
private FlowerService flowerService;
@Test @Test
public void isAFlower_withMockito_OK() { public void givenPoppyFlower_whenUsingDoReturn_thenCorrect() {
when(flowerService.analize(eq("violetta"))).thenReturn("Flower"); doReturn("Flower").when(flowerService).analyze("poppy");
String response = flowerController.isAFlower("poppy");
assertThat(response).isEqualTo("Flower");
}
@Test
public void givenAnyString_whenUsingArgumentMatcher_thenCorrect() {
when(flowerService.analyze(anyString())).thenReturn("Flower");
String response = flowerController.isAFlower("violetta"); String response = flowerController.isAFlower("violetta");
assertThat(response).isEqualTo("Flower");
}
Assert.assertEquals("Flower", response); @Test(expected = InvalidUseOfMatchersException.class)
public void whenIncorrectMatchers_thenThrowsError() {
when(flowerService.isABigFlower("poppy", anyInt())).thenReturn(true);
Flower flower = new Flower("poppy", 15);
Boolean response = flowerController.isABigFlower(flower);
assertThat(response).isTrue();
} }
@Test @Test
public void isABigFlower_withMockito_OK() { public void whenCorrectMatchers_thenCorrect() {
when(flowerService.isABigFlower(eq("violetta"), anyInt())).thenReturn(true); when(flowerService.isABigFlower(eq("poppy"), anyInt())).thenReturn(true);
Flower flower = new Flower("violetta", 15); Flower flower = new Flower("poppy", 15);
Boolean response = flowerController.isABigFlower(flower); Boolean response = flowerController.isABigFlower(flower);
assertThat(response).isTrue();
}
Assert.assertTrue(response); @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; 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.times;
import static org.mockito.Mockito.verify;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import com.baeldung.app.api.MessageApi; import com.baeldung.app.api.MessageApi;
@ -25,7 +26,19 @@ public class MessageControllerUnitTest {
private MessageController messageController; private MessageController messageController;
@Test @Test
public void createMessage_NewMessage_OK() { public void givenMsg_whenVerifyUsingAnyMatcher_thenOk() {
MessageApi messageApi = new MessageApi();
messageApi.setFrom("me");
messageApi.setTo("you");
messageApi.setText("Hello, you!");
messageController.createMessage(messageApi);
verify(messageService, times(1)).deliverMessage(any(Message.class));
}
@Test
public void givenMsg_whenVerifyUsingMessageMatcher_thenOk() {
MessageApi messageApi = new MessageApi(); MessageApi messageApi = new MessageApi();
messageApi.setFrom("me"); messageApi.setFrom("me");
messageApi.setTo("you"); messageApi.setTo("you");
@ -38,6 +51,6 @@ public class MessageControllerUnitTest {
message.setTo("you"); message.setTo("you");
message.setText("Hello, you!"); message.setText("Hello, you!");
Mockito.verify(messageService, times(1)).deliverMessage(ArgumentMatchers.argThat(new MessageMatcher(message))); verify(messageService, times(1)).deliverMessage(argThat(new MessageMatcher(message)));
} }
} }