Java EE 7便携式集成和单元测试解决方案 [英] Java EE 7 Portable solution for Integration and Unit testing

查看:141
本文介绍了Java EE 7便携式集成和单元测试解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一个可移植的解决方案来测试我的Java EE 7应用程序。在测试EJB及其注入时,这一点尤其棘手。例如:

I am trying to find a portable solution to test my Java EE 7 application. It is especially tricky when to test the EJB and their injections. For example:

@org.junit.Test
    public void testIsValidCredentials() throws Exception {
        System.out.println("isValidCredentials");
        String username = "";
        String password = "";

        Map properties = new HashMap();
        properties.put(EJBContainer.MODULES, new File[] {new File("target/classes")});

        EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
        AuthenticatorLocal instance = (AuthenticatorLocal) container.getContext().lookup("java:global/classes/Authenticator");
        boolean expResult = false;
        boolean result = instance.isValidCredentials(username, password);
        assertEquals(expResult, result);
        container.close();
    }

当我运行测试时,我会得到:

When I run test I will get:


没有可用的EJBContainer提供商

No EJBContainer provider available

我也尝试使用选项 properties.put(EJBContainer.PROVIDER,),但没有成功。有一些文档可用于Glassfish,但对于Wildfly来说它真的很差。

I also tried to use the option properties.put(EJBContainer.PROVIDER, ""), but no success. There is some documentation available for Glassfish, but for Wildfly it is really poor.

我也听说过arquillian,但我只看到Alpha套餐,这看起来并不安全。有没有人知道(集成)测试的可移植解决方案?

Also I have heard of arquillian, but I only see Alpha packages, which doesn't seem production safe. Does anyone know a portable solution for (integration) testing?

推荐答案

这是我用来执行JUnit测试的一种相当标准的方法在Java EE环境中。

Here is a fairly standard approach that I use to execute JUnit tests in a Java EE environment.

基本思路是:


  1. 用测试编写一个简单的测试应用程序Servlet和您要测试的任何EJB

  2. 使用JavaSE JUnit测试套件启动您的应用服务器,并将GET请求驱动到测试servlet

  3. 将您的实际测试逻辑放在测试servlet中,这样您的测试逻辑将在Java EE环境中执行

这是一个例子可能是这样的:

Here is an illustration of what that might look like:

使用带有基本测试存根的JUnit SE套件来驱动对应用程序的GET请求:

@BeforeClass
public static void oneTimeSetUp() {
    // start my app server and make sure apps are installed
}

@AfterClass
public static void oneTimeTearDown() {
    // shut down app server
}

@Test
public void testApplicationAvailable() {
    runTestInServlet("testApplicationAvailable");
}

@Test
public void testThatFails() {
    runTestInServlet("testThatFails");
}

private void runTestInServlet(String testName) {
    // Construct the URL, note that the name of the test is passed in
    // as the value of the "test" parameter.  This will get read from
    // the servlet's doGet method
    URL url = new URL("http://localhost:9080/myAppName/testServletName?test=" + test);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    try{
        con.setRequestMethod("GET");
        InputStream is = con.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        // use the BufferedReader to validate response from test servlet
        // i.e. search for some "successful test" token
    } finally {
        con.disconnect();
    }
}

在测试servlet中使用相应的方法在Java EE环境中执行的实际试验:

@WebServlet(name = "TestServlet", urlPatterns = { "/testServletName" })
public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException {
        String test = request.getParameter("test");
        PrintWriter out = response.getWriter();

        try {
            System.out.println("-----> " + test + " starting");
            // Use reflection to branch out and invoke the method found in the GET method's "test" parameter
            getClass().getMethod(test, PrintWriter.class).invoke(this, response.getWriter());
            System.out.println("<----- " + test + " successful");
        } catch (Throwable x) {
            System.out.println("<----- " + test + " failed:");
        } finally {
            out.flush();
            out.close();
        }
    }

    public void testApplicationAvailable(PrintWriter pw) throws Exception {
        System.out.prinln("Application and test servlet available");
    }

    public void testThatFails(PrintWriter pw) throws Exception {
        throw new Exception("This test will fail");
    }
}

您可以使用第三方库来实现此目的更简单一点,但这是基本的想法。我在这里展示的例子都可以用标准的Java库(当然还有Java EE环境)来完成。这里唯一特定于你运行的app服务器的东西是服务器启动和停止逻辑。

There are third party libs you can use to make this a little more simple, but this is the basic idea. The examples I showed here can all be done with standard Java libraries (and a Java EE environment, of course). The only stuff in here that is specific to what app server you are running on is the server start and stop logic.

另外,我听说过Arquillian的好东西,但最后我听说它只支持少数几个Java EE应用服务器,这会破坏你的可移植性要求。

Also, I've heard good things about Arquillian, but last I heard it's only supported for a select few Java EE app servers, which would break your portability requirement.

这篇关于Java EE 7便携式集成和单元测试解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆