From f8e9839390c7fe593b9acd864e5064836efc909c Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Tue, 23 Mar 2021 16:09:39 +0100 Subject: [PATCH] BAEL-4836 - Mocking Static Methods with Mockito (#10580) * BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson * BAEL-4756 - Mockito MockSettings * BAEL-4756 - Mockito MockSettings - fix spelling * BAEL-2674 - Upgrade the Okhttp article * BAEL-4204 - Adding Interceptors in OkHTTP * BAEL-4836 - Mocking Static Methods with Mockito Co-authored-by: Jonathan Cook --- testing-modules/mockito-3/.gitignore | 4 ++ testing-modules/mockito-3/pom.xml | 38 ++++++++++++++++++ .../mockito/mockedstatic/StaticUtils.java | 22 +++++++++++ .../mockito-3/src/main/resources/logback.xml | 13 +++++++ .../mockedstatic/MockedStaticUnitTest.java | 39 +++++++++++++++++++ testing-modules/pom.xml | 3 +- 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 testing-modules/mockito-3/.gitignore create mode 100644 testing-modules/mockito-3/pom.xml create mode 100644 testing-modules/mockito-3/src/main/java/com/baeldung/mockito/mockedstatic/StaticUtils.java create mode 100644 testing-modules/mockito-3/src/main/resources/logback.xml create mode 100644 testing-modules/mockito-3/src/test/java/com/baeldung/mockito/mockedstatic/MockedStaticUnitTest.java diff --git a/testing-modules/mockito-3/.gitignore b/testing-modules/mockito-3/.gitignore new file mode 100644 index 0000000000..38fe5148c1 --- /dev/null +++ b/testing-modules/mockito-3/.gitignore @@ -0,0 +1,4 @@ +/target/ +/.settings/ +/.classpath +/.project \ No newline at end of file diff --git a/testing-modules/mockito-3/pom.xml b/testing-modules/mockito-3/pom.xml new file mode 100644 index 0000000000..8d506561ed --- /dev/null +++ b/testing-modules/mockito-3/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + mockito-3 + 0.0.1-SNAPSHOT + mockito-3 + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + org.mockito + mockito-inline + ${mockito.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + + + 3.8.0 + 3.8.0 + + + diff --git a/testing-modules/mockito-3/src/main/java/com/baeldung/mockito/mockedstatic/StaticUtils.java b/testing-modules/mockito-3/src/main/java/com/baeldung/mockito/mockedstatic/StaticUtils.java new file mode 100644 index 0000000000..2e2f5ead33 --- /dev/null +++ b/testing-modules/mockito-3/src/main/java/com/baeldung/mockito/mockedstatic/StaticUtils.java @@ -0,0 +1,22 @@ +package com.baeldung.mockito.mockedstatic; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class StaticUtils { + + private StaticUtils() { + } + + public static List range(int start, int end) { + return IntStream.range(start, end) + .boxed() + .collect(Collectors.toList()); + } + + public static String name() { + return "Baeldung"; + } + +} diff --git a/testing-modules/mockito-3/src/main/resources/logback.xml b/testing-modules/mockito-3/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/testing-modules/mockito-3/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/testing-modules/mockito-3/src/test/java/com/baeldung/mockito/mockedstatic/MockedStaticUnitTest.java b/testing-modules/mockito-3/src/test/java/com/baeldung/mockito/mockedstatic/MockedStaticUnitTest.java new file mode 100644 index 0000000000..a212e6e3eb --- /dev/null +++ b/testing-modules/mockito-3/src/test/java/com/baeldung/mockito/mockedstatic/MockedStaticUnitTest.java @@ -0,0 +1,39 @@ +package com.baeldung.mockito.mockedstatic; + +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Arrays; + +class MockedStaticUnitTest { + + @Test + void givenStaticMethodWithNoArgs_whenMocked_thenReturnsMockSuccessfully() { + assertThat(StaticUtils.name()).isEqualTo("Baeldung"); + + try (MockedStatic utilities = Mockito.mockStatic(StaticUtils.class)) { + utilities.when(StaticUtils::name).thenReturn("Eugen"); + assertThat(StaticUtils.name()).isEqualTo("Eugen"); + } + + assertThat(StaticUtils.name()).isEqualTo("Baeldung"); + } + + @Test + void givenStaticMethodWithArgs_whenMocked_thenReturnsMockSuccessfully() { + assertThat(StaticUtils.range(2, 6)).containsExactly(2, 3, 4, 5); + + try (MockedStatic utilities = Mockito.mockStatic(StaticUtils.class)) { + utilities.when(() -> StaticUtils.range(2, 6)) + .thenReturn(Arrays.asList(10, 11, 12)); + + assertThat(StaticUtils.range(2, 6)).containsExactly(10, 11, 12); + } + + assertThat(StaticUtils.range(2, 6)).containsExactly(2, 3, 4, 5); + } + +} diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index fd4a13d026..c0c28e085d 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -23,7 +23,8 @@ junit5-migration load-testing-comparison mockito - mockito-2 + mockito-2 + mockito-3 hamcrest mocks mockserver