Json 如何测试调用REST调用的EJB服务类?

Json 如何测试调用REST调用的EJB服务类?,json,jersey,testng,resteasy,wildfly-8,Json,Jersey,Testng,Resteasy,Wildfly 8,我知道一些开发人员说调用请求web资源的EJB方法不是单元测试。但是,请不要在这篇文章中争论这个问题!我认为做这件事是值得的 我的测试:testng类-->EJB方法-->rest资源 设置: Wildfly 8.1 TestNG 6.8.8 jboss-jaxrs-api_1.1_规范,1.0.1.Final 这是我的测试课 import org.testng.Assert; import org.testng.annotations.DataProvider; import org.te

我知道一些开发人员说调用请求web资源的EJB方法不是单元测试。但是,请不要在这篇文章中争论这个问题!我认为做这件事是值得的

我的测试:testng类-->EJB方法-->rest资源

设置:

  • Wildfly 8.1
  • TestNG 6.8.8
  • jboss-jaxrs-api_1.1_规范,1.0.1.Final
这是我的测试课

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.doe.webapp.model.general.geoinfo.GeoInfo;

public class GeoIPBeanTest {

    @DataProvider(name = "ipAdresses")
    public static Object[][] primeNumbers() {
        return new Object[][] { 
                { "127.0.0.1", true }, // localhost
                { "80.218.114.61", true } }; // real IP
    }   

    @Test(dataProvider = "ipAdresses")
    public void getGeoInfoByIp(String ipAddress, boolean isExpectedTrue) {
        GeoIPBean geoIpBean = new GeoIPBean();
        GeoInfo geoInfo = null;
        try {
            geoInfo = geoIpBean.getGeoInfoByIp(ipAddress);
        } catch (Exception ex) {
            Assert.fail(ex.getLocalizedMessage());
        }
    }
}
这是我正在测试的班级

import javax.ejb.Singleton;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import com.doe.webapp.model.general.geoinfo.GeoInfo;

@Singleton
public class GeoIPBean {

private static final String IPV4_PATTERN = "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

Map<String, GeoInfo> geoInfoCache = new HashMap<String, GeoInfo>();
// Service Description is here http://freegeoip.net/
static final String GEO_SERICE_URL = "http://freegeoip.net/";
static final String FORMAT = "json";

public GeoInfo getGeoInfoByIp(String ipAddress) {
    if(!isValidIp(ipAddress)){
        //TODO log invalid IP as warning
        return null;
    }

    GeoInfo geoInfo = geoInfoCache.get(ipAddress);
    if (geoInfo == null) {

        Client client = ClientBuilder.newClient();
        // Invoke the service.
        WebTarget webTarget = client.target(GEO_SERICE_URL + FORMAT + "/"
                + ipAddress);
        //geoInfo 
        Builder builder = webTarget.request(MediaType.APPLICATION_JSON);
        geoInfo = builder.get(GeoInfo.class);
    }
    return geoInfo;
}

public static boolean isValidIp(String ipAddress) {
    if(ipAddress == null)
        return false;
    Pattern pattern = Pattern.compile(IPV4_PATTERN);
    Matcher matcher = pattern.matcher(ipAddress);
    return matcher.matches();
}
}
EJB中的这一行返回错误

java.lang.AssertionError: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder
    at org.testng.Assert.fail(Assert.java:94)
    at com.doe.webapp.service.general.geoinfo.GeoIPBeanTest.getGeoInfoByIp(GeoIPBeanTest.java:25)
我首先认为这是因为我用提供的范围注释了wildfly库

<!-- JBOSS JAX REST 2.0 FRAMEWORK -->
    <dependency>
        <groupId>org.jboss.spec.javax.ws.rs</groupId>
        <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <!-- RS client library -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
    </dependency>

org.jboss.spec.javax.ws.rs
jboss-jaxrs-api_1.1_规范
1.0.1.最终版本
javax.ws.rs
javax.ws.rs-api
2.0.1
。。。但是Wildfly使用的是RestEasy而不是Jersey。然后我补充说

<dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-server</artifactId>
   <version>1.18.1</version>
</dependency>

泽西岛
泽西服务器
1.18.1

。。。但是也没有帮助。

Wildfly
中,他们一直使用
RESTEasy
作为默认的rest提供者。它,

RESTEasy与JBoss/Wildfly捆绑在一起,并按照JavaEE6的要求完全集成

因此,在使用
Jboss/Wildfly
之前,必须删除
RESTEasy
依赖项。尽管我还没有做到这一点,但有很多资源可以指导我做到这一点。()

或者,也可以使用
RESTEasy
代替
Jersey

<dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-server</artifactId>
   <version>1.18.1</version>
</dependency>