繁体   English   中英

需要启动 Tomcat 服务器进行测试 Junit 测试用例

[英]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 提供了各种注释和不同的方法来测试你的应用程序。 喜欢:

  1. 您可以在测试用例期间启动服务器,初始化 spring 应用程序上下文,然后通过点击 api 来测试它以断言结果。
  2. 您可以只使用 spring 注释@SpringBootTest@AutoConfigureMockMvc来设置应用程序上下文,而无需启动服务器来端到端测试您的应用程序。
  3. 当您不想测试整个应用程序而只需要测试 web 层时,另一种方法是使用注释@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.

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