使用junit和mockito在JavaEE应用程序中使用RESTAPI调用测试服务
我正在尝试使用junit和mockito测试一个服务类。不确定如何在测试方法中包括Http GET请求uri。尝试使用HttpClientBuilder创建和构建请求,但不知何故,即使在我包含gradle依赖项之后,也找不到导入。我在请求中也有头参数 我的Java类如下所示:使用junit和mockito在JavaEE应用程序中使用RESTAPI调用测试服务,java,rest,unit-testing,junit,mockito,Java,Rest,Unit Testing,Junit,Mockito,我正在尝试使用junit和mockito测试一个服务类。不确定如何在测试方法中包括Http GET请求uri。尝试使用HttpClientBuilder创建和构建请求,但不知何故,即使在我包含gradle依赖项之后,也找不到导入。我在请求中也有头参数 我的Java类如下所示: @GET @Path("/home") public Response getAllEmployees(@HeaderParam("user") String user, @HeaderParam("password")
@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();
}
任何帮助都将不胜感激 从你的问题的措辞来看,我担心你在混合单元和集成级测试 在单元级别,您希望断言,当受控参数传递给方法时,您可以预测地断言该方法将执行什么操作并返回给客户机。它是一个隔离测试,将该方法与它将在其中执行的环境分离,并侧重于确保代码的一小部分具有可预测的行为 在集成级别上,您可以将该类视为“在运行时存在”。您可以将它放在一个容器中,启动bean或服务,并对其进行调用,以确保当它运行时,行为是同样可预测的,并且外部客户端将通过您的完整运行堆栈(或受控堆栈子集)从公众处获得您期望的结果。集成测试是检查应用程序是否按照预期运行 对于您提出的测试,我感觉您正在尝试对该方法执行单元测试。我建议不要担心在运行时如何从头中派生参数,直接调用该方法即可 下面是我如何尝试对ServiceFinder.getAllLockers(String,String)方法进行单元测试的存根 在单元级别,确保方法重复执行我们期望的操作
集成测试是我心目中的另一个怪兽。您需要让您的代码在服务器上运行,并设置一个可以对运行的URL进行调用的工具。这种配置我不太熟悉,所以在这方面我帮不了什么忙。我知道有很多方法可以做到这一点,还有很多程序和实用程序可以提供帮助。在集成测试RESTAPI上进行一些google搜索,我相信您会有很多选择。我建议您寻找一种解决方案,该解决方案非常类似于您最终希望在其上运行的环境 我可以提供的是,在集成测试期间,您需要使用诸如HttpClientBuilder之类的工具,或者使用诸如JMeter或Postman之类的工具,向服务器发出信息请求,然后读取并验证响应是否符合预期。您可能希望使用我们在单元测试中尝试的一些数据,以确保正在运行的系统不会更改预期结果
祝你好运 从你的问题的措辞来看,我担心你在混合单元和集成级测试 在单元级别,您希望断言,当受控参数传递给方法时,您可以预测地断言该方法将执行什么操作并返回给客户机。它是一个隔离测试,将该方法与它将在其中执行的环境分离,并侧重于确保代码的一小部分具有可预测的行为 在集成级别上,您可以将该类视为“在运行时存在”。您可以将它放在一个容器中,启动bean或服务,并对其进行调用,以确保当它运行时,行为是同样可预测的,并且外部客户端将通过您的完整运行堆栈(或受控堆栈子集)从公众处获得您期望的结果。集成测试是检查应用程序是否按照预期运行 对于您提出的测试,我感觉您正在尝试对该方法执行单元测试。我建议不要担心在运行时如何从头中派生参数,直接调用该方法即可 下面是我如何尝试对ServiceFinder.getAllLockers(String,String)方法进行单元测试的存根 在单元级别,确保方法重复执行我们期望的操作
集成测试是我心目中的另一个怪兽。您需要让您的代码在服务器上运行,并设置一个可以对运行的URL进行调用的工具。这种配置我不太熟悉,所以在这方面我帮不了什么忙。我知道有很多方法可以做到这一点,还有很多程序和实用程序可以提供帮助。在集成测试RESTAPI上进行一些google搜索,我相信您会有很多选择。我建议您寻找一种解决方案,该解决方案非常类似于您最终希望在其上运行的环境 我可以提供的是,在集成测试期间,您需要使用诸如HttpClientBuilder之类的工具,或者使用诸如JMeter或Postman之类的工具,向服务器发出信息请求,然后读取并验证响应是否符合预期。您可能希望使用我们在单元测试中尝试的一些数据,以确保正在运行的系统不会更改预期结果
祝你好运 非常感谢你!我把单元测试和集成测试搞混了。这很有帮助。成功了@杰里米太感谢你了!我把单元测试和集成测试搞混了。这很有帮助。成功了@耶利米
@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
}
}
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
}
}