在 docker 容器中运行 maven 集成测试

2022-01-14 00:00:00 dockerfile java maven

在运行集成测试之前,我正在使用 dockerfile-maven 插件将我的 jar 文件移动到 docker 容器中.但是 mvn verify 命令构建图像并运行集成测试,结果测试失败.有人可以在运行集成测试之前帮我运行 docker 映像吗?这样我就可以从我的集成测试文件 ping 到在 docker 容器内运行的服务.

以下是我的集成测试文件.

import java.sql.Connection;导入 java.sql.DriverManager;导入 java.sql.Statement;导入java.sql.SQLException;导入java.sql.ResultSet;导入 java.io.IOException;导入java.io.File;导入 java.util.Scanner;导入静态 org.hamcrest.MatcherAssert.*;导入静态 org.junit.matchers.JUnitMatchers.*;导入 org.junit.Assert.*;导入 com.facebook.presto.jdbc.PrestoDriver;导入 io.airlift.log.Logger;导入 org.testng.annotations.Test;类IntegrationTestIT {@测试公共无效 checkForQueryInFile() {System.out.println("这个测试方法应该运行");字符串 url = "jdbc:presto://localhost:8889/jmx/default";语句 stmt = null;尝试 {连接连接 = DriverManager.getConnection(url, "jumbo", null);stmt = connection.createStatement();字符串文件路径 = "";String sql_string = "显示模式";结果集 rs = stmt.executeQuery(sql_string);文件夹 = new File("//jars");//将此移动到常量类文件[] 文件 = 文件夹.listFiles();对于(文件文件:文件){if (file.isFile()) {file_path = file.getAbsolutePath();}}文件 log_file = new File(file_path);最终字符串扫描仪=新扫描仪(log_file).useDelimiter(\Z").next();;assertThat(扫描仪,包含字符串(sql_string));rs.close();stmt.close();连接.close();} 捕捉(IOException 异常){异常.printStackTrace();} 捕捉(SQLException sqlException){sqlException.printStackTrace();}}}

测试报告:

[INFO] 成功构建 rohitbarnwal7/presto_log_updated:0.0.1[信息] maven-failsafe-plugin:2.5:integration-test (默认) @plugin[INFO] 故障安全报告目录:/Users/rohit/workspace/presto plugins/target/failsafe-reports-------------------------------------------------------T E S T S-------------------------------------------------------运行测试套件测试运行:1,失败:1,错误:0,跳过:0,经过的时间:0.833 秒 <<<<失败!结果 :失败的测试:checkForQueryInFile(IntegrationTestIT)测试运行:1,失败:1,错误:0,跳过:0

TestSuite.txt 中的集成测试结果

测试运行:1,失败:1,错误:0,跳过:0,经过时间:0.559 秒 <<<<失败!checkForQueryInFile(IntegrationTestIT) 经过时间:0.015 秒 <<<<失败!java.lang.IllegalAccessException:类 org.testng.internal.MethodInvocationHelper 无法访问带有修饰符public"的类 IntegrationTestIT 的成员在 sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)在 java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)在 java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)在 java.lang.reflect.Method.invoke(Method.java:491)在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)在 org.testng.internal.Invoker.invokeMethod(Invoker.java:639)在 org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)在 org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)在 org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)在 org.testng.TestRunner.privateRun(TestRunner.java:774)在 org.testng.TestRunner.run(TestRunner.java:624)在 org.testng.SuiteRunner.runTest(SuiteRunner.java:359)在 org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)在 org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)在 org.testng.SuiteRunner.run(SuiteRunner.java:261)在 org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)在 org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)在 org.testng.TestNG.runSuitesSequentially(TestNG.java:1191)在 org.testng.TestNG.runSuitesLocally(TestNG.java:1116)在 org.testng.TestNG.run(TestNG.java:1024)在 org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:62)在 org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:141)在 org.apache.maven.surefire.Surefire.run(Surefire.java:180)在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在 java.lang.reflect.Method.invoke(Method.java:498)在 org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)在 org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)

解决方案

辅助功能错误信息:

<块引用>

java.lang.IllegalAccessException:类 org.testng.internal.MethodInvocationHelper 无法访问带有修饰符public"的 IntegrationTestIT 类的成员

你必须公开你的班级才能运行测试:

公共类 IntegrationTestIT {...

测序问题:

如果您的集成测试在 integration-test 阶段运行,您可以在 pre-integration-test 阶段强制执行 docker 插件:

<执行>...<阶段>预集成测试</阶段></执行>

I am using dockerfile-maven plugin to move my jar file inside docker container before running integration testing. But mvn verify command builds the images and run integration test, as a result test fails. Can someone help me run the docker images before running integration test. So that I can ping to service running inside docker container from my integration test file.

Below is my integration test file.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
import static org.hamcrest.MatcherAssert.*;
import static org.junit.matchers.JUnitMatchers.*;
import org.junit.Assert.*;
import com.facebook.presto.jdbc.PrestoDriver;
import io.airlift.log.Logger;
import org.testng.annotations.Test;

class IntegrationTestIT {

@Test
public void checkForQueryInFile() {

    System.out.println("This test method should be run");
    String url = "jdbc:presto://localhost:8889/jmx/default";

    Statement stmt = null;
    try {
        Connection connection = DriverManager.getConnection(url, "jumbo", null);

        stmt = connection.createStatement();
        String file_path = "";
        String sql_string = "show schemas";

        ResultSet rs = stmt.executeQuery(sql_string);
        File folder = new File("//jars");
        // Move this to constant class
        File[] files = folder.listFiles(); 

        for (File file:files) {
            if (file.isFile()) {
                file_path = file.getAbsolutePath();
            }
        }
        File log_file = new File(file_path);
        final String scanner = new Scanner(log_file).useDelimiter("\Z").next();;

        assertThat(scanner, containsString(sql_string));

    rs.close();
    stmt.close();
    connection.close();

    } catch (IOException exception) {
        exception.printStackTrace();
    } catch(SQLException sqlException) {
        sqlException.printStackTrace();
    }
}
}

Test Report:

[INFO] Successfully built rohitbarnwal7/presto_log_updated:0.0.1
[INFO] maven-failsafe-plugin:2.5:integration-test (default) @plugin
[INFO] Failsafe report directory: /Users/rohit/workspace/presto plugins/target/failsafe-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.833 sec <<< FAILURE!

Results :

Failed tests:
  checkForQueryInFile(IntegrationTestIT)

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

Integration Test result from TestSuite.txt

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.559 sec <<< FAILURE!
checkForQueryInFile(IntegrationTestIT)  Time elapsed: 0.015 sec  <<< FAILURE!
java.lang.IllegalAccessException: Class org.testng.internal.MethodInvocationHelper can not access a member of class IntegrationTestIT with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1191)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
    at org.testng.TestNG.run(TestNG.java:1024)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:62)
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:141)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)

解决方案

Accessibility error message :

java.lang.IllegalAccessException: Class org.testng.internal.MethodInvocationHelper can not access a member of class IntegrationTestIT with modifiers "public"

You have to make your class public to run tests :

public class IntegrationTestIT {
...

Sequencing issue :

If your integration tests are running in the integration-test phase, you can force the execution of the docker plugin during the pre-integration-test phase :

<execution>
    ...
    <phase>pre-integration-test</phase>
</execution>

相关文章