【问题标题】:How to test an EJB service class that invokes a REST call?如何测试调用 REST 调用的 EJB 服务类?
【发布时间】:2014-10-04 14:00:23
【问题描述】:

我知道一些开发人员说调用请求 Web 资源的 EJB 方法不是单元测试。但是,请不要在此线程中争论这一点!我认为这样做是值得的。

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

设置:

  • 野蝇 8.1
  • TestNG 6.8.8
  • jboss-jaxrs-api_1.1_spec,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 时,它可以工作。它不适用于 testNG 案例。

Client client = ClientBuilder.newClient();

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>

... 但是 Wildfly 使用的是 RestEasy 而不是 Jersey。然后我加了...

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

...但也没有帮助。

【问题讨论】:

    标签: json jersey testng resteasy wildfly-8


    【解决方案1】:

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

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

    因此,在将 JerseyJboss/Wildfly 一起使用之前,您必须删除 RESTEasy 依赖项。尽管我没有这样做,但可以有很多资源可以指导这样做。 (Checktheselinks.)

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

    【讨论】:

      猜你喜欢
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 2020-03-17
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多