简体   繁体   English

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

[英]Need to Start Tomcat server for test Junit test case

Hello I have write a juint test case for rest api using HttpUriRequest.您好,我已经使用 HttpUriRequest 为 rest api 编写了一个联合测试用例。 The Test case gives the proper result but the issue is that i need to run the tomcat server to test the junit test case.测试用例给出了正确的结果,但问题是我需要运行 tomcat 服务器来测试 junit 测试用例。 why??为什么??

Here is my Code:这是我的代码:

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);
    }
}

This test case is working fine but when i stop the server the test case is not executed.这个测试用例工作正常,但是当我停止服务器时,测试用例没有执行。 Is there is any way to test this case without run the server??有没有办法在不运行服务器的情况下测试这种情况? Thanks谢谢

I think you misunderstood the meaning of "unit tests".我认为您误解了“单元测试”的含义。 If you are doing an http request to a running server, you are testing the whole server, and not just some separate unit.如果您正在对正在运行的服务器发出 http 请求,那么您正在测试整个服务器,而不仅仅是某个单独的单元。

If you want to test the logic inside your controller, just create the controller object manually and call the corresponding method.如果要测试 controller 内部的逻辑,只需手动创建 controller object 并调用相应的方法即可。

If you want to test you api endpoints, spring provides various annotation and different ways in which you could test your appplication.如果你想测试你的 api 端点,spring 提供了各种注释和不同的方法来测试你的应用程序。 Like:喜欢:

  1. You could start up the server during test case, initialize the spring application context and then test the api by hitting it to assert the result.您可以在测试用例期间启动服务器,初始化 spring 应用程序上下文,然后通过点击 api 来测试它以断言结果。
  2. You could just use the spring annotation @SpringBootTest along with @AutoConfigureMockMvc to setup the application context without starting the server to test your application end to end.您可以只使用 spring 注释@SpringBootTest@AutoConfigureMockMvc来设置应用程序上下文,而无需启动服务器来端到端测试您的应用程序。
  3. Yet another approach when you do not want to test the entire application but just need to test the web layer is to use the annotation @WebMvcTest .当您不想测试整个应用程序而只需要测试 web 层时,另一种方法是使用注释@WebMvcTest In this case you need to mock the service layer and any other beans that might be needed to get the desired result.在这种情况下,您需要模拟服务层和获得所需结果可能需要的任何其他 bean。

You can use the annotation @SpringBootTest to tell spring that it should start up the server and have the application context configured to run your test.您可以使用注释@SpringBootTest告诉 spring 它应该启动服务器并配置应用程序上下文以运行您的测试。

The @SpringBootTest annotation tells Spring Boot to look for a main configuration class (one with @SpringBootApplication , for instance) and use that to start a Spring application context. @SpringBootTest注释告诉 Spring Boot 寻找主要配置 class (例如,带有@SpringBootApplication的配置)并使用它来启动 Spring 应用程序上下文。

In that case, you won't have to start your server manually spring will take care of everything and once the tests are done it will stop the server as well.I haven't tried compiling or running the code but use like:在这种情况下,您不必手动启动服务器 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);
    }

}

This should work as long as you have the following dependency in your pom.xml :只要您在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>

The server is needed because you are making tests against the URLs.需要服务器,因为您要针对 URL 进行测试。

The only way you can do this is to mock the server.您可以这样做的唯一方法是模拟服务器。 There are many different mocking libraries out there for Java, like Mockito or EasyMock, but they all allow you to provide fake responses from the server. Java 有许多不同的 mocking 库,例如 Mockito 或 EasyMock,但它们都允许您从服务器提供虚假响应。

From the test case you pasted, it does not make sense to perform such tests, since you seem to be checking that the actual server replies and do nothing with the response.从您粘贴的测试用例来看,执行此类测试没有任何意义,因为您似乎正在检查实际的服务器是否响应并且对响应不做任何事情。 Mocking the server is very useful to test API clients, but you don't seem to be testing that. Mocking 服务器对于测试 API 客户端非常有用,但您似乎没有在测试它。

You have two options here:您在这里有两个选择:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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