我能够使用jersey测试框架来测试我的JAX-RS restful api。但是,无论我尝试什么方法,都不能正确模拟我的EJB。
使用@peeskillet提供的示例,我的Resource中引用EJB的点仍然为null。
因此,我知道这不是我的依赖关系的问题。我相信问题在于我有嵌套的EJB。有一些因素可能会影响代码的行为,但我不确定如何解决:
1. 我要模拟的ApplicationService是一个接口,我有一个ApplicationServiceImpl类,其中包含实际逻辑。由于我的测试并不关注该逻辑,所以我认为模拟接口应该足够了。 2. 但是,接口的实现也包含一个连接到MongoDB单例的EJB。
所以我的问题是: 以上两个因素是否在这里起作用,导致我的资源类中的接口引用始终为空,从而明显地无法被模拟和注入?如果是这样,我该如何解决?
如果我需要切换到直接模拟实现而不是接口,那么我是否还需要模拟DAO EJB?
需要明确的是,只要我不调用通过EJB注入的字段/对象来依赖任何方法,测试框架就完全正常工作。
我的Resource类:
使用@peeskillet提供的示例,我的Resource中引用EJB的点仍然为null。
因此,我知道这不是我的依赖关系的问题。我相信问题在于我有嵌套的EJB。有一些因素可能会影响代码的行为,但我不确定如何解决:
1. 我要模拟的ApplicationService是一个接口,我有一个ApplicationServiceImpl类,其中包含实际逻辑。由于我的测试并不关注该逻辑,所以我认为模拟接口应该足够了。 2. 但是,接口的实现也包含一个连接到MongoDB单例的EJB。
所以我的问题是: 以上两个因素是否在这里起作用,导致我的资源类中的接口引用始终为空,从而明显地无法被模拟和注入?如果是这样,我该如何解决?
如果我需要切换到直接模拟实现而不是接口,那么我是否还需要模拟DAO EJB?
需要明确的是,只要我不调用通过EJB注入的字段/对象来依赖任何方法,测试框架就完全正常工作。
我的Resource类:
@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();
}
}
My HK2 MockFactory
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) {
}
}