2013-07-12 68 views
2

我用的球衣,我有以下的休息函数返回时,我的服务器部署一个JSON字符串:呼叫一个休息法的Mockito

@GET 
@Path("getallemployees") 
@Produces("application/json") 
public Response getAllEmployees() { 
//building the entity object which is List<Employee> 
return Response.ok(entity).build(); 
} 

我需要开发一些单元测试(不集成测试)和我想以某种方式模拟调用此方法的HTTPRequest,然后获取json字符串。最好的选择是为此使用mockito。

有没有关于如何做的建议?

谢谢!

回答

1

问题是该方法返回一个Response对象到深层框架代码中的调用者。它不返回JSON字符串。

如果你需要在方法本身内部嘲弄某些东西,你可以使用Mockito。这应该工作。

但是,如果您在Jersey中使用Jackson,您可能需要采用该方法返回的值并将其转换为JSON。

Response response = getAllEmployees(); 
Object retval = response.getEntity(); 
try { 
    ObjectMapper mapper = new ObjectMapper(); 
    // I like this formatting. You can change it. 
    mapper.configure(Feature.INDENT_OUTPUT, true); 
    mapper.configure(Feature.WRITE_ENUMS_USING_TO_STRING, true); 
    mapper.configure(Feature.USE_ANNOTATIONS, false); 
    mapper.configure(Feature.FAIL_ON_EMPTY_BEANS, false); 
    mapper.setSerializationInclusion(Inclusion.NON_NULL); 
    mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); 
    mapper.getSerializationConfig().withSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); 
    String json = mapper.writeValueAsString(retval); 
    ... assert something about the string 
} catch (JsonProcessingException e) { 
    // do something 
} catch (IOException e) { 
    // do something 
} 
+0

哪个杰克逊版本给你用这个?因为Feature.INDENT_OUTPUT不被识别... – SteveSt

+0

杰克逊版本1.9.4 –

+0

嗯,这实际上工作:),唯一的问题是,我得到整个实体对象为JSON字符串,它看起来像这样:'{“rawType” : “的java.util.ArrayList”, “类型”:{ “actualTypeArguments”:[ “dev.entities.Employee”], “rawType”:“java.util中。列表“},”实体“:[{”idEmployee“:0,”name“:”theName“,”surname“:”theSurname“}' 而原始的json看起来像这样: ''employee':[ {“idEmployee”:0,“name”:“theName”,“surname”:“theSurname”}' – SteveSt

1

一些,这是猜测和炒作我的一部分,但它可能会有所帮助。你可以尝试使用Jersey Test FrameworkInMemoryTestContainerFactory

它始于新泽西的应用和直接调用内部API来处理由测试框架提供的客户端创建请求。没有涉及网络通信。这个容器不支持servlet和其他容器相关的功能,但它是简单单元测试的完美选择。

它看起来像使用它,你需要做的是延长JerseyTest,然后覆盖getTestContainerFactory()和遵循的其他说明,如:

public class EmployeeResourceTest extends JerseyTest { 
    @Override 
    protected Application configure() { 
     // set up employee resource with mock dependencies etc... 
     return new ResourceConfig().registerInstances(employeeResource); 
    } 

    @Test 
    public void getAllEmployees() { 
     final String response = target("getallemployees").request().get(String.class); 
     // assert etc... 
    } 
} 

configure()使用registerInstances代替registerClasses因为它看起来像你可以提供一个现成的Resource,但设置任何模拟依赖你可能需要 - 虽然我没有尝试过这个我自己。

测试类有点不灵活,因为您只能在configure()方法中一次性设置依赖关系,因此可能需要使用MockitoJUnitRunner进行调查 - 尽管我不确定它是否可以与JerseyTest继承。它可以让你做每一@Test方法添加行为嘲笑,例如:

@Mock 
    private EmployeeResourceDependency dependency; 

    @InjectMocks 
    private EmployeeResource employeeResource; 

    // configure() as above but without mock setup up etc... 

    @Test 
    public void getAllEmployees() { 
     given(dependency.getEmployees()).willReturn(...); 

     // etc... 

但是就像我说可能不可能将他们在所有混合。