Java 输出\输入在Maven集成测试期间在Junit中不工作

Java 输出\输入在Maven集成测试期间在Junit中不工作,java,eclipse,maven,junit,m2e,Java,Eclipse,Maven,Junit,M2e,我正在使用JUnit和一些我编写、修改或从现有代码库中获得的客户机,将集成测试所需的文件推送到我想要用来进行集成测试的系统中 这是预集成设置步骤。Maven有自己的预集成步骤,但我只是认为它对于我想要\需要\喜欢的东西来说太死板了 @Before public void preIntegration() throws IOException, JSchException, UnsupportedOperationException, IllegalArgumentExceptio

我正在使用JUnit和一些我编写、修改或从现有代码库中获得的客户机,将集成测试所需的文件推送到我想要用来进行集成测试的系统中

这是预集成设置步骤。Maven有自己的预集成步骤,但我只是认为它对于我想要\需要\喜欢的东西来说太死板了

@Before
public void preIntegration() throws IOException, JSchException,
        UnsupportedOperationException, IllegalArgumentException,
        SecurityException, URISyntaxException, JSONException,
        RuntimeException, NoSuchAlgorithmException, KeyStoreException,
        KeyManagementException {
    if (!firstTimeSetupDone) {
        testConfig = new Properties();

        testConfig.load(getClass().getResourceAsStream("/test-config"));

        oozieClient = new OozieClientWithBlocking(
                testConfig.getProperty("oozieUrl"));

        BufferedReader in = new BufferedReader(new InputStreamReader(
                System.in));

        System.out.print("Username: ");
        String username = in.readLine();
        System.out.print("Password: ");
        String password = in.readLine();
        System.out.print("Host: ");
        String host = in.readLine();

        secureContext = new SecureContext(username, host);
        secureContext.setPassword(password);
        secureContext.setTrustAllHosts(true);

        assertNotNull("Linux test file present",
                getClass().getResource("/file.txt"));

        URL resourceUrl = getClass().getResource("/file.txt");

        System.out.println("Sending files to linux.");
        Scp.exec(secureContext, resourceUrl.getPath(),
                "/home/myUsername/file.txt");

        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();

        TrustStrategy acceptAnyCertificate = new TrustStrategy() {
            public boolean isTrusted(final X509Certificate[] chain,
                    String authType) throws CertificateException {
                return true;
            }
        };
        sslContextBuilder.loadTrustMaterial(null, acceptAnyCertificate);
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                sslContextBuilder.build());

        BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
        Credentials credentials = new UsernamePasswordCredentials(username,
                password);
        basicCredentialsProvider.setCredentials(AuthScope.ANY, credentials);

        HttpClientBuilder httpClientFactory = HttpClientBuilder.create();

        httpClientFactory
                .setDefaultCredentialsProvider(basicCredentialsProvider);
        httpClientFactory.setSSLSocketFactory(sslConnectionSocketFactory);

        CloseableHttpClient httpClient = httpClientFactory.build();

        hdfsClient = new HDFSRestClient(httpClient,
                testConfig.getProperty("nameNodeHttps"));

        URL jar = getClass().getResource("/Utilities-1.4.0.jar");

        System.out.println("Sending files to hdfs.");
        hdfsClient
                .create(jar.getPath(),
                        "/user/myUsername/java-action-workflow/lib/Utilities-1.4.0.jar");
        properties = new Properties();
        properties.setProperty("user.name", username);
        System.out.println("Prep done.");
        firstTimeSetupDone = true;
    }
}
在之前的预集成
@步骤中,我要求输入用户名和密码,用于三个客户端中的两个(和)。今年晚些时候,我们公司可能会实现Kerberos,这可能使它变得不必要;这是我发现的唯一能让人们快乐的方法,在这期间。是的,我知道我绕过了SSL的有用性,但我知道我连接的服务器是一个内部网络,它有大量的防火墙,这主要是一个概念证明。如果考虑将其投入生产,我会提出问题

问题是:当我单独运行集成测试Junit类时,一切正常。系统会提示我输入用户名和密码,我可以输入它们。在这之后,类中的长测试用例将被执行,我已经验证了客户机实际上正在工作。没问题。然后,当我运行带有“集成测试”的maven构建时,当需要运行测试时,故障保护插件就会停止。我将在Eclipse中运行这一切:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

标头打印,然后生成似乎处于空闲\等待\停止状态。我甚至试着在Eclipse中查看其他控制台窗口,看看JUnit是否在其他地方抛出System.out.print语句。

我猜您的
测试等待的原因是,它期望
输入。
要发送输入参数,可以执行以下操作

mvn集成测试-Dusername=XXX-Dpassword=YYYY

要使用m2e实现这一点,请在pom.xml中的故障保护插件声明中使用这一点:

<configuration>
                        <systemPropertyVariables>
                        <username>${myUsername}</username>
                        <password>${myPassword}</password>
                        <host>${myHost}</host>
                        </systemPropertyVariables>
            </configuration>

然后,您将使用System.getProperty调用替换System.in调用。(例如System.getProperty(“用户名”))

如果您在
preIntegration()
中明确定义
username
password
host
,而不是以交互方式读取它们(仅用于一次性测试),该怎么办?构建没有停止吗?看看这里:--Maven重定向System.out(和System.in),所以您甚至看不到控制台上的提示。此外,遵循JUnit或maven的理念,为自动化测试提供交互式输入不是一种好的风格。因此,正如萨扬在回答中所建议的那样,您最好使用属性。@Gerold:有人签入凭据的风险。最好让这些凭证尽可能暂时,尽管这是一个很好的测试,以确定读线是否是罪魁祸首。@Stefan:我已经经历过了。记录器很容易理解,但不起作用。另一种方法我无法尝试,因为TerminalView不容易找到。我同意交互输入不好,这也是我喜欢Sajan答案的原因。我喜欢这样,但是我无法在为集成测试创建的Eclipse运行配置中获取VM参数
System.getProperty(“用户名”)
返回空值:(.我在JRE->VM参数下得到了-Dusername=myUsername。在Main选项卡上尝试了同样的方法,效果也一样。@Tavor我不确定这是否有效,因为
maven failsafe plugin:integration test
surefire:test
,依次调用,都不知道
username
password
e> 主机
properties/parameters。我认为应该在这里使用它。我会尝试
-DargLine=-Dusername=…-Dpassword=…-Dhost=…
我最终做了类似的事情。但是我没有使用-DargLine。JUnit运行配置和maven构建运行配置都需要设置为使用这些。
-Dusername=myUsername
-Dpassword=myPassword
-Dhost=myHost