Last active
December 28, 2015 10:19
-
-
Save bmchild/7485848 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Business logic service | |
*/ | |
public class MyBusinessService { | |
private DateService dateService; | |
private SomeService someService; | |
public void executeSomeLogic(String dateString) { | |
Something thing = new Something(); | |
thing.setSomeField(dateService.parseDate(dateString)); | |
if(dateService.isToday(thing.getSomeField)) { | |
// ... logic no longer dependant on DateUtils.parseDate && isToday | |
} | |
//... some other logic here | |
someService.doSomething(thing); | |
} | |
} | |
/* | |
* Business Logic Test | |
*/ | |
@RunWith(MockitoJUnitRunner.class) | |
public class MyBusinessServiceTest { | |
@Mock | |
private DateService dateService; | |
@Mock | |
private SomeService someService; | |
@InjectMocks | |
private MyBusinessService myBusinessService; | |
@Captor | |
private ArgumentCaptor<Something> somethingCaptor; | |
@Test | |
public void testExecuteSomeLogic_Scenario1() { | |
// ...setup logic to test executeSomeLogic method logic and to make sure our static utility methods return an expected value | |
when(dateService.parseDate(anyString()).thenReturn(new Date()); | |
when(dateService.isToday(any(Date.class)).thenReturn(true); | |
String input = "anyString"; | |
myBusinessService.executeSomeLogic(input); | |
verify(someService).doSomething(somethingCaptor.capture()); | |
// ...assert captured values here | |
} | |
@Test | |
public void testExecuteSomeLogic_Scenario2() { | |
// ...setup logic to test executeSomeLogic method logic and to make sure our static utility methods return an expected value | |
when(dateService.parseDate(anyString()).thenReturn(new Date()); | |
when(dateService.isToday(any(Date.class)).thenReturn(false); | |
String input = "anyString"; | |
myBusinessService.executeSomeLogic(input); | |
verify(someService).doSomething(somethingCaptor.capture()); | |
// ...assert captured values here | |
} | |
} | |
/* | |
* Date service | |
*/ | |
public class DateService { | |
public Date parseDate(String dateString) { | |
return DateUtils.parseDate(dateString); | |
} | |
public Boolean isToday(Date date) { | |
return DateUtils.parseDate(date); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment