Skip to content

Instantly share code, notes, and snippets.

@bmchild
Created November 15, 2013 15:34
Show Gist options
  • Save bmchild/7486212 to your computer and use it in GitHub Desktop.
Save bmchild/7486212 to your computer and use it in GitHub Desktop.
/*
* Business logic service
*/
public class MyBusinessService {
private SomeService someService;
private AnotherService anotherService;
public void executeSomeLogic(String input) {
Something thing = anotherService.makeSomething(input);
thing.setSomeField(anotherService.makeSomeField(input));
someService.doSomething(thing);
}
public void executeSomeMoreLogic(String input) {
Something thing = anotherService.makeSomething(input);
someService.doSomethingElse(thing);
}
}
/*
* Business Logic Test
*/
@RunWith(MockitoJUnitRunner.class)
public class MyBusinessServiceTest {
@Mock
private AnotherService anotherService;
@Mock
private SomeService someService;
@InjectMocks
private MyBusinessService myBusinessService;
@Captor
private ArgumentCaptor<Something> somethingCaptor;
@Before
public void init() {
when(anotherService.makeSomething("A")).thenReturn(new Something());
when(anotherService.makeSomething("B")).thenReturn(new Something());
when(anotherService.makeSomething("C")).thenReturn(new Something());
when(anotherService.makeSomething("D")).thenReturn(new Something());
when(anotherService.makeSomeField(any(Something.class)).thenReturn("something");
}
@Test
public void testExecuteSomeLogic_SCenario1() {
String input = "A";
myBusinessService.executeSomeLogic(input);
verify(someService).doSomething(somethingCaptor.capture());
// ...assert captured values here
}
@Test
public void testExecuteSomeLogic_SCenario2() {
String input = "B";
myBusinessService.executeSomeLogic(input);
verify(someService).doSomething(somethingCaptor.capture());
// ...assert captured values here
}
/*
* This test doesn't use the anotherService.makeSomeField so it clutters up our init
*/
@Test
public void testExecuteSomeMoreLogic() {
String input = "A";
myBusinessService.executeSomeMoreLogic(input);
verify(someService).doSomething(somethingCaptor.capture());
// ...assert captured values here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment