简体   繁体   English

使用请求标头,遗留代码对带有REST REST API的单元测试

[英]Unit testing java REST API with request headers, legacy code

I need to pass in a non-null HttpServletRequest somehow or control HelperClass.getUser(request) which is throwing an error(the method does request.getHeader() inside and as the request is null it throws an error and fails the test). 我需要以某种方式传入一个非null的HttpServletRequest或者控制HelperClass.getUser(request),这会抛出一个错误(该方法会在request.getHeader()内部执行,并且当请求为null时,它会抛出错误并使测试失败)。

We would like to unit test our REST API, so we quickly know if a change (or a developer) breaks it or if we have a bug etc. eventually to start doing TDD/BDD. 我们想对我们的REST API进行单元测试,因此我们很快就会知道更改(或开发人员)是否会破坏它,或者我们是否有错误等等,最终会开始执行TDD / BDD。 These tests are destined eventually for use for automated builds by ant. 这些测试最终将用于ant的自动构建。 I can call REST API and set headers from java but don't think this would be a unit test? 我可以调用REST API并从java设置头文件,但不认为这是一个单元测试? Would depend on running it on local host? 将取决于在本地主机上运行它? Could be wrong about that 可能是错的

Code: 码:

@Path("service/")
public class Service {
static DataAccess da = null;
@javax.ws.rs.core.Context HttpServletRequest request;

//constructor where doa will be mock object
public Service(DataAccess dao){
da = dao;
}

@GET
@Path("custom-object/{date}/{batch}")
@Produces({"application/xml", "application/json", "application/x-protobuf"})

//Method to test
public CustomObject getCustomObject(
@PathParam("date") String date,
@PathParam("batch") int batch,
@QueryParam("type") String type) {
String user = HelperClass.getUser(request); // this is causing me issues
//da will be a mock object
CustomObject returnedVal = da(user,DatatypeConverter.parseDateTime(date).getTime(), batch, artifactType);
return returnedVal;
 }
}

Test using junit/mockito (happy to use powermock as a solution) : 使用junit / mockito测试(很高兴使用powermock作为解决方案):

@Test    
public void testGetCustomObject() {
    System.out.println("getCustomObject");

    //Arrange
    DataAccess da = mock(DataAccess.class);
    Service instance = new Service(da);
    String date = "2010-04-05T17:16:00Z";
    int batch = 0;
    String type = "";

    CustomObject expResult = null;
    //Act
    CustomObject result = instance.getCustomObject(date, batch, type);
    //Assert
    assertEquals(expResult, result);
}

The test passes if I hardcode the String user="123"; 如果我对String user="123";硬编码,则测试通过String user="123"; . I need to get over this problem before writing useful tests. 我需要在编写有用的测试之前解决这个问题。 Can anyone give example code to mock/control this line of code to effectively set user as a non-null string (this line String user = HelperClass.getUser(request); is in EVERY API method) 任何人都可以给出示例代码来模拟/控制这行代码以有效地将user设置为非空字符串(此行String user = HelperClass.getUser(request);在每个API方法中)

您可以尝试使用REST保证库来帮助单元测试REST服务。

For constructing HttpServletRequest you could use some mock object like Spring's MockHttpServletRequest. 为了构造HttpServletRequest,你可以使用一些模拟对象,比如Spring的MockHttpServletRequest。

Or you could refactor your helper class and make the getUser() method non-static and use Mockito to mock it in your unit tests. 或者你可以重构你的助手类并使getUser()方法非静态,并使用Mockito在你的单元测试中模拟它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM