3

我做了一个测试用例来测试我的 Rest Web Service。但在测试用例中,我看到请求将发送到 jersey 测试框架的默认端口, http://localhost:9998而我的服务在http://localhost:8080. 我找不到如何将其端口更改为8080

public class UMServiceTest extends JerseyTest {


    @Override
    public Application configure() {
        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);
        return new ResourceConfig(UMService.class);
    }    


    @Test
    public void testFetchAll() {
        System.out.println(getBaseUri()+"==========");
        Response output = target("usermanagement").path("um").path("user").request().get();
        assertEquals("should return status 200", 200, output.getStatus());
        //assertNotNull("Should return list", output.getEntity());
    }
4

4 回答 4

5

您也可以从 JerseyTest 的 TestProperties 更改 Systemproperty。

public class UMServiceTest extends JerseyTest
{
    static
    {
        System.setProperty("jersey.config.test.container.port", "0");
    }
...
于 2019-02-19T11:02:43.587 回答
5

您可以在运行测试时提供命令行参数,例如,

马文 mvn yourpack.UMServiceTest -Djersey.config.test.container.port=8080

或在 Eclipse 中,您可以在运行配置“参数”选项卡中传递它

于 2016-10-01T18:16:14.817 回答
2

除了 kuhajeyan 的回答,这里是 JerseyTest 端口的 Maven 配置:

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                ....
                <systemProperties>
                    <property>
                        <name>jersey.config.test.container.port</name>
                        <value>4410</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>
于 2017-10-20T17:14:58.753 回答
0

As of Jersey 2.33, the configure method can be set as in the following example taken from the Jersey Docs

@Override
protected Application configure() {
    // Find first available port.
    forceSet(TestProperties.CONTAINER_PORT, "0");

    return new ResourceConfig(Resource.class);
}

In case, the configureDeployment is used to specify the resource, the below approach can be used.

  @BeforeClass
  public static void beforeClass() {
    System.setProperty(
        // Use a random available port
        "jersey.config.test.container.port", String.valueOf(SocketUtils.findAvailableTcpPort()));
  }

  @AfterClass
  public static void afterClass() {
    System.clearProperty("jersey.config.test.container.port");
  }

  @Override
  protected DeploymentContext configureDeployment() {
    return ServletDeploymentContext.forServlet(
            new ServletContainer(ResourceConfig.forApplicationClass(TestApplication.class)))
        .contextPath("/cas/api")
        .build();
  }

Source: https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/test-framework.html#parallel

于 2021-02-08T16:14:42.237 回答