我正在使用dockerfile-maven插件在运行集成测试之前将我的jar文件移动到docker容器中。但是mvn verify命令会构建映像并运行集成测试,因此测试失败。在运行集成测试之前,有人可以帮助我运行docker镜像。这样我就可以从我的集成测试文件中ping到在docker容器内运行的服务。
以下是我的集成测试文件。
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的集成测试结果
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)
答案 0 :(得分:2)
java.lang.IllegalAccessException:类org.testng.internal.MethodInvocationHelper无法使用修饰符&#34; public&#34;
访问类IntegrationTestIT的成员
您必须公开您的课程才能运行测试:
public class IntegrationTestIT {
...
如果您的集成测试在integration-test
阶段运行,您可以在pre-integration-test
阶段强制执行docker插件:
<execution>
...
<phase>pre-integration-test</phase>
</execution>