模拟对象无法使用CXF WebClient.create()。post()

时间:2016-12-07 06:35:37

标签: java rest mockito cxf

我正在尝试为Web服务调用编写单元测试。使用Mockito模拟DAO层。问题我面临的问题是,当调用WebClient的post()时,模拟的对象变为空。请求在不同的上下文中加载。以下是代码段:

    @Before
    public void doSetup() {
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setResourceClasses(UserService.class);
    List<Object> providers = new ArrayList<Object>();
    providers.add(new JacksonJsonProvider());
    sf.setProviders(providers);
    sf.setResourceProvider(UserService.class, 
            new SingletonResourceProvider(new UserService(),true));
    sf.setAddress(ENDPOINT_ADDRESS);
    Server server = sf.create();

    mockDelegator = Mockito.mock(UserDelegator.class);
    mockUserDao = Mockito.mock(UserDAO.class);
    mockDelegator.setUserDao(userDao);
    userService.setUserDelegator(mockDelegator);
    }

    @Test    
    public void fetchPersonalInformationTest() {
    WebClient client = WebClient.create(ENDPOINT_ADDRESS);
    WebClient.getConfig(client).getRequestContext().put(LocalConduit.DIRECT_DISPATCH, Boolean.TRUE);
    client.path("users/fetchPersonalInfo");
    client.accept("application/json");
    client.type("application/json");
    Response response = client.post("{\"userName\":\"vijay@1357\"}"); //This call's UserService and thows NullPointerException for userDao and mockDelegator
    }

以下代码段来自UserService类:

@POST
@Path("/fetchPersonalInfo")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response fetchPersonalInformation(User user) {
    logger.error("Logger Running");
    String fetchJsonPersonalInfo=null;
    System.out.println(user.getUserName());
    try {
        BasicUtils.isNull(user.getUserName());
        BasicUtils.isNull(userDelegator);
        fetchJsonPersonalInfo = userDelegator.fetchPersonalInformation(user);
    } catch (ApplicationException e) {
        e.printStackTrace();

    }
    return Response.status(200).entity(fetchJsonPersonalInfo).build();
}

UserDelegator类

public String fetchPersonalInformation(User user) {
    BasicUtils.isNull(userDao);
    return userDao.fetchPersonalInformation(user);
}

UserDAO clas

public String fetchPersonalInformation(User user) {
    logger.info("Started Execution at " + System.currentTimeMillis());
    String json=null;
    try {
        Statement query = QueryBuilder.select().json().from("members_data", "users").where(QueryBuilder.eq("username",user.getUserName()));
        ResultSet resultset = executeStatementOrQuery(query);
        Row row = resultset.one();
        json = row.getString("[json]");
    }
    catch(Exception e) {
        e.printStackTrace();
        throw new ApplicationException(Constants.ERROR_CODE_101, errMsg101, e);
    }
    logger.info("Finished Execution at " + System.currentTimeMillis());
    return json;
}

1 个答案:

答案 0 :(得分:0)

发现问题。问题在于为JAXRSServerFactoryBean设置资源类。更改了以下代码:

@Before
public void doSetup() {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(UserService.class);

参与

@Before
public void doSetup() {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setServiceBean(userService);
  

如果您不想让JAX-RS运行时为您处理资源类生命周期(例如,您的资源类可能是由其他容器(如Spring)创建的)

参考文件CXF JAX-RS Service Configuration