间谍部分嘲笑
@Spy 注释 (或方法 )可用于部分模拟对象。当你想要部分模拟类的行为时,这非常有用。例如,假设你有一个使用两个不同服务的类,并且你只想模拟其中一个并使用其他服务的实际实现。
旁注:尽管哲学上我不认为这是真正意义上的纯单元测试,因为你正在整合一个真正的类而不是在完全隔离的情况下测试你的测试类。然而,这在现实世界中实际上是有用的,当我使用内存数据库实现中的一些模拟数据库时,我经常使用它,以便我可以使用真正的 DAO。
Class under test:
public class GreetingsService { // class to be tested in isolation
private UserService userService;
private AppService appService;
public GreetingsService(UserService userService, AppService appService) {
this.userService = userService;
this.appService = appService;
}
public String getGreetings(int userId, LocalTime time) { // the method under test
StringBuilder greetings = new StringBuilder();
String timeOfDay = getTimeOfDay(time.getHour());
greetings.append("Good ").append(timeOfDay).append(", ");
greetings.append(userService.getFirstName(userId)) // this call will be mocked
.append(" ")
.append(userService.getLastName(userId)) // this call will be mocked
.append("!");
greetings.append(" Welcome to ")
.append(appService.getAppName()) // actual method call will be made
.append(".");
return greetings.toString();
}
private String getTimeOfDay(int hour) { // private method doesn't need to be unit tested
if (hour >= 0 && hour < 12)
return "Morning";
else if (hour >= 12 && hour < 16)
return "Afternoon";
else if (hour >= 16 && hour < 21)
return "Evening";
else if (hour >= 21 && hour < 24)
return "Night";
else
return null;
}
}
将模拟此接口的行为:
public interface UserService {
String getFirstName(int userId);
String getLastName(int userId);
}
假设 UserService
的实际实现:
public class UserServiceImpl implements UserService {
@Override
public String getFirstName(int userId) {
String firstName = "";
// some logic to get user's first name
// this could be anything like a call to another service,
// a database query, or a web service call
return firstName;
}
@Override
public String getLastName(int userId) {
String lastName = "";
// some logic to get user's last name
// this could be anything like a call to another service,
// a database query, or a web service call
return lastName;
}
}
不会模拟此接口的行为:
public interface AppService {
String getAppName();
}
假设 AppService
的实际实现:
public class AppServiceImpl implements AppService {
@Override
public String getAppName() {
// assume you are reading this from properties file
String appName = "The Amazing Application";
return appName;
}
}
与 Mockito 一起进行 Junit 测试:
public class GreetingsServiceTest {
@Mock
private UserServiceImpl userService; // this class will be mocked
@Spy
private AppServiceImpl appService; // this class WON'T be mocked
@InjectMocks
private GreetingsService greetingsService = new GreetingsService(userService, appService);
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testGetGreetings_morning() throws Exception {
// specify mocked behavior
when(userService.getFirstName(99)).thenReturn("John");
when(userService.getLastName(99)).thenReturn("Doe");
// invoke method under test
String greetings = greetingsService.getGreetings(99, LocalTime.of(0, 45));
Assert.assertEquals("Failed to get greetings!", "Good Morning, John Doe! Welcome to The Amazing Application.", greetings);
}
@Test
public void testGetGreetings_afternoon() throws Exception {
// specify mocked behavior
when(userService.getFirstName(11)).thenReturn("Jane");
when(userService.getLastName(11)).thenReturn("Doe");
// invoke method under test
String greetings = greetingsService.getGreetings(11, LocalTime.of(13, 15));
Assert.assertEquals("Failed to get greetings!", "Good Afternoon, Jane Doe! Welcome to The Amazing Application.", greetings);
}
}