[BAEL-16668] Split or move testing-modules/mockito module (#7835)
* Split or move testing-modules/mockito module * [BAEL-16668] fix after merge with master
This commit is contained in:
parent
4e31b41919
commit
2255577ffd
12
testing-modules/hamcrest/README.md
Normal file
12
testing-modules/hamcrest/README.md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
## Hamcrest
|
||||||
|
|
||||||
|
This module contains articles about Hamcrest
|
||||||
|
|
||||||
|
### Relevant articles
|
||||||
|
- [Hamcrest Text Matchers](https://www.baeldung.com/hamcrest-text-matchers)
|
||||||
|
- [Hamcrest File Matchers](https://www.baeldung.com/hamcrest-file-matchers)
|
||||||
|
- [Hamcrest Object Matchers](https://www.baeldung.com/hamcrest-object-matchers)
|
||||||
|
- [Hamcrest Bean Matchers](https://www.baeldung.com/hamcrest-bean-matchers)
|
||||||
|
- [Hamcrest Number Matchers](https://www.baeldung.com/hamcrest-number-matchers)
|
||||||
|
- [Hamcrest Common Core Matchers](https://www.baeldung.com/hamcrest-core-matchers)
|
||||||
|
- [Hamcrest Custom Matchers](https://www.baeldung.com/hamcrest-custom-matchers)
|
29
testing-modules/hamcrest/pom.xml
Normal file
29
testing-modules/hamcrest/pom.xml
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<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>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>hamcrest</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>hamcrest</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>java-hamcrest</artifactId>
|
||||||
|
<version>${hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<hamcrest.version>2.0.0.0</hamcrest.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.hamcrest;
|
package com.baeldung.hamcrest.objectmatchers;
|
||||||
|
|
||||||
public class City extends Location {
|
public class City extends Location {
|
||||||
String name;
|
String name;
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.baeldung.hamcrest.objectmatchers;
|
||||||
|
|
||||||
|
public class Location {
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package org.baeldung.hamcrest;
|
package com.baeldung.hamcrest;
|
||||||
|
|
||||||
|
import com.baeldung.hamcrest.objectmatchers.City;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.beans.PropertyDescriptor;
|
import java.beans.PropertyDescriptor;
|
||||||
@ -8,13 +9,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import static java.util.stream.Collectors.toList;
|
import static java.util.stream.Collectors.toList;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.hasProperty;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.hamcrest.Matchers.not;
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
|
||||||
import static org.hamcrest.Matchers.equalToIgnoringCase;
|
|
||||||
import static org.hamcrest.Matchers.samePropertyValuesAs;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
|
||||||
import static org.hamcrest.beans.PropertyUtil.getPropertyDescriptor;
|
import static org.hamcrest.beans.PropertyUtil.getPropertyDescriptor;
|
||||||
import static org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor;
|
import static org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.hamcrest;
|
package com.baeldung.hamcrest;
|
||||||
|
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
@ -145,7 +145,6 @@ public class HamcrestCoreMatchersUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenString_WhenContainsStringIgnoringCase_ThenCorrect() {
|
public void givenString_WhenContainsStringIgnoringCase_ThenCorrect() {
|
||||||
|
|
||||||
|
|
||||||
// GIVEN
|
// GIVEN
|
||||||
String testString = "hamcrest core";
|
String testString = "hamcrest core";
|
||||||
|
|
||||||
@ -164,7 +163,6 @@ public class HamcrestCoreMatchersUnitTest {
|
|||||||
assertThat(list, hasItem(isA(String.class)));
|
assertThat(list, hasItem(isA(String.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTestInput_WhenUsingHasItemsInCollection() {
|
public void givenTestInput_WhenUsingHasItemsInCollection() {
|
||||||
|
|
||||||
@ -223,7 +221,6 @@ public class HamcrestCoreMatchersUnitTest {
|
|||||||
assertThat(testString, either(startsWith("Bael")).or(containsString("Core")));
|
assertThat(testString, either(startsWith("Bael")).or(containsString("Core")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTestInput_WhenUsingEveryItemForMatchInCollection() {
|
public void givenTestInput_WhenUsingEveryItemForMatchInCollection() {
|
||||||
|
|
@ -1,22 +1,13 @@
|
|||||||
package org.baeldung.hamcrest;
|
package com.baeldung.hamcrest;
|
||||||
|
|
||||||
import static org.hamcrest.core.StringContains.containsString;
|
import org.junit.Test;
|
||||||
import static org.hamcrest.io.FileMatchers.aFileNamed;
|
|
||||||
import static org.hamcrest.io.FileMatchers.aFileWithAbsolutePath;
|
|
||||||
import static org.hamcrest.io.FileMatchers.aFileWithCanonicalPath;
|
|
||||||
import static org.hamcrest.io.FileMatchers.aFileWithSize;
|
|
||||||
import static org.hamcrest.io.FileMatchers.aReadableFile;
|
|
||||||
import static org.hamcrest.io.FileMatchers.aWritableFile;
|
|
||||||
import static org.hamcrest.io.FileMatchers.anExistingDirectory;
|
|
||||||
import static org.hamcrest.io.FileMatchers.anExistingFile;
|
|
||||||
import static org.hamcrest.io.FileMatchers.anExistingFileOrDirectory;
|
|
||||||
import static org.hamcrest.number.OrderingComparison.greaterThan;
|
|
||||||
import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import org.junit.Test;
|
import static org.hamcrest.io.FileMatchers.*;
|
||||||
|
import static org.hamcrest.number.OrderingComparison.greaterThan;
|
||||||
|
import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
public class HamcrestFileUnitTest {
|
public class HamcrestFileUnitTest {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.hamcrest;
|
package com.baeldung.hamcrest;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -6,15 +6,7 @@ import java.math.BigDecimal;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.hamcrest.Matchers.closeTo;
|
|
||||||
import static org.hamcrest.Matchers.not;
|
|
||||||
import static org.hamcrest.Matchers.greaterThan;
|
|
||||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
|
||||||
import static org.hamcrest.Matchers.lessThan;
|
|
||||||
import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
|
||||||
import static org.hamcrest.Matchers.comparesEqualTo;
|
|
||||||
import static org.hamcrest.Matchers.notANumber;
|
|
||||||
|
|
||||||
public class HamcrestNumberUnitTest {
|
public class HamcrestNumberUnitTest {
|
||||||
|
|
@ -1,14 +1,11 @@
|
|||||||
package org.baeldung.hamcrest;
|
package com.baeldung.hamcrest;
|
||||||
|
|
||||||
|
import com.baeldung.hamcrest.objectmatchers.City;
|
||||||
|
import com.baeldung.hamcrest.objectmatchers.Location;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.hasToString;
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.hamcrest.Matchers.equalToIgnoringCase;
|
|
||||||
import static org.hamcrest.Matchers.emptyOrNullString;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
import static org.hamcrest.Matchers.typeCompatibleWith;
|
|
||||||
import static org.hamcrest.Matchers.not;
|
|
||||||
|
|
||||||
public class HamcrestObjectUnitTest {
|
public class HamcrestObjectUnitTest {
|
||||||
|
|
@ -1,4 +1,6 @@
|
|||||||
package org.baeldung.hamcrest;
|
package com.baeldung.hamcrest;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hamcrest.core.StringContains.containsString;
|
import static org.hamcrest.core.StringContains.containsString;
|
||||||
import static org.hamcrest.core.StringContains.containsStringIgnoringCase;
|
import static org.hamcrest.core.StringContains.containsStringIgnoringCase;
|
||||||
@ -16,8 +18,6 @@ import static org.hamcrest.text.MatchesPattern.matchesPattern;
|
|||||||
import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder;
|
import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class HamcrestTextUnitTest {
|
public class HamcrestTextUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
@ -1,10 +1,10 @@
|
|||||||
package org.baeldung.hamcrest;
|
package com.baeldung.hamcrest.custommatchers;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.baeldung.hamcrest.custommatchers.IsDivisibleBy.divisibleBy;
|
import static com.baeldung.hamcrest.custommatchers.IsDivisibleBy.divisibleBy;
|
||||||
import static org.baeldung.hamcrest.custommatchers.IsOnlyDigits.onlyDigits;
|
import static com.baeldung.hamcrest.custommatchers.IsOnlyDigits.onlyDigits;
|
||||||
import static org.baeldung.hamcrest.custommatchers.IsUppercase.uppercase;
|
import static com.baeldung.hamcrest.custommatchers.IsUppercase.uppercase;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.hamcrest.custommatchers;
|
package com.baeldung.hamcrest.custommatchers;
|
||||||
|
|
||||||
import org.hamcrest.Description;
|
import org.hamcrest.Description;
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.hamcrest.custommatchers;
|
package com.baeldung.hamcrest.custommatchers;
|
||||||
|
|
||||||
import org.hamcrest.Description;
|
import org.hamcrest.Description;
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.hamcrest.custommatchers;
|
package com.baeldung.hamcrest.custommatchers;
|
||||||
|
|
||||||
import org.hamcrest.Description;
|
import org.hamcrest.Description;
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
@ -1,5 +1,7 @@
|
|||||||
### Relevant articles
|
### Relevant articles
|
||||||
|
|
||||||
- [Mockito’s Java 8 Features](http://www.baeldung.com/mockito-2-java-8)
|
- [Mockito’s Java 8 Features](https://www.baeldung.com/mockito-2-java-8)
|
||||||
- [Lazy Verification with Mockito 2](http://www.baeldung.com/mockito-2-lazy-verification)
|
- [Lazy Verification with Mockito 2](https://www.baeldung.com/mockito-2-lazy-verification)
|
||||||
- [Mockito Strict Stubbing and The UnnecessaryStubbingException](https://www.baeldung.com/mockito-unnecessary-stubbing-exception)
|
- [Mockito Strict Stubbing and The UnnecessaryStubbingException](https://www.baeldung.com/mockito-unnecessary-stubbing-exception)
|
||||||
|
- [Quick Guide to BDDMockito](https://www.baeldung.com/bdd-mockito)
|
||||||
|
- [Mockito – Using Spies](https://www.baeldung.com/mockito-spy)
|
||||||
|
@ -1,7 +1,4 @@
|
|||||||
package org.baeldung.bddmockito;
|
package com.baeldung.mockito.bddmockito;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
import static org.mockito.BDDMockito.*;
|
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@ -9,6 +6,9 @@ import org.junit.Test;
|
|||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
import static org.mockito.BDDMockito.*;
|
||||||
|
|
||||||
|
|
||||||
public class BDDMockitoUnitTest {
|
public class BDDMockitoUnitTest {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.bddmockito;
|
package com.baeldung.mockito.bddmockito;
|
||||||
|
|
||||||
public interface PhoneBookRepository {
|
public interface PhoneBookRepository {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.bddmockito;
|
package com.baeldung.mockito.bddmockito;
|
||||||
|
|
||||||
public class PhoneBookService {
|
public class PhoneBookService {
|
||||||
|
|
@ -1,16 +1,16 @@
|
|||||||
package com.baeldung.mockito.java8;
|
package com.baeldung.mockito.lazyverification;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.exceptions.base.MockitoAssertionError;
|
import org.mockito.exceptions.base.MockitoAssertionError;
|
||||||
import org.mockito.junit.MockitoJUnit;
|
import org.mockito.junit.MockitoJUnit;
|
||||||
import org.mockito.junit.VerificationCollector;
|
import org.mockito.junit.VerificationCollector;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
public class LazyVerificationUnitTest {
|
public class LazyVerificationUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
@ -1,11 +1,4 @@
|
|||||||
package org.baeldung.mockito.misusing;
|
package com.baeldung.mockito.spy;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -13,6 +6,13 @@ import org.mockito.Mockito;
|
|||||||
import org.mockito.exceptions.misusing.NotAMockException;
|
import org.mockito.exceptions.misusing.NotAMockException;
|
||||||
import org.mockito.internal.progress.ThreadSafeMockingProgress;
|
import org.mockito.internal.progress.ThreadSafeMockingProgress;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
public class MockitoMisusingUnitTest {
|
public class MockitoMisusingUnitTest {
|
||||||
|
|
||||||
@After
|
@After
|
@ -1,9 +1,4 @@
|
|||||||
package org.baeldung.mockito.spy;
|
package com.baeldung.mockito.spy;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -11,6 +6,11 @@ import org.mockito.Mockito;
|
|||||||
import org.mockito.Spy;
|
import org.mockito.Spy;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class MockitoSpyUnitTest {
|
public class MockitoSpyUnitTest {
|
||||||
|
|
@ -4,23 +4,13 @@
|
|||||||
|
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Mockito Verify Cookbook](http://www.baeldung.com/mockito-verify)
|
- [Mockito Verify Cookbook](https://www.baeldung.com/mockito-verify)
|
||||||
- [Mockito When/Then Cookbook](http://www.baeldung.com/mockito-behavior)
|
- [Mockito When/Then Cookbook](https://www.baeldung.com/mockito-behavior)
|
||||||
- [Mockito – Using Spies](http://www.baeldung.com/mockito-spy)
|
- [Getting Started with Mockito @Mock, @Spy, @Captor and @InjectMocks](https://www.baeldung.com/mockito-annotations)
|
||||||
- [Getting Started with Mockito @Mock, @Spy, @Captor and @InjectMocks](http://www.baeldung.com/mockito-annotations)
|
- [Mockito’s Mock Methods](https://www.baeldung.com/mockito-mock-methods)
|
||||||
- [Mockito’s Mock Methods](http://www.baeldung.com/mockito-mock-methods)
|
- [Introduction to PowerMock](https://www.baeldung.com/intro-to-powermock)
|
||||||
- [Introduction to PowerMock](http://www.baeldung.com/intro-to-powermock)
|
- [Mocking of Private Methods Using PowerMock](https://www.baeldung.com/powermock-private-method)
|
||||||
- [Mocking Exception Throwing using Mockito](http://www.baeldung.com/mockito-exceptions)
|
- [Mocking Exception Throwing using Mockito](https://www.baeldung.com/mockito-exceptions)
|
||||||
- [Mocking Void Methods with Mockito](http://www.baeldung.com/mockito-void-methods)
|
- [Mocking Void Methods with Mockito](https://www.baeldung.com/mockito-void-methods)
|
||||||
- [Mocking of Private Methods Using PowerMock](http://www.baeldung.com/powermock-private-method)
|
- [Mock Final Classes and Methods with Mockito](https://www.baeldung.com/mockito-final)
|
||||||
- [Mock Final Classes and Methods with Mockito](http://www.baeldung.com/mockito-final)
|
- [Testing Callbacks with Mockito](https://www.baeldung.com/mockito-callbacks)
|
||||||
- [Hamcrest Custom Matchers](http://www.baeldung.com/hamcrest-custom-matchers)
|
|
||||||
- [Hamcrest Common Core Matchers](http://www.baeldung.com/hamcrest-core-matchers)
|
|
||||||
- [Testing Callbacks with Mockito](http://www.baeldung.com/mockito-callbacks)
|
|
||||||
- [Using Hamcrest Number Matchers](https://www.baeldung.com/hamcrest-number-matchers)
|
|
||||||
- [Quick Guide to BDDMockito](http://www.baeldung.com/bdd-mockito)
|
|
||||||
- [Hamcrest Bean Matchers](http://www.baeldung.com/hamcrest-bean-matchers)
|
|
||||||
- [Hamcrest Object Matchers](http://www.baeldung.com/hamcrest-object-matchers)
|
|
||||||
- [Hamcrest File Matchers](https://www.baeldung.com/hamcrest-file-matchers)
|
|
||||||
- [Hamcrest Text Matchers](http://www.baeldung.com/hamcrest-text-matchers)
|
|
||||||
- [Testing Callbacks with Mockito](http://www.baeldung.com/mockito-callbacks)
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>mockito</artifactId>
|
<artifactId>mockito</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<name>mockito</name>
|
<name>mockito</name>
|
||||||
@ -58,12 +58,6 @@
|
|||||||
<version>${powermock.version}</version>
|
<version>${powermock.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.hamcrest</groupId>
|
|
||||||
<artifactId>java-hamcrest</artifactId>
|
|
||||||
<version>${hamcrest.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito.service;
|
package com.baeldung.mockito.callbacks;
|
||||||
|
|
||||||
public class ActionHandler {
|
public class ActionHandler {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito.service;
|
package com.baeldung.mockito.callbacks;
|
||||||
|
|
||||||
public interface Callback<T> {
|
public interface Callback<T> {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito.service;
|
package com.baeldung.mockito.callbacks;
|
||||||
|
|
||||||
public class Data {
|
public class Data {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito.service;
|
package com.baeldung.mockito.callbacks;
|
||||||
|
|
||||||
public class Response {
|
public class Response {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito.service;
|
package com.baeldung.mockito.callbacks;
|
||||||
|
|
||||||
public interface Service {
|
public interface Service {
|
||||||
|
|
@ -1,4 +0,0 @@
|
|||||||
package org.baeldung.hamcrest;
|
|
||||||
|
|
||||||
public class Location {
|
|
||||||
}
|
|
@ -1,6 +1,4 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import org.baeldung.mockito.voidmethods.MyList;
|
|
||||||
|
|
||||||
public final class FinalList extends MyList {
|
public final class FinalList extends MyList {
|
||||||
|
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
public class MockFinals {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMockFinalClassMockWorks() {
|
||||||
|
|
||||||
|
FinalList finalList = new FinalList();
|
||||||
|
|
||||||
|
FinalList mock = mock(FinalList.class);
|
||||||
|
when(mock.size()).thenReturn(2);
|
||||||
|
|
||||||
|
assertNotEquals(mock.size(), finalList.size());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenMockFinalMethodMockWorks() {
|
||||||
|
|
||||||
|
MyList myList = new MyList();
|
||||||
|
|
||||||
|
MyList mock = mock(MyList.class);
|
||||||
|
when(mock.finalMethod()).thenReturn(1);
|
||||||
|
|
||||||
|
assertNotEquals(mock.finalMethod(), myList.finalMethod());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -6,7 +6,7 @@ import static org.junit.Assert.assertNotEquals;
|
|||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import org.baeldung.mockito.voidmethods.MyList;
|
import com.baeldung.mockito.MyList;
|
||||||
|
|
||||||
public class MockFinalsUnitTest {
|
public class MockFinalsUnitTest {
|
||||||
|
|
@ -0,0 +1,114 @@
|
|||||||
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
//@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class MockitoAnnotationIntegrationTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private List<String> mockedList;
|
||||||
|
|
||||||
|
@Spy
|
||||||
|
private List<String> spiedList = new ArrayList<>();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// tests
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotUseMockAnnotation_thenCorrect() {
|
||||||
|
final List<String> mockList = Mockito.mock(List.class);
|
||||||
|
mockList.add("one");
|
||||||
|
Mockito.verify(mockList).add("one");
|
||||||
|
assertEquals(0, mockList.size());
|
||||||
|
|
||||||
|
Mockito.when(mockList.size()).thenReturn(100);
|
||||||
|
assertEquals(100, mockList.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUseMockAnnotation_thenMockIsInjected() {
|
||||||
|
mockedList.add("one");
|
||||||
|
Mockito.verify(mockedList).add("one");
|
||||||
|
assertEquals(0, mockedList.size());
|
||||||
|
|
||||||
|
Mockito.when(mockedList.size()).thenReturn(100);
|
||||||
|
assertEquals(100, mockedList.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotUseSpyAnnotation_thenCorrect() {
|
||||||
|
final List<String> spyList = Mockito.spy(new ArrayList<String>());
|
||||||
|
spyList.add("one");
|
||||||
|
spyList.add("two");
|
||||||
|
|
||||||
|
Mockito.verify(spyList).add("one");
|
||||||
|
Mockito.verify(spyList).add("two");
|
||||||
|
|
||||||
|
assertEquals(2, spyList.size());
|
||||||
|
|
||||||
|
Mockito.doReturn(100).when(spyList).size();
|
||||||
|
assertEquals(100, spyList.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUseSpyAnnotation_thenSpyIsInjectedCorrectly() {
|
||||||
|
spiedList.add("one");
|
||||||
|
spiedList.add("two");
|
||||||
|
|
||||||
|
Mockito.verify(spiedList).add("one");
|
||||||
|
Mockito.verify(spiedList).add("two");
|
||||||
|
|
||||||
|
assertEquals(2, spiedList.size());
|
||||||
|
|
||||||
|
Mockito.doReturn(100).when(spiedList).size();
|
||||||
|
assertEquals(100, spiedList.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenNotUseCaptorAnnotation_thenCorrect() {
|
||||||
|
final List<String> mockList = Mockito.mock(List.class);
|
||||||
|
final ArgumentCaptor<String> arg = ArgumentCaptor.forClass(String.class);
|
||||||
|
mockList.add("one");
|
||||||
|
Mockito.verify(mockList).add(arg.capture());
|
||||||
|
|
||||||
|
assertEquals("one", arg.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Captor
|
||||||
|
private
|
||||||
|
ArgumentCaptor<String> argCaptor;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUseCaptorAnnotation_thenTheSam() {
|
||||||
|
mockedList.add("one");
|
||||||
|
Mockito.verify(mockedList).add(argCaptor.capture());
|
||||||
|
|
||||||
|
assertEquals("one", argCaptor.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Map<String, String> wordMap;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private MyDictionary dic = new MyDictionary();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUseInjectMocksAnnotation_thenCorrect() {
|
||||||
|
Mockito.when(wordMap.get("aWord")).thenReturn("aMeaning");
|
||||||
|
|
||||||
|
assertEquals("aMeaning", dic.getMeaning("aWord"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
@ -0,0 +1,95 @@
|
|||||||
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Matchers.anyString;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
public class MockitoConfigExamplesIntegrationTest {
|
||||||
|
|
||||||
|
// tests
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() {
|
||||||
|
final MyList listMock = Mockito.mock(MyList.class);
|
||||||
|
when(listMock.add(anyString())).thenReturn(false);
|
||||||
|
|
||||||
|
final boolean added = listMock.add(randomAlphabetic(6));
|
||||||
|
assertThat(added, is(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenMockReturnBehaviorIsConfigured2_thenBehaviorIsVerified() {
|
||||||
|
final MyList listMock = Mockito.mock(MyList.class);
|
||||||
|
doReturn(false).when(listMock).add(anyString());
|
||||||
|
|
||||||
|
final boolean added = listMock.add(randomAlphabetic(6));
|
||||||
|
assertThat(added, is(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public final void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() {
|
||||||
|
final MyList listMock = Mockito.mock(MyList.class);
|
||||||
|
when(listMock.add(anyString())).thenThrow(IllegalStateException.class);
|
||||||
|
|
||||||
|
listMock.add(randomAlphabetic(6));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() {
|
||||||
|
final MyList listMock = Mockito.mock(MyList.class);
|
||||||
|
doThrow(NullPointerException.class).when(listMock).clear();
|
||||||
|
|
||||||
|
listMock.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingOnlyOnce_thenNoExceptionIsThrown() {
|
||||||
|
final MyList listMock = Mockito.mock(MyList.class);
|
||||||
|
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
|
||||||
|
|
||||||
|
listMock.add(randomAlphabetic(6));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() {
|
||||||
|
final MyList listMock = Mockito.mock(MyList.class);
|
||||||
|
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
|
||||||
|
|
||||||
|
listMock.add(randomAlphabetic(6));
|
||||||
|
listMock.add(randomAlphabetic(6));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMethodIsCalled() {
|
||||||
|
final MyList listMock = Mockito.mock(MyList.class);
|
||||||
|
when(listMock.size()).thenCallRealMethod();
|
||||||
|
|
||||||
|
assertThat(listMock.size(), equalTo(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenRealMethodIsCalled() {
|
||||||
|
final MyList listMock = Mockito.mock(MyList.class);
|
||||||
|
doAnswer(invocation -> "Always the same").when(listMock).get(anyInt());
|
||||||
|
|
||||||
|
final String element = listMock.get(1);
|
||||||
|
assertThat(element, is(equalTo("Always the same")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() {
|
||||||
|
final MyList instance = new MyList();
|
||||||
|
final MyList spy = Mockito.spy(instance);
|
||||||
|
|
||||||
|
doThrow(NullPointerException.class).when(spy).size();
|
||||||
|
spy.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
@ -11,7 +11,7 @@ import static org.mockito.Matchers.anyInt;
|
|||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import org.baeldung.mockito.voidmethods.MyList;
|
import com.baeldung.mockito.MyList;
|
||||||
|
|
||||||
public class MockitoConfigExamplesUnitTest {
|
public class MockitoConfigExamplesUnitTest {
|
||||||
|
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
public class MockitoExceptionIntegrationTest {
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
|
||||||
|
MyDictionary dictMock = mock(MyDictionary.class);
|
||||||
|
when(dictMock.getMeaning(anyString())).thenThrow(NullPointerException.class);
|
||||||
|
|
||||||
|
dictMock.getMeaning("word");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public void whenConfigVoidRetunMethodToThrowEx_thenExIsThrown() {
|
||||||
|
MyDictionary dictMock = mock(MyDictionary.class);
|
||||||
|
doThrow(IllegalStateException.class).when(dictMock)
|
||||||
|
.add(anyString(), anyString());
|
||||||
|
|
||||||
|
dictMock.add("word", "meaning");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void whenConfigNonVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
|
||||||
|
MyDictionary dictMock = mock(MyDictionary.class);
|
||||||
|
when(dictMock.getMeaning(anyString())).thenThrow(new NullPointerException("Error occurred"));
|
||||||
|
|
||||||
|
dictMock.getMeaning("word");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException.class)
|
||||||
|
public void whenConfigVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
|
||||||
|
MyDictionary dictMock = mock(MyDictionary.class);
|
||||||
|
doThrow(new IllegalStateException("Error occurred")).when(dictMock)
|
||||||
|
.add(anyString(), anyString());
|
||||||
|
|
||||||
|
dictMock.add("word", "meaning");
|
||||||
|
}
|
||||||
|
|
||||||
|
// =====
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void givenSpy_whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
|
||||||
|
MyDictionary dict = new MyDictionary();
|
||||||
|
MyDictionary spy = Mockito.spy(dict);
|
||||||
|
|
||||||
|
when(spy.getMeaning(anyString())).thenThrow(NullPointerException.class);
|
||||||
|
spy.getMeaning("word");
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.Mockito.doThrow;
|
import static org.mockito.Mockito.doThrow;
|
@ -1,13 +1,12 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
import org.mockito.*;
|
import org.mockito.*;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
import org.mockito.MockSettings;
|
||||||
|
import org.mockito.exceptions.verification.TooLittleActualInvocations;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
|
import static org.hamcrest.Matchers.containsString;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
public class MockitoMockIntegrationTest {
|
||||||
|
|
||||||
|
private static class CustomAnswer implements Answer<Boolean> {
|
||||||
|
@Override
|
||||||
|
public Boolean answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingSimpleMock_thenCorrect() {
|
||||||
|
MyList listMock = mock(MyList.class);
|
||||||
|
when(listMock.add(anyString())).thenReturn(false);
|
||||||
|
boolean added = listMock.add(randomAlphabetic(6));
|
||||||
|
|
||||||
|
verify(listMock).add(anyString());
|
||||||
|
assertThat(added, is(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingMockWithName_thenCorrect() {
|
||||||
|
MyList listMock = mock(MyList.class, "myMock");
|
||||||
|
when(listMock.add(anyString())).thenReturn(false);
|
||||||
|
listMock.add(randomAlphabetic(6));
|
||||||
|
|
||||||
|
thrown.expect(TooLittleActualInvocations.class);
|
||||||
|
thrown.expectMessage(containsString("myMock.add"));
|
||||||
|
|
||||||
|
verify(listMock, times(2)).add(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingMockWithAnswer_thenCorrect() {
|
||||||
|
MyList listMock = mock(MyList.class, new CustomAnswer());
|
||||||
|
boolean added = listMock.add(randomAlphabetic(6));
|
||||||
|
|
||||||
|
verify(listMock).add(anyString());
|
||||||
|
assertThat(added, is(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingMockWithSettings_thenCorrect() {
|
||||||
|
MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer());
|
||||||
|
MyList listMock = mock(MyList.class, customSettings);
|
||||||
|
boolean added = listMock.add(randomAlphabetic(6));
|
||||||
|
|
||||||
|
verify(listMock).add(anyString());
|
||||||
|
assertThat(added, is(false));
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import org.baeldung.mockito.voidmethods.MyList;
|
import com.baeldung.mockito.MyList;
|
||||||
|
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
@ -0,0 +1,117 @@
|
|||||||
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.InOrder;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.exceptions.verification.NoInteractionsWanted;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.hasItem;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.Matchers.anyString;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
public class MockitoVerifyExamplesIntegrationTest {
|
||||||
|
|
||||||
|
// tests
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.size();
|
||||||
|
verify(mockedList).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.size();
|
||||||
|
verify(mockedList, times(1)).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
verifyZeroInteractions(mockedList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
verify(mockedList, times(0)).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NoInteractionsWanted.class)
|
||||||
|
public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.size();
|
||||||
|
mockedList.clear();
|
||||||
|
|
||||||
|
verify(mockedList).size();
|
||||||
|
verifyNoMoreInteractions(mockedList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenVerifyingOrderOfInteractions_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.size();
|
||||||
|
mockedList.add("a parameter");
|
||||||
|
mockedList.clear();
|
||||||
|
|
||||||
|
final InOrder inOrder = Mockito.inOrder(mockedList);
|
||||||
|
inOrder.verify(mockedList).size();
|
||||||
|
inOrder.verify(mockedList).add("a parameter");
|
||||||
|
inOrder.verify(mockedList).clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.size();
|
||||||
|
|
||||||
|
verify(mockedList, never()).clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.clear();
|
||||||
|
mockedList.clear();
|
||||||
|
mockedList.clear();
|
||||||
|
|
||||||
|
verify(mockedList, atLeast(1)).clear();
|
||||||
|
verify(mockedList, atMost(10)).clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// with arguments
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenVerifyingAnInteractionWithExactArgument_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.add("test");
|
||||||
|
|
||||||
|
verify(mockedList).add("test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenVerifyingAnInteractionWithAnyArgument_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.add("test");
|
||||||
|
|
||||||
|
verify(mockedList).add(anyString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenVerifyingAnInteractionWithArgumentCapture_thenCorrect() {
|
||||||
|
final List<String> mockedList = mock(MyList.class);
|
||||||
|
mockedList.addAll(Lists.<String>newArrayList("someElement"));
|
||||||
|
final ArgumentCaptor<List> argumentCaptor = ArgumentCaptor.forClass(List.class);
|
||||||
|
verify(mockedList).addAll(argumentCaptor.capture());
|
||||||
|
final List<String> capturedArgument = argumentCaptor.<List<String>>getValue();
|
||||||
|
assertThat(capturedArgument, hasItem("someElement"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
import org.baeldung.mockito.voidmethods.MyList;
|
import com.baeldung.mockito.MyList;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.InOrder;
|
import org.mockito.InOrder;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
@ -1,4 +1,4 @@
|
|||||||
package org.baeldung.mockito.voidmethods;
|
package com.baeldung.mockito;
|
||||||
|
|
||||||
import java.util.AbstractList;
|
import java.util.AbstractList;
|
||||||
|
|
@ -1,11 +1,4 @@
|
|||||||
package org.baeldung.mockito.service;
|
package com.baeldung.mockito.callbacks;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.Mockito.doAnswer;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -15,6 +8,13 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.mockito.stubbing.Answer;
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
public class ActionHandlerUnitTest {
|
public class ActionHandlerUnitTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
@ -1,23 +1,16 @@
|
|||||||
package org.baeldung.mockito.voidmethods;
|
package com.baeldung.mockito.voidmethods;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
|
||||||
import static org.mockito.ArgumentMatchers.isA;
|
|
||||||
import static org.mockito.ArgumentMatchers.isNull;
|
|
||||||
import static org.mockito.Mockito.doAnswer;
|
|
||||||
import static org.mockito.Mockito.doCallRealMethod;
|
|
||||||
import static org.mockito.Mockito.doNothing;
|
|
||||||
import static org.mockito.Mockito.doThrow;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.times;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
|
|
||||||
|
import com.baeldung.mockito.MyList;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
import org.mockito.stubbing.Answer;
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class MockitoVoidMethodsUnitTest {
|
public class MockitoVoidMethodsUnitTest {
|
||||||
|
|
@ -23,6 +23,7 @@
|
|||||||
<module>load-testing-comparison</module>
|
<module>load-testing-comparison</module>
|
||||||
<module>mockito</module>
|
<module>mockito</module>
|
||||||
<module>mockito-2</module>
|
<module>mockito-2</module>
|
||||||
|
<module>hamcrest</module>
|
||||||
<module>mocks</module>
|
<module>mocks</module>
|
||||||
<module>mockserver</module>
|
<module>mockserver</module>
|
||||||
<module>parallel-tests-junit</module>
|
<module>parallel-tests-junit</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user