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

时间:2020-04-22 11:12:23

标签: java mockito junit5

您好,我已经使用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);
    }
}

此测试用例工作正常,但是当我停止服务器时,该测试用例未执行。 有没有办法在不运行服务器的情况下测试这种情况? 谢谢

3 个答案:

答案 0 :(得分:2)

我认为您误解了“单元测试”的含义。如果您要向正在运行的服务器发出http请求,那么您将测试整个服务器,而不仅仅是单个单元。

如果要测试控制器内部的逻辑,只需手动创建控制器对象并调用相应的方法即可。

答案 1 :(得分:0)

服务器是必需的,因为您正在根据URL进行测试。

执行此操作的唯一方法是模拟服务器。有许多不同的Java模拟库,例如Mockito或EasyMock,但它们都允许您从服务器提供伪造的响应。

从您粘贴的测试用例中,执行这样的测试是没有意义的,因为您似乎正在检查实际服务器是否答复,并且对响应不执行任何操作。模拟服务器对于测试API客户端非常有用,但是您似乎并没有对此进行测试。

您在这里有两个选择:

答案 2 :(得分:0)

如果要测试api端点,spring提供了各种注释和测试应用程序的不同方法。喜欢:

  1. 您可以在测试用例期间启动服务器,初始化spring应用程序上下文,然后通过点击它来声明结果来测试api。
  2. 您可以仅使用spring注释@SpringBootTest@AutoConfigureMockMvc来设置应用程序上下文,而无需启动服务器来端到端地测试您的应用程序。
  3. 当您不想测试整个应用程序而只需要测试Web层时,另一种方法是使用注释@WebMvcTest。在这种情况下,您需要模拟服务层和可能需要的其他任何bean,以获得所需的结果。

您可以使用注释@SpringBootTest告诉spring它应该启动服务器并配置应用程序上下文以运行您的测试。

@SpringBootTest注释告诉Spring Boot寻找主要的 配置类(例如,带有@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>