[英]Need to Start Tomcat server for test Junit test case
您好,我已经使用 HttpUriRequest 为 rest api 编写了一个联合测试用例。 测试用例给出了正确的结果,但问题是我需要运行 tomcat 服务器来测试 junit 测试用例。 为什么??
这是我的代码:
package com.dataguise.webservices;
import static org.junit.jupiter.api.Assertions.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import com.dataguise.webservices.beans.DgException;
class RestAPIsTest {
final String versionNumber = "v1";
final String baseUrl = "http://localhost:10181/dgcontroller/services/restAPIs/";
final String sessionId = "2";
@Test
public void idpsTest() throws ClientProtocolException, IOException {
HttpUriRequest request = new HttpGet(baseUrl + versionNumber + "/idps");
request.addHeader("sessionId", sessionId);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
System.out.println(">>>>>>>>>>>>>>>." + httpResponse.getStatusLine().getStatusCode());
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
// Getting Json From Http Response
String json = EntityUtils.toString(httpResponse.getEntity());
System.out.println(">>>>>>>>>>>>>>>." + json);
}
}
这个测试用例工作正常,但是当我停止服务器时,测试用例没有执行。 有没有办法在不运行服务器的情况下测试这种情况? 谢谢
我认为您误解了“单元测试”的含义。 如果您正在对正在运行的服务器发出 http 请求,那么您正在测试整个服务器,而不仅仅是某个单独的单元。
如果要测试 controller 内部的逻辑,只需手动创建 controller object 并调用相应的方法即可。
如果你想测试你的 api 端点,spring 提供了各种注释和不同的方法来测试你的应用程序。 喜欢:
@SpringBootTest
和@AutoConfigureMockMvc
来设置应用程序上下文,而无需启动服务器来端到端测试您的应用程序。@WebMvcTest
。 在这种情况下,您需要模拟服务层和获得所需结果可能需要的任何其他 bean。 您可以使用注释@SpringBootTest
告诉 spring 它应该启动服务器并配置应用程序上下文以运行您的测试。
@SpringBootTest
注释告诉 Spring Boot 寻找主要配置 class (例如,带有@SpringBootApplication
的配置)并使用它来启动 Spring 应用程序上下文。
在这种情况下,您不必手动启动服务器 spring 会处理所有事情,一旦测试完成,它也会停止服务器。我没有尝试编译或运行代码,但使用如下:
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {
@LocalServerPort
private int port;
final String versionNumber = "v1";
final String pathUrl = "/dgcontroller/services/restAPIs/";
final String sessionId = "2";
@Test
public void idpsTest() throws ClientProtocolException, IOException {
String baseUrl = "http://localhost:"+port+pathUrl;
HttpUriRequest request = new HttpGet(baseUrl + versionNumber + "/idps");
request.addHeader("sessionId", sessionId);
HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
System.out.println(">>>>>>>>>>>>>>>." + httpResponse.getStatusLine().getStatusCode());
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
// Getting Json From Http Response
String json = EntityUtils.toString(httpResponse.getEntity());
System.out.println(">>>>>>>>>>>>>>>." + json);
}
}
只要您在pom.xml
中具有以下依赖项,这应该可以工作:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
需要服务器,因为您要针对 URL 进行测试。
您可以这样做的唯一方法是模拟服务器。 Java 有许多不同的 mocking 库,例如 Mockito 或 EasyMock,但它们都允许您从服务器提供虚假响应。
从您粘贴的测试用例来看,执行此类测试没有任何意义,因为您似乎正在检查实际的服务器是否响应并且对响应不做任何事情。 Mocking 服务器对于测试 API 客户端非常有用,但您似乎没有在测试它。
您在这里有两个选择:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.