Moved spring-mockito to testing-modules (#12476)
Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
@@ -39,6 +39,7 @@
|
||||
<module>rest-assured</module>
|
||||
<module>rest-testing</module>
|
||||
<module>selenium-junit-testng</module>
|
||||
<module>spring-mockito</module>
|
||||
<module>spring-testing-2</module>
|
||||
<module>spring-testing</module>
|
||||
<module>test-containers</module>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
@@ -0,0 +1,7 @@
|
||||
## Spring Mockito
|
||||
|
||||
This module contains articles about Spring with Mockito
|
||||
|
||||
### Relevant Articles:
|
||||
- [Injecting Mockito Mocks into Spring Beans](https://www.baeldung.com/injecting-mocks-in-spring)
|
||||
- [Mockito ArgumentMatchers](https://www.baeldung.com/mockito-argument-matchers)
|
||||
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-mockito</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-mockito</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Injecting Mockito Mocks into Spring Beans</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MocksApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MocksApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class NameService {
|
||||
public String getUserName(String id) {
|
||||
return "Real user name";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
private NameService nameService;
|
||||
|
||||
@Autowired
|
||||
public UserService(NameService nameService) {
|
||||
this.nameService = nameService;
|
||||
}
|
||||
|
||||
public String getUserName(String id) {
|
||||
return nameService.getUserName(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.app.api;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
private Integer petals;
|
||||
|
||||
public Flower(String name, Integer petals) {
|
||||
this.name = name;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getPetals() {
|
||||
return petals;
|
||||
}
|
||||
|
||||
public void setPetals(Integer petals) {
|
||||
this.petals = petals;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.app.api;
|
||||
|
||||
public class MessageApi {
|
||||
private String from;
|
||||
private String to;
|
||||
private String text;
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.app.rest;
|
||||
|
||||
import com.baeldung.app.api.Flower;
|
||||
import com.baeldung.domain.service.FlowerService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/flowers")
|
||||
public class FlowerController {
|
||||
|
||||
@Autowired
|
||||
private FlowerService flowerService;
|
||||
|
||||
@PostMapping("/isAFlower")
|
||||
public String isAFlower (@RequestBody String flower) {
|
||||
return flowerService.analize(flower);
|
||||
}
|
||||
|
||||
@PostMapping("/isABigFlower")
|
||||
public Boolean isABigFlower (@RequestBody Flower flower) {
|
||||
return flowerService.isABigFlower(flower.getName(), flower.getPetals());
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.app.rest;
|
||||
|
||||
import com.baeldung.app.api.MessageApi;
|
||||
import com.baeldung.domain.model.Message;
|
||||
import com.baeldung.domain.service.MessageService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/message")
|
||||
public class MessageController {
|
||||
|
||||
@Autowired
|
||||
private MessageService messageService;
|
||||
|
||||
@PostMapping
|
||||
public Message createMessage (@RequestBody MessageApi messageDTO) {
|
||||
Message message = new Message();
|
||||
message.setText(messageDTO.getText());
|
||||
message.setFrom(messageDTO.getFrom());
|
||||
message.setTo(messageDTO.getTo());
|
||||
message.setDate(Date.from(Instant.now()));
|
||||
message.setId(UUID.randomUUID());
|
||||
|
||||
return messageService.deliverMessage(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.domain.model;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Message {
|
||||
|
||||
private String from;
|
||||
private String to;
|
||||
private String text;
|
||||
private Date date;
|
||||
private UUID id;
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
public void setFrom(String from) {
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
public String getTo() {
|
||||
return to;
|
||||
}
|
||||
|
||||
public void setTo(String to) {
|
||||
this.to = to;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.domain.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class FlowerService {
|
||||
|
||||
private List<String> flowers = Arrays.asList("Poppy", "Ageratum", "Carnation", "Diascia", "Lantana");
|
||||
|
||||
public String analize(String name) {
|
||||
if(flowers.contains(name)) {
|
||||
return "flower";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isABigFlower(String name, int petals) {
|
||||
if(flowers.contains(name)) {
|
||||
if(petals > 10) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.domain.service;
|
||||
|
||||
import com.baeldung.domain.model.Message;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MessageService {
|
||||
|
||||
public Message deliverMessage (Message message) {
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.domain.util;
|
||||
|
||||
import com.baeldung.domain.model.Message;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
|
||||
public class MessageMatcher implements ArgumentMatcher<Message> {
|
||||
|
||||
private Message left;
|
||||
|
||||
public MessageMatcher(Message message) {
|
||||
this.left = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Message right) {
|
||||
return left.getFrom().equals(right.getFrom()) &&
|
||||
left.getTo().equals(right.getTo()) &&
|
||||
left.getText().equals(right.getText()) &&
|
||||
right.getDate() != null &&
|
||||
right.getId() != null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
@Profile("test")
|
||||
@Configuration
|
||||
public class NameServiceTestConfiguration {
|
||||
@Bean
|
||||
@Primary
|
||||
public NameService nameService() {
|
||||
return Mockito.mock(NameService.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@ActiveProfiles("test")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = MocksApplication.class)
|
||||
public class UserServiceUnitTest {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private NameService nameService;
|
||||
|
||||
@Test
|
||||
public void whenUserIdIsProvided_thenRetrievedNameIsCorrect() {
|
||||
Mockito.when(nameService.getUserName("SomeId")).thenReturn("Mock user name");
|
||||
|
||||
String testName = userService.getUserName("SomeId");
|
||||
|
||||
Assert.assertEquals("Mock user name", testName);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.app.rest;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
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.junit.MockitoJUnitRunner;
|
||||
|
||||
import com.baeldung.app.api.Flower;
|
||||
import com.baeldung.domain.service.FlowerService;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class FlowerControllerUnitTest {
|
||||
|
||||
@Mock
|
||||
private FlowerService flowerService;
|
||||
|
||||
@InjectMocks
|
||||
private FlowerController flowerController;
|
||||
|
||||
@Test
|
||||
public void isAFlower_withMockito_OK() {
|
||||
when(flowerService.analize(eq("violetta"))).thenReturn("Flower");
|
||||
|
||||
String response = flowerController.isAFlower("violetta");
|
||||
|
||||
Assert.assertEquals("Flower", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isABigFlower_withMockito_OK() {
|
||||
when(flowerService.isABigFlower(eq("violetta"), anyInt())).thenReturn(true);
|
||||
|
||||
Flower flower = new Flower("violetta", 15);
|
||||
|
||||
Boolean response = flowerController.isABigFlower(flower);
|
||||
|
||||
Assert.assertTrue(response);
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.app.rest;
|
||||
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
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;
|
||||
import com.baeldung.domain.model.Message;
|
||||
import com.baeldung.domain.service.MessageService;
|
||||
import com.baeldung.domain.util.MessageMatcher;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MessageControllerUnitTest {
|
||||
|
||||
@Mock
|
||||
private MessageService messageService;
|
||||
|
||||
@InjectMocks
|
||||
private MessageController messageController;
|
||||
|
||||
@Test
|
||||
public void createMessage_NewMessage_OK() {
|
||||
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!");
|
||||
|
||||
Mockito.verify(messageService, times(1)).deliverMessage(ArgumentMatchers.argThat(new MessageMatcher(message)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.MocksApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MocksApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user