JAVA-12032 Move Mockito ebook articles code to common module - mockito-simple- Delete mockito-3 since it had only 1 article which was moved to mockito-simple

This commit is contained in:
Dhawal Kapil
2022-07-15 17:52:21 +05:30
parent 3860716e20
commit 24d4feb541
60 changed files with 111 additions and 435 deletions
@@ -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;
}
}
@@ -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());
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -0,0 +1,41 @@
package com.baeldung.junit5.mockito;
public class User {
private Integer id;
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
@@ -0,0 +1,9 @@
package com.baeldung.junit5.mockito.repository;
import com.baeldung.junit5.mockito.User;
public interface MailClient {
void sendUserRegistrationMail(User user);
}
@@ -0,0 +1,9 @@
package com.baeldung.junit5.mockito.repository;
public interface SettingRepository {
int getUserMinAge();
int getUserNameMinLength();
}
@@ -0,0 +1,10 @@
package com.baeldung.junit5.mockito.repository;
import com.baeldung.junit5.mockito.User;
public interface UserRepository {
User insert(User user);
boolean isUsernameAlreadyExists(String userName);
}
@@ -0,0 +1,46 @@
package com.baeldung.junit5.mockito.service;
import com.baeldung.junit5.mockito.User;
import com.baeldung.junit5.mockito.repository.MailClient;
import com.baeldung.junit5.mockito.repository.SettingRepository;
import com.baeldung.junit5.mockito.repository.UserRepository;
public class DefaultUserService implements UserService {
private UserRepository userRepository;
private SettingRepository settingRepository;
private MailClient mailClient;
public DefaultUserService(UserRepository userRepository, SettingRepository settingRepository, MailClient mailClient) {
this.userRepository = userRepository;
this.settingRepository = settingRepository;
this.mailClient = mailClient;
}
@Override
public User register(User user) {
validate(user);
User insertedUser = userRepository.insert(user);
mailClient.sendUserRegistrationMail(insertedUser);
return insertedUser;
}
private void validate(User user) {
if(user.getName() == null) {
throw new RuntimeException(Errors.USER_NAME_REQUIRED);
}
if(user.getName().length() < settingRepository.getUserNameMinLength()) {
throw new RuntimeException(Errors.USER_NAME_SHORT);
}
if(user.getAge() < settingRepository.getUserMinAge()) {
throw new RuntimeException(Errors.USER_AGE_YOUNG);
}
if(userRepository.isUsernameAlreadyExists(user.getName())) {
throw new RuntimeException(Errors.USER_NAME_DUPLICATE);
}
}
}
@@ -0,0 +1,10 @@
package com.baeldung.junit5.mockito.service;
public class Errors {
public static final String USER_NAME_REQUIRED = "user.name.required";
public static final String USER_NAME_SHORT = "user.name.short";
public static final String USER_AGE_YOUNG = "user.age.young";
public static final String USER_NAME_DUPLICATE = "user.name.duplicate";
}
@@ -0,0 +1,9 @@
package com.baeldung.junit5.mockito.service;
import com.baeldung.junit5.mockito.User;
public interface UserService {
User register(User user);
}
@@ -0,0 +1,7 @@
package com.baeldung.mockito.argumentcaptor;
public enum AuthenticationStatus {
AUTHENTICATED,
NOT_AUTHENTICATED,
ERROR
}
@@ -0,0 +1,14 @@
package com.baeldung.mockito.argumentcaptor;
public class Credentials {
private final String name;
private final String password;
private final String key;
public Credentials(String name, String password, String key) {
this.name = name;
this.password = password;
this.key = key;
}
}
@@ -0,0 +1,10 @@
package com.baeldung.mockito.argumentcaptor;
public interface DeliveryPlatform {
void deliver(Email email);
String getServiceStatus();
AuthenticationStatus authenticate(Credentials credentials);
}
@@ -0,0 +1,47 @@
package com.baeldung.mockito.argumentcaptor;
public class Email {
private String address;
private String subject;
private String body;
private Format format;
public Email(String address, String subject, String body) {
this.address = address;
this.subject = subject;
this.body = body;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Format getFormat() {
return format;
}
public void setFormat(Format format) {
this.format = format;
}
}
@@ -0,0 +1,36 @@
package com.baeldung.mockito.argumentcaptor;
public class EmailService {
private DeliveryPlatform platform;
public EmailService(DeliveryPlatform platform) {
this.platform = platform;
}
public void send(String to, String subject, String body, boolean html) {
Format format = Format.TEXT_ONLY;
if (html) {
format = Format.HTML;
}
Email email = new Email(to, subject, body);
email.setFormat(format);
platform.deliver(email);
}
public ServiceStatus checkServiceStatus() {
if (platform.getServiceStatus().equals("OK")) {
return ServiceStatus.UP;
} else {
return ServiceStatus.DOWN;
}
}
public boolean authenticatedSuccessfully(Credentials credentials) {
if (platform.authenticate(credentials).equals(AuthenticationStatus.AUTHENTICATED)) {
return true;
} else {
return false;
}
}
}
@@ -0,0 +1,6 @@
package com.baeldung.mockito.argumentcaptor;
public enum Format {
TEXT_ONLY,
HTML
}
@@ -0,0 +1,7 @@
package com.baeldung.mockito.argumentcaptor;
public enum ServiceStatus {
UP,
DOWN,
AUTHENTICATED
}
@@ -0,0 +1,22 @@
package com.baeldung.mockito.mockedstatic;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class StaticUtils {
private StaticUtils() {
}
public static List<Integer> range(int start, int end) {
return IntStream.range(start, end)
.boxed()
.collect(Collectors.toList());
}
public static String name() {
return "Baeldung";
}
}