add all file to git
This commit is contained in:
parent
a4683ce0d6
commit
c18eb6d2ed
|
@ -0,0 +1,10 @@
|
|||
## Spring Data Reactive Project
|
||||
|
||||
This module contains articles about reactive Spring 5 Data
|
||||
|
||||
### The Course
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles
|
||||
- [Spring Data Reactive Repositories with MongoDB](https://www.baeldung.com/spring-data-mongodb-reactive)
|
||||
- [Spring Data MongoDB Tailable Cursors](https://www.baeldung.com/spring-data-mongodb-tailable-cursors)
|
|
@ -0,0 +1,131 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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-data-mongodb-reactive</artifactId>
|
||||
<name>spring-data-mongodb-reactive</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<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>io.projectreactor</groupId>
|
||||
<artifactId>reactor-core</artifactId>
|
||||
<version>${reactor-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.reactivex.rxjava2</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
<version>${spring-tx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
<executions>
|
||||
<!-- Replacing default-compile as it is treated specially by maven -->
|
||||
<execution>
|
||||
<id>default-compile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<!-- Replacing default-testCompile as it is treated specially by maven -->
|
||||
<execution>
|
||||
<id>default-testCompile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>java-compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>java-test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-tx.version>5.3.15</spring-tx.version>
|
||||
<httpclient.version>4.5.2</httpclient.version>
|
||||
<reactor-core.version>3.3.1.RELEASE</reactor-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,17 @@
|
|||
package com.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.reactive.Spring5ReactiveApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Spring5ReactiveApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
spring.mongodb.embedded.version=4.4.9
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>testing-modules</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,2 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>assertion-libraries</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,4 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding/<project>=UTF-8
|
|
@ -0,0 +1,2 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.apt.aptEnabled=false
|
|
@ -0,0 +1,9 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
|
||||
org.eclipse.jdt.core.compiler.compliance=11
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
|
||||
org.eclipse.jdt.core.compiler.processAnnotations=disabled
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=11
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="target/generated-sources/annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="ignore_optional_problems" value="true"/>
|
||||
<attribute name="m2e-apt" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>junit-5-basics</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,6 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/main/resources=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding//src/test/resources=UTF-8
|
||||
encoding/<project>=UTF-8
|
|
@ -0,0 +1,2 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.apt.aptEnabled=false
|
|
@ -0,0 +1,9 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
|
||||
org.eclipse.jdt.core.compiler.compliance=11
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
|
||||
org.eclipse.jdt.core.compiler.processAnnotations=disabled
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=11
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.constructor;
|
||||
|
||||
public class PaymentProcessor {
|
||||
|
||||
private final PaymentService paymentService;
|
||||
|
||||
public PaymentProcessor(PaymentService paymentService) {
|
||||
this.paymentService = paymentService;
|
||||
}
|
||||
|
||||
public PaymentProcessor() {
|
||||
this.paymentService = new PaymentService();
|
||||
}
|
||||
|
||||
public PaymentProcessor(String paymentMode) {
|
||||
this.paymentService = new PaymentService(paymentMode);
|
||||
}
|
||||
|
||||
public String processPayment(){
|
||||
return paymentService.processPayment();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.constructor;
|
||||
|
||||
public class PaymentService {
|
||||
|
||||
private final String paymentMode;
|
||||
|
||||
public PaymentService(String paymentMode) {
|
||||
this.paymentMode = paymentMode;
|
||||
}
|
||||
|
||||
public PaymentService() {
|
||||
this.paymentMode = "Cash";
|
||||
}
|
||||
|
||||
public String processPayment(){
|
||||
// simulate processing payment and returns the payment mode
|
||||
return this.paymentMode;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.nullmatcher;
|
||||
|
||||
class Helper {
|
||||
|
||||
String concat(String a, String b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.nullmatcher;
|
||||
|
||||
class Main {
|
||||
|
||||
Helper helper = new Helper();
|
||||
|
||||
String methodUnderTest() {
|
||||
return helper.concat("Baeldung", null);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.onemethodmultipleparams;
|
||||
|
||||
public class ExampleService {
|
||||
public int getValue(int arg) {
|
||||
return 1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class CacheManager {
|
||||
private final HashMap<String, Object> map;
|
||||
|
||||
private static CacheManager instance;
|
||||
|
||||
private CacheManager() {
|
||||
map = new HashMap<>();
|
||||
}
|
||||
|
||||
public static CacheManager getInstance() {
|
||||
if(instance == null) {
|
||||
instance = new CacheManager();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public <T> T getValue(String key, Class<T> clazz) {
|
||||
return clazz.cast(map.get(key));
|
||||
}
|
||||
|
||||
public Object setValue(String key, Object value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
public class Product {
|
||||
private String name;
|
||||
private String description;
|
||||
|
||||
public Product(String name, String description) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
public class ProductDAO {
|
||||
public Product getProduct(String productName) {
|
||||
|
||||
return new Product(productName, "description");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
public class ProductService {
|
||||
|
||||
private final ProductDAO productDAO;
|
||||
private final CacheManager cacheManager;
|
||||
|
||||
public ProductService(ProductDAO productDAO) {
|
||||
this.productDAO = productDAO;
|
||||
this.cacheManager = CacheManager.getInstance();
|
||||
}
|
||||
|
||||
public ProductService(ProductDAO productDAO, CacheManager cacheManager) {
|
||||
this.productDAO = productDAO;
|
||||
this.cacheManager = cacheManager;
|
||||
}
|
||||
|
||||
public Product getProduct(String productName) {
|
||||
Product product = cacheManager.getValue(productName, Product.class);
|
||||
if (product == null) {
|
||||
product = productDAO.getProduct(productName);
|
||||
}
|
||||
|
||||
return product;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.wantedbutnotinvocked;
|
||||
|
||||
class Helper {
|
||||
|
||||
String getBaeldungString() {
|
||||
return "Baeldung";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.wantedbutnotinvocked;
|
||||
|
||||
class Main {
|
||||
|
||||
Helper helper = new Helper();
|
||||
|
||||
String methodUnderTest(int i) {
|
||||
if (i > 5) {
|
||||
return helper.getBaeldungString();
|
||||
}
|
||||
return "Hello";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.constructor;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedConstruction;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
class PaymentServiceUnitTest {
|
||||
|
||||
@Test
|
||||
void whenConstructorInvokedWithInitializer_ThenMockObjectShouldBeCreated(){
|
||||
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class,(mock,context)-> when(mock.processPayment()).thenReturn("Credit"))){
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor();
|
||||
Assertions.assertEquals(1,mockPaymentService.constructed().size());
|
||||
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenConstructorInvokedWithoutInitializer_ThenMockObjectShouldBeCreatedWithNullFields(){
|
||||
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class)){
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor();
|
||||
Assertions.assertEquals(1,mockPaymentService.constructed().size());
|
||||
Assertions.assertNull(paymentProcessor.processPayment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenConstructorInvokedWithParameters_ThenMockObjectShouldBeCreated(){
|
||||
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class,(mock, context) -> when(mock.processPayment()).thenReturn("Credit"))){
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor("Debit");
|
||||
Assertions.assertEquals(1,mockPaymentService.constructed().size());
|
||||
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMultipleConstructorsInvoked_ThenMultipleMockObjectsShouldBeCreated(){
|
||||
try(MockedConstruction<PaymentService> mockPaymentService = Mockito.mockConstruction(PaymentService.class)){
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor();
|
||||
PaymentProcessor secondPaymentProcessor = new PaymentProcessor();
|
||||
PaymentProcessor thirdPaymentProcessor = new PaymentProcessor("Debit");
|
||||
|
||||
when(mockPaymentService.constructed().get(0).processPayment()).thenReturn("Credit");
|
||||
when(mockPaymentService.constructed().get(1).processPayment()).thenReturn("Online Banking");
|
||||
|
||||
Assertions.assertEquals(3,mockPaymentService.constructed().size());
|
||||
Assertions.assertEquals("Credit", paymentProcessor.processPayment());
|
||||
Assertions.assertEquals("Online Banking", secondPaymentProcessor.processPayment());
|
||||
Assertions.assertNull(thirdPaymentProcessor.processPayment());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDependencyInjectionIsUsed_ThenMockObjectShouldBeCreated(){
|
||||
PaymentService mockPaymentService = Mockito.mock(PaymentService.class);
|
||||
PaymentProcessor paymentProcessor = new PaymentProcessor(mockPaymentService);
|
||||
when(mockPaymentService.processPayment()).thenReturn("Online Banking");
|
||||
Assertions.assertEquals("Online Banking", paymentProcessor.processPayment());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.nullmatcher;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
class MainUnitTest {
|
||||
|
||||
@Mock
|
||||
Helper helper;
|
||||
|
||||
@InjectMocks
|
||||
Main main;
|
||||
|
||||
@BeforeEach
|
||||
void openMocks() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMethodUnderTest_thenSecondParameterNull() {
|
||||
main.methodUnderTest();
|
||||
Mockito.verify(helper)
|
||||
.concat("Baeldung", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMethodUnderTest_thenSecondParameterNullWithMatchers() {
|
||||
main.methodUnderTest();
|
||||
Mockito.verify(helper)
|
||||
.concat(any(), isNull());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.onemethodmultipleparams;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class OneMethodMultipleParamsUnitTest {
|
||||
|
||||
@Mock
|
||||
ExampleService exampleService;
|
||||
|
||||
@Test
|
||||
public void givenAMethod_whenStubbingForMultipleArguments_thenExpectDifferentResults() {
|
||||
when(exampleService.getValue(10)).thenReturn(100);
|
||||
when(exampleService.getValue(20)).thenReturn(200);
|
||||
when(exampleService.getValue(30)).thenReturn(300);
|
||||
|
||||
assertEquals(100, exampleService.getValue(10));
|
||||
assertEquals(200, exampleService.getValue(20));
|
||||
assertEquals(300, exampleService.getValue(30));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAMethod_whenUsingThenAnswer_thenExpectDifferentReults() {
|
||||
when(exampleService.getValue(anyInt())).thenAnswer(invocation -> {
|
||||
int argument = (int) invocation.getArguments()[0];
|
||||
int result;
|
||||
switch (argument) {
|
||||
case 25:
|
||||
result = 125;
|
||||
break;
|
||||
case 50:
|
||||
result = 150;
|
||||
break;
|
||||
case 75:
|
||||
result = 175;
|
||||
break;
|
||||
default:
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
});
|
||||
assertEquals(125, exampleService.getValue(25));
|
||||
assertEquals(150, exampleService.getValue(50));
|
||||
assertEquals(175, exampleService.getValue(75));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAMethod_whenUsingConsecutiveStubbing_thenExpectResultsInOrder() {
|
||||
when(exampleService.getValue(anyInt())).thenReturn(9, 18, 27);
|
||||
assertEquals(9, exampleService.getValue(1));
|
||||
assertEquals(18, exampleService.getValue(1));
|
||||
assertEquals(27, exampleService.getValue(1));
|
||||
assertEquals(27, exampleService.getValue(1));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.singleton;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ProductServiceUnitTest {
|
||||
|
||||
@Test
|
||||
void givenValueExistsInCache_whenGetProduct_thenDAOIsNotCalled() {
|
||||
ProductDAO productDAO = mock(ProductDAO.class);
|
||||
CacheManager cacheManager = mock(CacheManager.class);
|
||||
Product product = new Product("product1", "description");
|
||||
ProductService productService = new ProductService(productDAO, cacheManager);
|
||||
|
||||
when(cacheManager.getValue(any(), any())).thenReturn(product);
|
||||
|
||||
productService.getProduct("product1");
|
||||
|
||||
Mockito.verify(productDAO, times(0)).getProduct(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenValueExistsInCache_whenGetProduct_thenDAOIsNotCalled_mockingStatic() {
|
||||
ProductDAO productDAO = mock(ProductDAO.class);
|
||||
CacheManager cacheManager = mock(CacheManager.class);
|
||||
Product product = new Product("product1", "description");
|
||||
|
||||
try (MockedStatic<CacheManager> cacheManagerMock = mockStatic(CacheManager.class)) {
|
||||
cacheManagerMock.when(CacheManager::getInstance).thenReturn(cacheManager);
|
||||
when(cacheManager.getValue(any(), any())).thenReturn(product);
|
||||
ProductService productService = new ProductService(productDAO);
|
||||
productService.getProduct("product1");
|
||||
Mockito.verify(productDAO, times(0)).getProduct(any());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.wantedbutnotinvocked;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
class MainUnitTest {
|
||||
|
||||
@Mock
|
||||
Helper helper;
|
||||
|
||||
@InjectMocks
|
||||
Main main = new Main();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenValueUpperThan5_WhenMethodUnderTest_ThenDelegatesToHelperClass() {
|
||||
main.methodUnderTest(7);
|
||||
Mockito.verify(helper)
|
||||
.getBaeldungString();
|
||||
}
|
||||
|
||||
// Uncomment the next line to see the error
|
||||
// @Test
|
||||
void givenValueLowerThan5_WhenMethodUnderTest_ThenDelegatesToGetBaeldungString() {
|
||||
main.methodUnderTest(3);
|
||||
Mockito.verify(helper)
|
||||
.getBaeldungString();
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
- [SpringRunner vs MockitoJUnitRunner](https://www.baeldung.com/junit-springrunner-vs-mockitojunitrunner)
|
|
@ -0,0 +1,35 @@
|
|||
<?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>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.runners;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Component
|
||||
public class DataProvider {
|
||||
|
||||
private final List<String> memory = Stream.of("baeldung", "java", "dummy").collect(Collectors.toList());
|
||||
|
||||
public Stream<String> getValues() {
|
||||
return memory.stream();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.runners;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class StringConverter {
|
||||
|
||||
private final DataProvider dataProvider;
|
||||
|
||||
@Autowired
|
||||
public StringConverter(DataProvider dataProvider) {
|
||||
this.dataProvider = dataProvider;
|
||||
}
|
||||
|
||||
public List<String> convert() {
|
||||
return dataProvider.getValues().map(String::toUpperCase).collect(Collectors.toList());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.runners;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class StringConverterStarter {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(StringConverterStarter.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
spring.main.allow-bean-definition-overriding=true
|
||||
spring.main.web-application-type=none
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.runners;
|
||||
|
||||
import lombok.val;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class StringConverterMockitoRunnerUnitTest {
|
||||
@Mock
|
||||
private DataProvider dataProvider;
|
||||
|
||||
@InjectMocks
|
||||
private StringConverter stringConverter;
|
||||
|
||||
@Test
|
||||
public void givenStrings_whenConvert_thenReturnUpperCase() {
|
||||
Mockito.when(dataProvider.getValues()).thenReturn(Stream.of("first", "second"));
|
||||
|
||||
val result = stringConverter.convert();
|
||||
|
||||
Assertions.assertThat(result).contains("FIRST", "SECOND");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.runners;
|
||||
|
||||
import lombok.val;
|
||||
import org.assertj.core.api.Assertions;
|
||||
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.mock.mockito.MockBean;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ContextConfiguration(classes = StringConverter.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
public class StringConverterSpringRunnerUnitTest {
|
||||
@MockBean
|
||||
private DataProvider dataProvider;
|
||||
|
||||
@Autowired
|
||||
private StringConverter stringConverter;
|
||||
|
||||
@Test
|
||||
public void givenStrings_whenConvert_thenReturnUpperCase() {
|
||||
Mockito.when(dataProvider.getValues()).thenReturn(Stream.of("first", "second"));
|
||||
|
||||
val result = stringConverter.convert();
|
||||
|
||||
Assertions.assertThat(result).contains("FIRST", "SECOND");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.runners;
|
||||
|
||||
import lombok.val;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class StringConverterWithInitUnitTest {
|
||||
@Mock
|
||||
private DataProvider dataProvider;
|
||||
|
||||
@InjectMocks
|
||||
private StringConverter stringConverter;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
// or
|
||||
// dataProvider = Mockito.mock(DataProvider.class);
|
||||
// stringConverter = new StringConverter(dataProvider);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStrings_whenConvert_thenReturnUpperCase() {
|
||||
Mockito.when(dataProvider.getValues()).thenReturn(Stream.of("first", "second"));
|
||||
|
||||
val result = stringConverter.convert();
|
||||
|
||||
Assertions.assertThat(result).contains("FIRST", "SECOND");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue