java - 从 Scanner 获取输入时 Maven 挂起

标签 java maven input file-io

我正在设计一个程序,该程序必须使用 mvn test 命令运行并从命令行获取用户输入。当我使用 mvn test 运行程序时,一切正常,直到执行 Scanner.next() ,然后 CLI 挂起,我必须关闭程序。

my test method
    public class AppTest 
{
    @Test
    public void shouldAnswerWithTrue()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Awaiting input");
        String in = sc.next();
        System.out.println("TEST!" + in);
        assertTrue( true );
    }
}

我的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>my-app</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <plugin>
               <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>com.vogella.build.maven.intro.Main</mainClass>
                </configuration>
            </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

是否可以使用 Scanner 类以这种方式处理输入?或者通过这种方式通过maven命令行界面?

最佳答案

据我所知,您不能通过 Maven 使用 Scanner 作为任何代码的一部分。 Maven 是帮助你构建、测试、部署——实际上并不是在运行时帮助你。此外,通常来说,单元测试不应依赖于运行时的输入。

解决方案 1:从命令行直接使用 Java 运行 jUnit 测试
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore AppTest

更多信息here .

解决方案 2:使用系统属性
然后您可以使用 System.getProperty("myVariable");
您可以将其运行为 mvn -Dtest=shouldAnswerWithTrue -DargLine="-myVariable=abc"

查看有关this method here的更多信息.

关于java - 从 Scanner 获取输入时 Maven 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58085781/

相关文章:

java - 如何像在 Notification 中一样展开和折叠 RecyclerView 中的项目

java - 将数字转换为有效加倍

java - 如何在java中画一条线?

ios - iOS模拟器中文输入

java - 如何计算 Java 数组用户输入的字符数?

java - spring找不到我的bean

java - Gwt编译错误: unable to find type

java - 如何让Jenkins使用本地用户的.m2文件夹?

java - 在 Spring Boot 中使用 @autowire 注释抛出 'BeanCreationException'

node.js - JSON 中位置 0 处出现意外标记 u - 将文件中的 JSON 字符串数据解析到 Web 服务器时