我能够使用 jersey 测试框架来测试我的 JAX-RS restful api。但是,无论我尝试什么,我都无法正确地模拟我的 EJB。
使用@peeskillet提供的示例,在资源中引用我的 EJB 的点仍然为空。
所以我知道这不是我的依赖。我相信问题在于我已经嵌套了 EJB。我确信有几个因素可能会影响这段代码的行为,但我不确定如何解决: 1. 我试图模拟的 ApplicationService 是一个接口,我有一个 ApplicationServiceImpl 类里面有实际的逻辑。由于我的测试不专注于该逻辑,因此我的思考过程是模拟界面应该足够了。2. 然而,接口的实现还包含一个连接到 MongoDB 单例的 EJB。
所以我的问题是:为什么我的资源类中的接口引用始终为空,因此显然无法模拟和注入,这些因素中的任何一个是否在这里发挥作用?如果是这样,我该如何解决?
如果我需要切换到直接模拟实现而不是接口,那么我是否还需要模拟 DAO ejb?
需要明确的是,只要我不调用任何依赖于通过 EJB 注入的字段/对象的方法,测试框架就可以正常工作。
我的资源类
@Path("/stats")
@Api(value = "stats", authorizations = {
@Authorization(value = "apiKey", scopes = {})
})
@Stateless
@Produces({MediaType.APPLICATION_JSON})
public class StatsResource {
private final static Logger LOG = Logger.getLogger(StatsResource.class.getName());
@EJB
ApplicationService applicationService;
public void setApplicationService(ApplicationService applicationService) {
this.applicationService = applicationService;
}
@GET
public String getHere(){
return "Got Here!";
}
@GET
@Path("test")
@ApiOperation(value = "Finds all applications",
notes = "Returns brief display which uses a limited subset of Application.class fields",
response = Application.class,
responseContainer = "List<Application>"
)
@JsonView(Views.Brief.class)
@Asynchronous
public void getApplications(@Suspended
final AsyncResponse asyncResponse) throws Exception {
asyncResponse.resume(doGetApplications());
}
private ApplicationList doGetApplications() throws Exception {
return new ApplicationList(applicationService.getAll());
}
}
我的测试用例
public class StatsResourceNGTest extends JerseyTestNg.ContainerPerMethodTest {
@Mock
private ApplicationService applicationService;
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
MockitoAnnotations.initMocks(this);
Logger.getLogger(StatsResourceNGTest.class.getName()).log(Level.INFO, "Mock App Service: {0}", applicationService.toString());
AbstractBinder binder = new AbstractBinder() {
@Override
protected void configure() {
bindFactory(MockApplicationServiceFactory.class)
.to(ApplicationResource.class);
}
};
ResourceConfig config = new IdentityRestApplication(true);
config.register(binder);
config.register(StatsResource.class);
return config;
}
@Test
public void assertMockInjectionWorked() throws Exception {
Response response = target("/stats/test").request(MediaType.APPLICATION_JSON).get();
verify(applicationService, times(1)).getAll();
Logger.getLogger(ApplicationResourceNGTest.class.getName()).log(Level.INFO, "Status: {0}", response.getStatus());
String msg = response.readEntity(String.class);
Logger.getLogger(ApplicationResourceNGTest.class.getName()).log(Level.INFO, "Response: {0}", msg);
Assert.assertNotNull(msg);
response.close();
}
}
我的 HK2 模拟工厂
public class MockApplicationServiceFactory implements Factory<ApplicationService> {
private List<Application> appList;
@Override
public ApplicationService provide() {
appList = new ArrayList<>();
for (int i = 1; i < 6; i++) {
String appname = "App " + i;
Application app = new ApplicationBuilder()
.setName(appname)
.setDescription(appname + " Description")
.setTenantId(new ObjectId())
.createApplication();
appList.add(app);
}
try {
final ApplicationService mockedService = Mockito.mock(ApplicationService.class);
Mockito.when(mockedService.getAll()).thenReturn(appList);
return mockedService;
} catch (ApplicationException ex) {
Logger.getLogger(ApplicationResourceNGTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(ApplicationResourceNGTest.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
@Override
public void dispose(ApplicationService t) {
}
}