我做了一个测试用例来测试我的Rest Web服务。但在测试用例中,我看到请求将是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());
}
您可以在运行测试时给出命令行参数,例如:
Maven mvn yourpack.UMServiceTest -Djersey.config.test.container.port=8080
或者在eclipse中你可以在run config'Arguments'选项卡中传递它
除了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>
您也可以从JerseyTest的TestProperties更改Systemproperty。
public class UMServiceTest extends JerseyTest
{
static
{
System.setProperty("jersey.config.test.container.port", "0");
}
...