added code snippets for bael-4554 (#9979)
This commit is contained in:
parent
1927bb1b46
commit
74efd2530e
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.mockito.whenvsdomethods;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
|
||||
public interface Employee {
|
||||
|
||||
String greet();
|
||||
|
||||
void work(DayOfWeek day);
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.mockito.whenvsdomethods;
|
||||
|
||||
public class IAmOnHolidayException extends RuntimeException {
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue