From 74efd2530ef142902a10b44431d6981d789f577c Mon Sep 17 00:00:00 2001 From: fanatixan Date: Mon, 7 Sep 2020 04:13:25 +0200 Subject: [PATCH] added code snippets for bael-4554 (#9979) --- .../mockito/whenvsdomethods/Employee.java | 11 +++ .../IAmOnHolidayException.java | 5 ++ .../WhenVsDoMethodsUnitTest.java | 90 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java create mode 100644 testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java create mode 100644 testing-modules/mockito-2/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java b/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java new file mode 100644 index 0000000000..4bbd2843f2 --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/Employee.java @@ -0,0 +1,11 @@ +package com.baeldung.mockito.whenvsdomethods; + +import java.time.DayOfWeek; + +public interface Employee { + + String greet(); + + void work(DayOfWeek day); + +} diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java b/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java new file mode 100644 index 0000000000..24276ba958 --- /dev/null +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/whenvsdomethods/IAmOnHolidayException.java @@ -0,0 +1,5 @@ +package com.baeldung.mockito.whenvsdomethods; + +public class IAmOnHolidayException extends RuntimeException { + +} diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java b/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java new file mode 100644 index 0000000000..8c2b86daf1 --- /dev/null +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/whenvsdomethods/WhenVsDoMethodsUnitTest.java @@ -0,0 +1,90 @@ +package com.baeldung.mockito.whenvsdomethods; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.willThrow; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +import java.time.DayOfWeek; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class WhenVsDoMethodsUnitTest { + + @Mock + private Employee employee; + + @BeforeEach + void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + void givenNonVoidMethod_callingWhen_shouldConfigureBehavior() { + // given + when(employee.greet()).thenReturn("Hello"); + + // when + String greeting = employee.greet(); + + // then + assertThat(greeting, is("Hello")); + } + + @Test + void givenNonVoidMethod_callingDoReturn_shouldConfigureBehavior() { + // given + doReturn("Hello").when(employee).greet(); + + // when + String greeting = employee.greet(); + + // then + assertThat(greeting, is("Hello")); + } + + @Test + void givenVoidMethod_callingDoThrow_shouldConfigureBehavior() { + // given + doThrow(new IAmOnHolidayException()).when(employee).work(DayOfWeek.SUNDAY); + + // when + Executable workCall = () -> employee.work(DayOfWeek.SUNDAY); + + // then + assertThrows(IAmOnHolidayException.class, workCall); + } + + @Test + void givenNonVoidMethod_callingGiven_shouldConfigureBehavior() { + // given + given(employee.greet()).willReturn("Hello"); + + // when + String greeting = employee.greet(); + + // then + assertThat(greeting, is("Hello")); + } + + @Test + void givenVoidMethod_callingWillThrow_shouldConfigureBehavior() { + // given + willThrow(new IAmOnHolidayException()).given(employee).work(DayOfWeek.SUNDAY); + + // when + Executable workCall = () -> employee.work(DayOfWeek.SUNDAY); + + // then + assertThrows(IAmOnHolidayException.class, workCall); + } + +}