Java 在docker容器中运行maven集成测试
在运行集成测试之前,我使用dockerfile maven插件将jar文件移动到docker容器中。但是mvnverify命令构建映像并运行集成测试,结果测试失败。在运行集成测试之前,有人能帮我运行docker映像吗。这样我就可以从集成测试文件ping到docker容器中运行的服务Java 在docker容器中运行maven集成测试,java,maven,dockerfile,Java,Maven,Dockerfile,在运行集成测试之前,我使用dockerfile maven插件将jar文件移动到docker容器中。但是mvnverify命令构建映像并运行集成测试,结果测试失败。在运行集成测试之前,有人能帮我运行docker映像吗。这样我就可以从集成测试文件ping到docker容器中运行的服务 import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.SQLExc
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();
}
}
}
下面是我的集成测试文件
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
[INFO]成功构建rohitbarnwal7/presto_日志更新:0.0.1
[信息]maven故障保护插件:2.5:集成测试(默认)@plugin
[信息]故障保护报告目录:/Users/rohit/workspace/presto plugins/target/Failsafe报告
-------------------------------------------------------
T T S T S
-------------------------------------------------------
运行测试套件
测试运行:1,失败:1,错误:0,跳过:0,运行时间:0.833秒辅助功能错误消息:
java.lang.IllegalacessException:Class org.testng.internal.MethodInvocationHelper无法访问修饰符为“public”的类IntegrationTestIT的成员
必须公开类才能运行测试:
public class IntegrationTestIT {
...
排序问题:
如果您的集成测试在集成测试阶段运行,您可以在集成测试前阶段强制执行docker插件:
<execution>
...
<phase>pre-integration-test</phase>
</execution>
...
预集成测试
第一个建议是升级使用过的maven failsafe插件版本……我也尝试过,但由于@khmarbaise的建议,我将其降级。我一直在寻找解决方案,但没有取得太大的成功。您是否有关于Github或Bitbucket等的示例项目?它位于私有存储库中。我可以共享错误日志和其他必需的详细信息。没有帮助。测试运行:1,失败:1,错误:0,跳过:0,所用时间:1.558秒错误已解决,但由于测试未通过,docker在生成完成后未运行。测试用例期望在映射到主机8889的docker端口8889上运行presto服务器,因此引发此错误:原因:java.io.UncheckedIOException:java.net.ConnectException:未能连接到本地主机/0:0:0:0:0:1:8889