問題描述
在運行集成測試之前,我正在使用 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("http://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("http://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>
這篇關于在 docker 容器中運行 maven 集成測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!