add all file to git
This commit is contained in:
@@ -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>
|
||||
+17
@@ -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() {
|
||||
}
|
||||
}
|
||||
+1
@@ -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
|
||||
+22
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+7
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user