在docker容器内运行maven集成测试 [英] Running maven integration test inside docker container
问题描述
我正在使用dockerfile-maven插件在运行集成测试之前将我的jar文件移动到docker容器中。但是mvn verify命令会构建映像并运行集成测试,因此测试失败。在运行集成测试之前,有人可以帮助我运行docker镜像。这样我就可以从我的集成测试文件中ping到在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();
}
}
}
测试报告:
[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
来自TestSuite.txt的集成测试结果
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无法访问具有修饰符public的IntegrationTestIT类的成员
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 {
...
测序问题:
如果您的集成测试在 integration-test
阶段运行,您可以强制执行docker插件预整合测试
阶段:
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>
这篇关于在docker容器内运行maven集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!