使用Junit和Mockito在Java EE应用程序中使用Rest api调用测试服务

时间:2018-07-13 09:44:19

标签: java rest unit-testing junit mockito

我正在尝试使用junit和mockito测试Service类。不确定如何在测试方法中包括Http GET Request uri。尝试使用HttpClientBuilder创建和构建请求,但是即使在包含gradle依赖关系后,也无法以某种方式找到导入。我在请求中也有Header参数。

我的Java类如下:

@GET
@Path("/home")
public Response getAllEmployees(@HeaderParam("user") String user, @HeaderParam("password") String password) {
    List<Info> listOfInfo = new ArrayList<>();
    LoginUser loginUser = checkLoginValidation();

    if(loginUser != null){
        List<Employees> list = employeeManager.fetchAllEmployees();

        if(list == null || list.isEmpty()){
            return Response.status(Status.NO_CONTENT).build();
        }

        listOfInfo = getEmployees(list);
        log.info(listOfInfo);
        return Response.ok(listOfInfo, MediaType.APPLICATION_JSON_TYPE).build();
    }

    return Response.status(Status.BAD_REQUEST).build();
}

这就是我在下面的测试课程中写的所有内容:

@RunWith(MockitoJUnitRunner.class)
public class FinderServiceTest {

@InjectMocks
private FinderService finderService;

@Test
public void testMethod() {
    HttpGet request = new HttpGet("http://localhost:8080/home");

//Don't know how to proceed further

}
 }

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

问题表达方式,我担心您正在混合单元测试和集成级别测试。

在单元级别,您要断言当将受控参数传递给方法时,您可以预测性地断言该方法将执行什么操作并返回给客户端。这是一个隔离测试,它将方法与将在其中执行的环境分开,并专注于确保一小部分代码具有可预测的行为。

然后在集成级别将类视为“在运行时存在”。您可以将其放入容器中,启动Bean或服务,并对其进行调用,以确保行为在运行时同样可预测,并且外部客户端将通过整个运行堆栈获得公众期望的结果(或受控堆栈子集)。集成测试正在检查应用程序是否按照您的意愿集体运行。

使用您提出的测试,我觉得您正在尝试对该方法进行单元测试。我建议不要担心在运行时如何从Header派生参数,而直接调用该方法。

下面是我相信如何对 ServiceFinder.getAllLockers(String,String)方法进行单元测试的存根

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;

public class FinderServiceTest {
    // This is created new between each test.
    private ServiceFinder serviceFinder = new ServiceFinder();
/* ***************************************/
/*  PARAMETER TESTS
/* ***************************************/
/*
 * What should happen in these cases? - Is it an Exception? - Should the method return Status.BAD_REQUEST?
 * 
 * org.junit.Assert can be used to compare the response Object with the desired outcome OR
 * org.junit.rules.ExpectedException can be used to verify specific exceptions are thrown
 */
@Test
public void testGetAllLockersNullUserParam() {
    Response response = serviceFinder.getAllLockers(null, "password");

    // Assert the right thing happened.
}

@Test
public void testGetAllLockersEmptyUserParam() {
    Response response = serviceFinder.getAllLockers("", "password");

    // Assert the right thing happened.
}

@Test
public void testGetAllLockersNullPasswordParam() {
    Response response = serviceFinder.getAllLockers("user", null);

    // Assert the right thing happened.
}

@Test
public void testGetAllLockersEmptyPasswordParam() {
    Response response = serviceFinder.getAllLockers("user", "");

    // Assert the right thing happened.
}

/* ***************************************/
/*  BRANCH TESTS (SHORT PATHS)
/* ***************************************/

@Test
public void testGetAllLockersNullValidatedUser() {
    // For ease of use in my case I'm going to pretend that checkLoginValidation 
    // just calls a delegate interface, which I'm calling LoginValidator, with the same API.
    // Which I will mock and set the expected return...
    LoginValidator mockLoginValidator = Mockito.mock(LoginValidator.class);
    Mockito.when(mockLoginValidator.checkLoginValidation()).thenReturn(null);
    //Using PowerMock, I'm going to set the LoginValidator field inside of my service finder.
    //I'm assuming that LoginValidator field in the ServiceFinder is named "loginValidator"
    Whitebox.setInternalState(serviceFinder, "loginValidator", mockLoginValidator);

    //Now that my class is set up to give me the null Validated User, I'll make the call to the test instance
    Response response = serviceFinder.getAllLockers("validUser", "validPassword");

    //From the implementation posted, I know that when the user validates as null I should get back something with a Status.BAD_REQUEST state.
    assertEquals("When the logged in user is null BAD_REQUEST should be returned", Status.BAD_REQUEST, response.getStatus);   
}

@Test
public void testGetAllLockersNullEmployeeList() {
    //FIXME:  Configure user validation to return LoginUser Object.
    //FIXME:  Configure test reference state to return a null employee list when employeeManager.fetchAllEmployees() is called

    Response response = serviceFinder.getAllLockers("validUser", "validPassword");
    assertEquals("When the employee list is null NO_CONTENT should be returned", Status.NO_CONTENT, response.getStatus);   
}

@Test
public void testGetAllLockersEmptyEmployeeList() {
  //FIXME:  Configure user validation to return LoginUser Object.
    //  FIXME:  Configure test reference state to return an empty employee list when employeeManager.fetchAllEmployees() is called

    Response response = serviceFinder.getAllLockers("validUser", "validPassword");
    assertEquals("When the employee list is null NO_CONTENT should be returned", Status.NO_CONTENT, response.getStatus);
}

/* ***************************************/
/*  HAPPY PATH TEST
/* ***************************************/
public void testgetAllLockers() {
  //FIXME:  Configure user validation to return LoginUser Object.
    //  FIXME:  Configure test reference state to return a correctly-populated employee list when employeeManager.fetchAllEmployees() is called

    Response response = serviceFinder.getAllLockers("validUser", "validPassword");
    assertEquals("When the employee list is null NO_CONTENT should be returned", Status.OK, response.getStatus);
    //FIXME get JSON from response reference
    //FIXME Check that JSON holds all of the expected employee list data
}

}

在单位一级确保该方法重复执行我们期望的操作。


整合测试是我心目中的另一头野兽。您需要使代码在服务器上运行,并设置一个可以针对正在运行的URL进行调用的工具。我不太熟悉这种配置,因此在这方面我没有太多帮助。我确实知道有很多方法可以做到这一点,并且有许多程序和实用程序可以提供帮助。关于集成测试的一些google搜索都使用了api,我敢肯定您会拥有很多选择。我建议您寻找一种与最终要运行的环境非常相似的解决方案。

我能提供的是要注意的是,在集成测试期间,您将需要使用 HttpClientBuilder 之类的工具,或使用 JMeter Postman之类的工具将信息请求发送到服务器,然后阅读并验证响应是您所期望的。您可能需要使用我们在单元测试中尝试过的一些数据,以确保运行的系统不会更改预期的结果。

好运!