Jax RS&泽西与朱尼特;测试用例被忽略

时间:2018-02-02 17:15:36

标签: eclipse maven junit jersey jax-rs

我无法让泽西岛与Junit合作。有人可以帮忙吗?我不知道还能做什么。

我正在使用我的tomcat服务器,当我运行项目时,我右键单击该文件夹并选择"在服务器上运行。"完成此操作后,我可以在我的Web浏览器中使用post-client来使用Web服务并且它可以正常工作。我不能让它与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/

1 个答案:

答案 0 :(得分:0)

return new ResourceConfig(TestCases.class);

这没有任何作用。您应该使用ResourceConfig注册资源类。该测试未检测到已注册的任何资源类,因此它不会运行。

如果你想使用ApplicationConfig,那就回去吧,因为它正在使用属性扫描软件包

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

顺便说一句,只需在您计划测试的资源上注册即可。例如

@Override
public Application configure() {
   return new ResourceConfig()
       .register(ResourceUnderTest.class);
}

这样做的一个好处是,此测试的成功与其他任何正确初始化资源无关。

除此之外,通常在Jersey应用程序中,您应该扩展Application,而不是扩展ResourceConfig。而不是使用getProperties,您只需拨打property中的ResourceConfig或只需致电packages即可设置包裹扫描。

@ApplicationPath("/")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        packages("you.package.to.scan");
    }
}

另一方面,如果您使用这种Java风格的配置,则不需要web.xml