commit
75db30f1c1
|
@ -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")
|
||||||
|
|
|
@ -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";
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
@InjectMocks
|
||||||
private FlowerService flowerService;
|
private FlowerController flowerController;
|
||||||
|
|
||||||
@InjectMocks
|
@Mock
|
||||||
private FlowerController flowerController;
|
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("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
|
String response = flowerController.isAFlower("violetta");
|
||||||
public void isABigFlower_withMockito_OK() {
|
assertThat(response).isEqualTo("Flower");
|
||||||
when(flowerService.isABigFlower(eq("violetta"), anyInt())).thenReturn(true);
|
}
|
||||||
|
|
||||||
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")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -18,26 +19,38 @@ import com.baeldung.domain.util.MessageMatcher;
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class MessageControllerUnitTest {
|
public class MessageControllerUnitTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private MessageService messageService;
|
private MessageService messageService;
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private MessageController messageController;
|
private MessageController messageController;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createMessage_NewMessage_OK() {
|
public void givenMsg_whenVerifyUsingAnyMatcher_thenOk() {
|
||||||
MessageApi messageApi = new MessageApi();
|
MessageApi messageApi = new MessageApi();
|
||||||
messageApi.setFrom("me");
|
messageApi.setFrom("me");
|
||||||
messageApi.setTo("you");
|
messageApi.setTo("you");
|
||||||
messageApi.setText("Hello, you!");
|
messageApi.setText("Hello, you!");
|
||||||
|
|
||||||
messageController.createMessage(messageApi);
|
messageController.createMessage(messageApi);
|
||||||
|
|
||||||
Message message = new Message();
|
verify(messageService, times(1)).deliverMessage(any(Message.class));
|
||||||
message.setFrom("me");
|
}
|
||||||
message.setTo("you");
|
|
||||||
message.setText("Hello, you!");
|
|
||||||
|
|
||||||
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue