我有一个类名ServiceLocator
public class ServiceLocator implements ApplicationContextAware {
private transient ApplicationContext _applicationContext;
private static ServiceLocator _instance = new ServiceLocator();
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
_instance._applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return _instance._applicationContext;
}
public static Object findService(String serviceName) {
return _instance._applicationContext.getBean(serviceName);
}
}
我正在尝试使用该类将Service查找到Approver类方法中
public class ApproverService extends AbstractDataService implements IApproverService {
public void updateCompletedInboxStatus(String status) {
IInboxService inboxService = (IInboxService)ServiceLocator.findService("inboxService");
InboxItem inboxItem = inboxService.getInboxItem("test");
inboxItem.setWorkItemStatus(status);
inboxService.saveInboxItem(inboxItem);
}
}
使用该代码,我试图用PowerMockRunner编写Junit
@RunWith(PowerMockRunner.class)
@PrepareForTest({ApproverService.class})
public class ApproverServiceTest {
@InjectMocks
ApproverService approverService;
@Mock
IInboxService inboxService;
@Mock
ServiceLocator serviceLocator;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void updateCompletedInboxStatus() {
RequestAccessHeader reqHdr = new RequestAccessHeader();
reqHdr.setRequestStatus(AccessConstants.REQ_STATUS_HOLD_INT);
String status = "test";
PowerMockito.mockStatic(ServiceLocator.class);
when(serviceLocator.findService("inboxService")).thenReturn(inboxService);
approverService.updateCompletedInboxStatus(status);
}
}
但是我得到空指针
java.lang.NullPointerException在com.alnt.fabric.common.ServiceLocator.findService(ServiceLocator.java:25)在com.alnt.access.approver.service.ApproverServiceTest.updateCompletedInboxStatus(ApproverServiceTest.java:80)
[请帮助我找到该问题的解决方案。
静态方法的设置未正确模拟
@RunWith(PowerMockRunner.class)
@PrepareForTest({ServiceLocator.class}) //Prepare static class for mock
public class ApproverServiceTest {
@Mock
IInboxService inboxService;
@Mock
InboxItem item;
@InjectMocks
ApproverService approverService;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void updateCompletedInboxStatus() {
//Arrange
String status = "test";
PowerMockito.mockStatic(ServiceLocator.class);
when(ServiceLocator.findService("inboxService")) //<-- NOTE static call
.thenReturn(inboxService);
when(inboxService.getInboxItem("test")).thenReturn(item);
//Act
approverService.updateCompletedInboxStatus(status);
//...
}
}
显然没有嘲笑静态方法。
问题很可能是因为您尚未在@PrepareForTest
中添加要模拟的类
将其更改为@PrepareForTest({ApproverService.class, ServiceLocator.class})
主题外:
尽管可以编译,但是通过实例引用调用静态方法不是一个好习惯。因此,该行应为when(ServiceLocator.findService(...)).thenReturn(inboxService)
。
另一个问题是,您尝试使用Singleton模式,但方式错误。假设单例返回一个实例,以便调用者可以调用其实例方法。您的findService
优选是实例方法,被称为ServiceLocator.getInstance().findService(...)
。为了进一步改进,除非您真的需要将其作为单例,否则应将其设置为普通的对象实例,并注入需要它的对象(假设您已经在使用Spring,我认为没有必要制作单例)