JAVA-13336 Refactor mockito modules (#12517)
* JAVA-13336 Refactor mockito modules * JAVA-13336 Fix Jenkins build
This commit is contained in:
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.mockito.additionalanswers;
|
||||
|
||||
public class Book {
|
||||
|
||||
private Long bookId;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
private int numberOfPages;
|
||||
|
||||
public Book(String title, String author, int numberOfPages) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.numberOfPages = numberOfPages;
|
||||
}
|
||||
|
||||
public Book(Long bookId, String title, String author, int numberOfPages) {
|
||||
this.bookId = bookId;
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.numberOfPages = numberOfPages;
|
||||
}
|
||||
|
||||
public Long getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public void setBookId(Long bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public int getNumberOfPages() {
|
||||
return numberOfPages;
|
||||
}
|
||||
|
||||
public void setNumberOfPages(int numberOfPages) {
|
||||
this.numberOfPages = numberOfPages;
|
||||
}
|
||||
}
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.mockito.additionalanswers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class BookRepository {
|
||||
public Book getByBookId(Long bookId) {
|
||||
return new Book(bookId, "To Kill a Mocking Bird", "Harper Lee", 256);
|
||||
}
|
||||
|
||||
public Book save(Book book) {
|
||||
return new Book(book.getBookId(), book.getTitle(), book.getAuthor(), book.getNumberOfPages());
|
||||
}
|
||||
|
||||
public Book selectRandomBook(Book bookOne, Book bookTwo, Book bookThree) {
|
||||
List<Book> selection = new ArrayList<>();
|
||||
selection.add(bookOne);
|
||||
selection.add(bookTwo);
|
||||
selection.add(bookThree);
|
||||
Random random = new Random();
|
||||
return selection.get(random.nextInt(selection.size()));
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.mockito.additionalanswers;
|
||||
|
||||
public class BookService {
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
public BookService(BookRepository bookRepository) {
|
||||
this.bookRepository = bookRepository;
|
||||
}
|
||||
|
||||
public Book getByBookId(Long id) {
|
||||
return bookRepository.getByBookId(id);
|
||||
}
|
||||
|
||||
public Book save(Book book) {
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
|
||||
public Book selectRandomBook(Book book1, Book book2, Book book3) {
|
||||
return bookRepository.selectRandomBook(book1, book2, book3);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.baeldung.mockito.fluentapi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Pizza {
|
||||
|
||||
public enum PizzaSize {
|
||||
LARGE, MEDIUM, SMALL;
|
||||
}
|
||||
|
||||
private String name;
|
||||
private PizzaSize size;
|
||||
private List<String> toppings;
|
||||
private boolean stuffedCrust;
|
||||
private boolean collect;
|
||||
private Integer discount;
|
||||
|
||||
private Pizza(PizzaBuilder builder) {
|
||||
this.name = builder.name;
|
||||
this.size = builder.size;
|
||||
this.toppings = builder.toppings;
|
||||
this.stuffedCrust = builder.stuffedCrust;
|
||||
this.collect = builder.collect;
|
||||
this.discount = builder.discount;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public PizzaSize getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public List<String> getToppings() {
|
||||
return toppings;
|
||||
}
|
||||
|
||||
public boolean isStuffedCrust() {
|
||||
return stuffedCrust;
|
||||
}
|
||||
|
||||
public boolean isCollecting() {
|
||||
return collect;
|
||||
}
|
||||
|
||||
public Integer getDiscount() {
|
||||
return discount;
|
||||
}
|
||||
|
||||
public static class PizzaBuilder {
|
||||
private String name;
|
||||
private PizzaSize size;
|
||||
|
||||
private List<String> toppings;
|
||||
private boolean stuffedCrust;
|
||||
private boolean collect;
|
||||
private Integer discount = null;
|
||||
|
||||
public PizzaBuilder() {
|
||||
}
|
||||
|
||||
public PizzaBuilder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PizzaBuilder size(PizzaSize size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PizzaBuilder withExtraTopping(String extraTopping) {
|
||||
if (this.toppings == null) {
|
||||
toppings = new ArrayList<>();
|
||||
}
|
||||
this.toppings.add(extraTopping);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PizzaBuilder withStuffedCrust(boolean stuffedCrust) {
|
||||
this.stuffedCrust = stuffedCrust;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PizzaBuilder willCollect(boolean collect) {
|
||||
this.collect = collect;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PizzaBuilder applyDiscount(Integer discount) {
|
||||
this.discount = discount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Pizza build() {
|
||||
return new Pizza(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.mockito.fluentapi;
|
||||
|
||||
import com.baeldung.mockito.fluentapi.Pizza.PizzaSize;
|
||||
|
||||
public class PizzaService {
|
||||
|
||||
private Pizza.PizzaBuilder builder;
|
||||
|
||||
public PizzaService(Pizza.PizzaBuilder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public Pizza orderHouseSpecial() {
|
||||
return builder.name("Special")
|
||||
.size(PizzaSize.LARGE)
|
||||
.withExtraTopping("Mushrooms")
|
||||
.withStuffedCrust(true)
|
||||
.withExtraTopping("Chilli")
|
||||
.willCollect(true)
|
||||
.applyDiscount(20)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.mockito.java8;
|
||||
|
||||
public class JobPosition {
|
||||
private String title;
|
||||
|
||||
public JobPosition() {}
|
||||
|
||||
public JobPosition(String title) {
|
||||
super();
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.mockito.java8;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface JobService {
|
||||
Optional<JobPosition> findCurrentJobPosition(Person person);
|
||||
|
||||
default boolean assignJobPosition(Person person, JobPosition jobPosition) {
|
||||
if (!findCurrentJobPosition(person).isPresent()) {
|
||||
person.setCurrentJobPosition(jobPosition);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Stream<JobPosition> listJobs(Person person);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.mockito.java8;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private JobPosition currentJobPosition;
|
||||
|
||||
public Person() {}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public JobPosition getCurrentJobPosition() {
|
||||
return currentJobPosition;
|
||||
}
|
||||
|
||||
public void setCurrentJobPosition(JobPosition currentJobPosition) {
|
||||
this.currentJobPosition = currentJobPosition;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.mockito.java8;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UnemploymentService {
|
||||
|
||||
boolean personIsEntitledToUnemploymentSupport(Person person);
|
||||
|
||||
Optional<JobPosition> searchJob(Person person, String searchString);
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.mockito.java8;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class UnemploymentServiceImpl implements UnemploymentService {
|
||||
private final JobService jobService;
|
||||
|
||||
public UnemploymentServiceImpl(JobService jobService) {
|
||||
this.jobService = jobService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean personIsEntitledToUnemploymentSupport(Person person) {
|
||||
Optional<JobPosition> optional = jobService.findCurrentJobPosition(person);
|
||||
|
||||
return !optional.isPresent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<JobPosition> searchJob(Person person, String searchString) {
|
||||
Stream<JobPosition> stream = jobService.listJobs(person);
|
||||
|
||||
return stream.filter((j) -> j.getTitle().contains(searchString)).findFirst();
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.mockito.mocksettings;
|
||||
|
||||
public abstract class AbstractCoffee {
|
||||
|
||||
protected String name;
|
||||
|
||||
protected AbstractCoffee(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
protected String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.mockito.mocksettings;
|
||||
|
||||
public class SimpleService {
|
||||
|
||||
public SimpleService(SpecialInterface special) {
|
||||
Runnable runnable = (Runnable) special;
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.mockito.mocksettings;
|
||||
|
||||
public interface SpecialInterface {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.mockito.objectmapper;
|
||||
|
||||
public class Flower {
|
||||
|
||||
private String name;
|
||||
private Integer petals;
|
||||
|
||||
public Flower(String name, Integer petals) {
|
||||
this.name = name;
|
||||
this.petals = petals;
|
||||
}
|
||||
|
||||
public Flower() {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.mockito.objectmapper;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class FlowerJsonStringValidator {
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
public FlowerJsonStringValidator(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public boolean flowerHasPetals(String jsonFlowerAsString) throws JsonProcessingException {
|
||||
Flower flower = objectMapper.readValue(jsonFlowerAsString, Flower.class);
|
||||
return flower.getPetals() > 0;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.mockito.whenvsdomethods;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
|
||||
public interface Employee {
|
||||
|
||||
String greet();
|
||||
|
||||
void work(DayOfWeek day);
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.mockito.whenvsdomethods;
|
||||
|
||||
public class IAmOnHolidayException extends RuntimeException {
|
||||
|
||||
}
|
||||
@@ -2,17 +2,11 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
Reference in New Issue
Block a user