Maven 在从扫描仪获取输入时挂起



我正在设计一个必须使用 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 );
}
}

我的绒球.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

更多信息在这里。

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

在此处查看有关此方法的更多信息。

最新更新