【发布时间】:2018-02-02 17:15:35
【问题描述】:
我无法让 Jersey 与 Junit 合作。有人可以在这里帮忙吗?我不知道还能做什么。
我正在使用我的 tomcat 服务器,当我运行项目时,我右键单击该文件夹并选择“在服务器上运行”。完成此操作后,我可以在我的网络浏览器中使用后期客户端来使用网络服务并且它可以工作。我只是无法让它与 JUnit 交互。 似乎找不到网址,但我发誓当我去帖子客户端时它可以工作。 我期望的网址是:
http://localhost:8080/propsub/webapi/new/getit
以下是当我尝试在我的 tomcast 服务器停止的情况下运行我的测试用例时得到的警告 + 输出:
Feb 01, 2018 3:30:31 PM org.glassfish.jersey.test.JerseyTest getDefaultTestContainerFactory
WARNING: Found multiple TestContainerFactory service providers, using the first found {0}
Feb 01, 2018 3:30:31 PM org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory$JdkHttpSe rverTestContainer <init>
INFO: Creating JdkHttpServerTestContainer configured at the base URI http://localhost:8080/
Feb 01, 2018 3:30:31 PM org.glassfish.jersey.internal.inject.Providers checkProviderRuntime
WARNING: A provider com.liron.test.TestCases registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.liron.test.TestCases will be ignored.
begin test case
end : 404 x:
这是我的服务器开启时的输出:
Feb 02, 2018 9:49:09 AM org.glassfish.jersey.test.JerseyTest getDefaultTestContainerFactory
WARNING: Found multiple TestContainerFactory service providers, using the first found {0}
Feb 02, 2018 9:49:09 AM org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory$JdkHttpServerTestContainer <init>
INFO: Creating JdkHttpServerTestContainer configured at the base URI http://localhost:8080/
Feb 02, 2018 9:49:10 AM org.glassfish.jersey.internal.inject.Providers checkProviderRuntime
WARNING: A provider com.liron.test.TestCases registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider com.liron.test.TestCases will be ignored.
这是我的 Junit 课程:
package com.liron.test;
import static org.junit.Assert.*;
import java.net.URI;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.liron.pojo.error.ErrorMsg;
public class TestCases extends JerseyTest{
@Override
public Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
return new ResourceConfig(TestCases.class);
}
@BeforeClass
public static void setUpBeforeClass() throws Exception{
}
@Before
public void setUp() throws Exception{
super.setUp();
}
@Override
public URI getBaseUri() {
return URI.create("http://localhost:8080/");
}
@Test
public void testParseException() {
System.out.println("begin test case");
String json = "{\"test\":\"val\".}";
//Response output = target("propsub/webapi/new/getit").request().buildPost(Entity.json("hey")).invoke();
//String output = target("/getit").request().post(Entity.json(json), String.class);
Response output = target("/propsub/webapi/new/getit").request().post(Entity.json("hey"));
//System.out.println(output.getStatus());
// assertEquals("should return status 200", "200", output.getStatus());
//assertNotNull("Should return list", output.getEntity());
String x = output.readEntity(String.class);
System.out.println("end : " + output.getStatus() + " x: " + x);
}
}
这里是api:
package com.liron.webserv;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.liron.pojo.error.ErrorMsg;
import com.google.gson.*;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
* Root resource (exposed at "myresource" path)
*/
@Path("/new")
public class New {
/**
* Method handling HTTP GET requests. The returned object will be sent
* to the client as "text/plain" media type.
*
* @return String that will be returned as a text/plain response.
*/
@Path("/getit")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response getIt(String s) {
System.out.println("begin getit");
return Response.status(Status.NOT_IMPLEMENTED)
.entity(new ErrorMsg("414","Testtesttest"))
.build();
}
}
这里是 ErrorMsg pojo
package com.liron.pojo.error;
public class ErrorMsg{
String errorCode;
String errorMessage;
public ErrorMsg(String code, String msg){
setErrorCode(code);
setErrorMessage(msg);
}
public String getErrorCode(){
return errorCode;
}
public void setErrorCode(String errorCode){
this.errorCode = errorCode;
}
public String getErrorMessage(){
return errorMessage;
}
public void setErrorMessage(String errorMessage){
this.errorMessage = errorMessage;
}
}
应用配置:
package com.liron.config;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/api")
public class ApplicationConfig extends Application {
@Override
public Map<String, Object> getProperties() {
System.out.println("begin getProperties");
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("jersey.config.server.provider.packages", "com.memorynotfound.rs");
System.out.println("end getProperties");
return properties;
}
}
这是我的 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liron.webserv</groupId>
<artifactId>propsub</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>propsub</name>
<build>
<finalName>propsub</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
<systemProperties>
<property>
<name>jersey.config.test.container.port</name>
<value>8080</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>net.sf.jt400</groupId>
<artifactId>jt400</artifactId>
<version>6.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jdk-http</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<jersey.version>2.26</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.liron.webserv</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
编辑更新 1 将我的测试用例更改为包含后的新输出:
public class TestCases extends JerseyTest{
@Override
public Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
//return new ResourceConfig(TestCases.class);
return new ApplicationConfig();
//return new ResourceConfig().register(New.class);
}
输出:
begin getProperties
end getProperties
Feb 02, 2018 2:33:44 PM org.glassfish.jersey.test.JerseyTest getDefaultTestContainerFactory
WARNING: Found multiple TestContainerFactory service providers, using the first found {0}
Feb 02, 2018 2:33:44 PM org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory$JdkHttpServerTestContainer <init>
INFO: Creating JdkHttpServerTestContainer configured at the base URI http://localhost:8080/
【问题讨论】:
-
编辑添加了一些建议的更改
标签: eclipse maven junit jersey jax-rs