简体   繁体   English

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

[英]Running maven integration test inside docker container

I am using dockerfile-maven plugin to move my jar file inside docker container before running integration testing.在运行集成测试之前,我正在使用 dockerfile-maven 插件将我的 jar 文件移动到 docker 容器内。 But mvn verify command builds the images and run integration test, as a result test fails.但是 mvn verify 命令构建图像并运行集成测试,结果测试失败。 Can someone help me run the docker images before running integration test.有人可以在运行集成测试之前帮我运行 docker 镜像吗? So that I can ping to service running inside docker container from my integration test file.这样我就可以从我的集成测试文件 ping 到在 docker 容器内运行的服务。

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来自 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" java.lang.IllegalAccessException: Class org.testng.internal.MethodInvocationHelper 无法访问带有修饰符“public”的 IntegrationTestIT 类的成员

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 :如果您的集成测试在integration-test阶段运行,您可以在pre-integration-test阶段强制执行 docker 插件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM