使用JUnit测试正确运行此WebService的方法getWsInfoByMethod
。返回WsInfo
DTO
@Stateless
@WebService(serviceName = "WsMethodService")
public class WsMethodService implements WebServiceMethodLocal {
@EJB
private WebServiceMethodDao webServiceMethodDao;
public WsMethodService() {
}
@Override
@WebMethod(operationName = "getWsInfoByMethod")
public WsInfo getWsInfoByMethod(@WebParam(name = "webMethodId") Long webMethodId) throws ServiceFault {
System.out.println("webServiceMethodDao " + webServiceMethodDao);
return webServiceMethodDao.getWebServiceByMethod(webMethodId);
}
}
现在我想在Managed Bean中调用此方法" WsFunction"
@Stateless
public class WsFunction {
@EJB
private WsMethodService ws;
private WsInfo getWsInfo(String webMethodId) throws ServiceFault{
System.out.println("ws " + ws);
WsInfo wsi = ws.getWsInfoByMethod(Long.valueOf(webMethodId));
return wsi;
}
}
但ws
无法注入:
***ws null
Exception in thread "main" java.lang.NullPointerException
at mn.interactive.module.meta.expression.function.WsFunction.getWsInfo(WsFunction.java:75);***
如何解决此错误?
答案 0 :(得分:0)
在WsFunction调用中尝试@Inject而不是@EJB
答案 1 :(得分:0)
WsMethodService
应仅用于在外面调用。你不应该注射它。如果您想使用它的某些功能,那么您需要将该部分抽象到单独的服务层中。为什么不将WebServiceMethodDao
注入WsFunction
并将其调用?
在实践中,注射问题取决于应用程序服务器,您没有指定您正在使用的内容。此外,当您注入EJB时,您需要引用接口(WebServiceMethodLocal
),而不是实现(WsMethodService
)。