feat: Implement example tests for all scenarios

Relates to: BAEL-632
This commit is contained in:
Felipe Reis 2017-03-01 19:29:34 -03:00
parent 6b6b7c79b4
commit 4b70b1d5ce
6 changed files with 309 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package com.baeldung.mockito.java8;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class ArgumentMatcherWithLambdaUnitTest {
@InjectMocks
private UnemploymentServiceImpl unemploymentService;
@Mock
private JobService jobService;
@Test
public void whenPersonWithJob_thenIsNotEntitled() {
Person peter = new Person("Peter");
Person linda = new Person("Linda");
JobPosition teacher = new JobPosition("Teacher");
when(jobService.findCurrentJobPosition(
ArgumentMatchers.argThat((p) -> p.getName().equals("Peter")))
).thenReturn(Optional.of(teacher));
assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(linda));
assertFalse(unemploymentService.personIsEntitledToUnemploymentSupport(peter));
}
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.mockito.java8;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class ArgumentMatcherWithoutLambdaUnitTest {
@InjectMocks
private UnemploymentServiceImpl unemploymentService;
@Mock
private JobService jobService;
@Test
public void whenPersonWithJob_thenIsNotEntitled() {
Person peter = new Person("Peter");
Person linda = new Person("Linda");
JobPosition teacher = new JobPosition("Teacher");
when(jobService.findCurrentJobPosition(
ArgumentMatchers.argThat(new PeterArgumentMatcher()))
).thenReturn(Optional.of(teacher));
assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(linda));
assertFalse(unemploymentService.personIsEntitledToUnemploymentSupport(peter));
}
private class PeterArgumentMatcher implements ArgumentMatcher<Person> {
@Override
public boolean matches(Person p) {
if (p.getName().equals("Peter")) {
return true;
}
return false;
}
}
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.mockito.java8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class CustomAnswerWithLambdaUnitTest {
@InjectMocks
private UnemploymentServiceImpl unemploymentService;
@Mock
private JobService jobService;
@Test
public void whenPersonWithJobHistory_thenSearchReturnsValue() {
Person peter = new Person("Peter");
assertEquals("Teacher", unemploymentService.searchJob(peter, "").get().getTitle());
}
@Test
public void whenPersonWithNoJobHistory_thenSearchReturnsEmpty() {
Person linda = new Person("Linda");
assertFalse(unemploymentService.searchJob(linda, "").isPresent());
}
@Before
public void init() {
MockitoAnnotations.initMocks(this);
when(jobService.listJobs(any(Person.class))).then((i) -> {
return ((Person) i.getArgument(0)).getName().equals("Peter") ? Stream.<JobPosition> builder().add(new JobPosition("Teacher")).build() : Stream.empty();
});
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.mockito.java8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class CustomAnswerWithoutLambdaUnitTest {
@InjectMocks
private UnemploymentServiceImpl unemploymentService;
@Mock
private JobService jobService;
@Test
public void whenPersonWithJobHistory_thenSearchReturnsValue() {
Person peter = new Person("Peter");
assertEquals("Teacher", unemploymentService.searchJob(peter, "").get().getTitle());
}
@Test
public void whenPersonWithNoJobHistory_thenSearchReturnsEmpty() {
Person linda = new Person("Linda");
assertFalse(unemploymentService.searchJob(linda, "").isPresent());
}
private class PersonAnswer implements Answer<Stream<JobPosition>> {
@Override
public Stream<JobPosition> answer(InvocationOnMock invocation) throws Throwable {
Person person = invocation.getArgument(0);
if(person.getName().equals("Peter")) {
return Stream.<JobPosition>builder().add(new JobPosition("Teacher")).build();
}
return Stream.empty();
}
}
@Before
public void init() {
MockitoAnnotations.initMocks(this);
when(jobService.listJobs(any(Person.class))).then(new PersonAnswer());
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.mockito.java8;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.when;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
public class JobServiceUnitTest {
@Mock
private JobService jobService;
@Test
public void givenDefaultMethod_whenCallRealMethod_thenNoExceptionIsRaised() {
Person person = new Person();
when(jobService.findCurrentJobPosition(person)).thenReturn(Optional.of(new JobPosition()));
doCallRealMethod().when(jobService).assignJobPosition(Mockito.any(Person.class), Mockito.any(JobPosition.class));
assertFalse(jobService.assignJobPosition(person, new JobPosition()));
}
@Test
public void givenReturnIsOfTypeOptional_whenDefaultValueIsReturned_thenValueIsEmpty() {
Person person = new Person();
when(jobService.findCurrentJobPosition(person)).thenReturn(Optional.empty());
doCallRealMethod().when(jobService).assignJobPosition(Mockito.any(Person.class), Mockito.any(JobPosition.class));
assertTrue(jobService.assignJobPosition(person, new JobPosition()));
}
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.mockito.java8;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
public class UnemploymentServiceImplUnitTest {
@Mock
private JobService jobService;
@InjectMocks
private UnemploymentServiceImpl unemploymentService;
@Test
public void givenReturnIsOfTypeOptional_whenMocked_thenValueIsEmpty() {
Person person = new Person();
when(jobService.findCurrentJobPosition(any(Person.class))).thenReturn(Optional.empty());
assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(person));
}
@Test
public void givenReturnIsOfTypeOptional_whenDefaultValueIsReturned_thenValueIsEmpty() {
Person person = new Person();
// This will fail when Mockito 1 is used
assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(person));
}
@Test
public void givenReturnIsOfTypeStream_whenMocked_thenValueIsEmpty() {
Person person = new Person();
when(jobService.listJobs(any(Person.class))).thenReturn(Stream.empty());
assertFalse(unemploymentService.searchJob(person, "").isPresent());
}
@Test
public void givenReturnIsOfTypeStream_whenDefaultValueIsReturned_thenValueIsEmpty() {
Person person = new Person();
// This will fail when Mockito 1 is used
assertFalse(unemploymentService.searchJob(person, "").isPresent());
}
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
}