如何在Spring Boot Maven项目的测试方法中获取命令行参数
控制台XML标签验证应用解决方案
一、规范项目结构调整
Spring Boot+Maven项目需遵循标准结构,同时修正主类的Spring Boot启动逻辑(使用CommandLineRunner更符合控制台应用规范):
1. Maven构建配置(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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>xml-tag-checker</artifactId> <version>0.0.1-SNAPSHOT</version> <name>xml-tag-checker</name> <description>Console app to validate XML tag structure</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.xmltagchecker.XmlTagChecker</mainClass> </configuration> </plugin> </plugins> </build> </project>
2. 主类重构(XmlTagChecker.java)
实现CommandLineRunner接口,Spring Boot启动后自动执行参数处理逻辑:
package com.example.xmltagchecker; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.ArrayList; import java.util.List; @SpringBootApplication public class XmlTagChecker implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(XmlTagChecker.class, args); } @Override public void run(String... args) throws Exception { if (args.length == 0) { System.out.println("请传入待验证的字符串作为命令行参数"); return; } String input = args[0]; System.out.println(determineXml(input)); } public String determineXml(String input) { int n = input.length(); char[] array = input.toCharArray(); List<String> leftTag = new ArrayList<>(); List<String> rightTag = new ArrayList<>(); int current = 0, start = 0; int leftcount = 0, rightcount = 0; boolean isLeftTag = false, isRightTag = false; while (current < n) { if (current < n && array[current] == '<') { current++; if (array[current] == '/') { isRightTag = true; isLeftTag = false; start = current + 1; } else { if (rightcount > 0) return "Invalid"; isLeftTag = true; isRightTag = false; start = current; } while (current < n && array[current] != '>') { current++; } if (current < n && array[current] == '>') { if (isLeftTag) { leftTag.add(input.substring(start, current)); leftcount++; } else { rightTag.add(input.substring(start, current)); rightcount++; } current++; } } else { while (current < n && array[current] != '<') { current++; } } } if (leftcount != rightcount) return "Invalid"; int size = leftcount; for (int i = 0; i < size; i++) { int right = i; int left = size - 1 - i; if (!rightTag.get(right).equals(leftTag.get(left))) return "Invalid"; } return "Valid"; } }
二、测试类参数传递解决方案
针对测试方法的参数需求,提供两种实用方案:
方案1:单元测试直接指定用例(推荐)
直接构造不同场景的输入参数,覆盖有效、无效、边界情况:
package com.example.xmltagchecker; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class XmlTagCheckerTest { private final XmlTagChecker checker = new XmlTagChecker(); @Test void testValidXmlTags() { String input = "<Design><Code>hello world</Code></Design>"; assertEquals("Valid", checker.determineXml(input)); } @Test void testInvalidUnbalancedTags() { String input = "<Design><Code>hello world</Design>"; assertEquals("Invalid", checker.determineXml(input)); } @Test void testInvalidWrongOrderTags() { String input = "<Design><Code>hello world</Design></Code>"; assertEquals("Invalid", checker.determineXml(input)); } @Test void testInvalidRightTagFirst() { String input = "</Design><Design>hello</Design>"; assertEquals("Invalid", checker.determineXml(input)); } }
方案2:从命令行传递参数给测试
通过JVM参数传递测试输入,模拟实际运行场景:
package com.example.xmltagchecker; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class XmlTagCheckerCommandLineTest { private final XmlTagChecker checker = new XmlTagChecker(); @Test void testWithCommandLineArg() { // 读取JVM参数,运行测试时需添加:-Dtest.input="<Design><Code>hello</Code></Design>" String input = System.getProperty("test.input"); assertNotNull(input, "请通过JVM参数-Dtest.input传入测试字符串"); assertEquals("Valid", checker.determineXml(input)); } }
运行该测试的Maven命令:
mvn test -Dtest.input="<Design><Code>hello world</Code></Design>"
三、控制台运行验证
打包并执行应用:
# 打包应用 mvn clean package # 运行jar包并传入参数 java -jar target/xml-tag-checker-0.0.1-SNAPSHOT.jar "<Design><Code>hello world</Code></Design>"
预期输出:
Valid
内容的提问来源于stack exchange,提问作者Vaibhav
相关产品推荐
相关产品推荐

