使用 junit test 将命令行参数传递给 Spring Boot 应用程序

2023-12-25

我有一个非常基本的 Spring Boot 应用程序,它需要来自命令行的参数,没有它就无法工作。这是代码。

@SpringBootApplication
public class Application implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    @Autowired
    private Reader reader;

    @Autowired
    private Writer writer;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        Assert.notEmpty(args);

        List<> cities = reader.get("Berlin");
         writer.write(cities);
    }
}

这是我的 JUnit 测试类。

@RunWith(SpringRunner.class)
@SpringBootTest
public class CityApplicationTests {

    @Test
    public void contextLoads() {
    }
}

Now, Assert.notEmpty()通过论证的授权。然而,现在,我正在为此编写 JUnit 测试。但是,我收到以下异常引发Assert.

2016-08-25 16:59:38.714 ERROR 9734 --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:801) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:782) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:769) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111) [spring-boot-test-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:46) [spring-boot-test-autoconfigure-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
Caused by: java.lang.IllegalArgumentException: [Assertion failed] - this array must not be empty: it must contain at least 1 element
    at org.springframework.util.Assert.notEmpty(Assert.java:222) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.util.Assert.notEmpty(Assert.java:234) ~[spring-core-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at com.deepakshakya.dev.Application.run(Application.java:33) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
    ... 32 common frames omitted

知道如何传递参数吗?


@SpringBootTest has args参数。你可以在那里传递 cli 参数

see https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html#args-- https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html#args--

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 junit test 将命令行参数传递给 Spring Boot 应用程序 的相关文章

  • Firestore - RecycleView - 图像持有者

    我不知道如何编写图像的支架 我已经设置了 2 个文本 但我不知道图像的支架应该是什么样子 你能帮我告诉我图像的文字应该是什么样子才能正确显示吗 holder artistImage setImageResource model getArt
  • 如何获取 WebElement 的父级[重复]

    这个问题在这里已经有答案了 我试过了 private WebElement getParent final WebElement webElement return webElement findElement By xpath 但我得到
  • Java 服务器-客户端 readLine() 方法

    我有一个客户端类和一个服务器类 如果客户端向服务器发送消息 服务器会将响应发送回客户端 然后客户端将打印它收到的所有消息 例如 如果客户端向服务器发送 A 则服务器将向客户端发送响应 1111 所以我在客户端类中使用 readLine 从服
  • 如何在没有 web.xml 的情况下将 Struts2 添加到 Web 应用程序?

    有人可以帮助我使用 Spring Boot 和 Struts2 进行最小项目设置吗 我已经使用 H2 数据库创建了一个 Spring Boot 应用程序 我还添加了一个h2Configuration类 以便我能够访问数据库localhost
  • Git 无法识别重命名和修改的包文件

    我有一个名为的java文件package old myfile java 我已经通过 git 提交了这个文件 然后我将我的包重命名为new所以我的文件在package new myfile java 我现在想将此文件重命名 和内容更改 提交
  • Java 数组的最大维数

    出于好奇 在 Java 中数组可以有多少维 爪哇language不限制维数 但是JavaVM规范将维度数限制为 255 例如 以下代码将无法编译 class Main public static void main String args
  • 获取给定类文件的目录路径

    我遇到的代码尝试从类本身的 class 文件所在的同一目录中读取一些配置文件 File configFiles new File this getClass getResource getPath listFiles new Filenam
  • 如何将使用消息侦听器接收到的 JMS 消息转换为域对象

    我在用 春季3 1 1 ActiveMQ 5 6 0 我有两个 JMS 应用程序 应用程序 A 使用 JmsTemplate 使用 jmsTemplate convertAndSend msg 发送域对象 App B 使用消息监听器并注册了
  • 计算日期之间的天数差异

    在我的代码中 日期之间的差异是错误的 因为它应该是 38 天而不是 8 天 我该如何修复 package random04diferencadata import java text ParseException import java t
  • 尝试使用等于“是”或“否”的字符串变量重新启动 do-while 循环

    计算行程距离的非常简单的程序 一周前刚刚开始 我有这个循环用于解决真或假问题 但我希望它适用于简单的 是 或 否 我为此分配的字符串是答案 public class Main public static void main String a
  • 如何通过 Inno Setup for NetBeans 使用自定义 .iss 文件

    我将 Inno Setup 5 与 NetBeans 8 一起使用 并且我已经能够创建一个安装程序来安装该应用程序C users username local appname 但是我希望将其安装在C Programfiles 我如何在 Ne
  • 无法捕获 Spring Batch 的 ItemWriter 中的异常

    我正在编写一个 Spring Batch 流程来将数据集从一个系统迁移到另一个系统 在这种情况下 这就像使用RowMapper实现在传递给查询之前从查询构建对象ItemWriter The ItemWriter称为save我的 DAO 上的
  • Linux 上有关 getBounds() 和 setBounds() 的 bug_id=4806603 的解决方法?

    在 Linux 平台上 Frame getBounds 和 Frame setBounds 的工作方式不一致 这在 2003 年就已经有报道了 请参见此处 http bugs java com bugdatabase view bug do
  • vue-test-utils:如何测试 Mounted() 生命周期挂钩中的逻辑(使用 vuex)?

    我正在尝试为 Vue 中的逻辑编写一个单元测试mounted 生命周期钩子 但运气不太好 问题似乎是这样的mounted 使用 vue test utils 安装组件时永远不会被调用mount 这是我要测试的 Vue 组件
  • Java:多线程内的 XA 事务传播

    我如何使用事务管理器 例如Bitronix http docs codehaus org display BTM Home JBoss TS http www jboss org jbosstm or Atomikos http www a
  • Android - 9 补丁

    我正在尝试使用 9 块图片创建一个新的微调器背景 我尝试了很多方法来获得完美的图像 但都失败了 s Here is my 9 patch 当我用Draw 9 patch模拟时 内容看起来不错 但是带有箭头的部分没有显示 或者当它显示时 这部
  • Hibernate 和可序列化实体

    有谁知道是否有一个框架能够从实体类中剥离 Hibernate 集合以使它们可序列化 我查看了 BeanLib 但它似乎只进行实体的深层复制 而不允许我为实体类中的集合类型指定实现映射 BeanLib 目前不适用于 Hibernate 3 5
  • 在android中跟踪FTP上传数据?

    我有一个运行 Android 的 FTP 系统 但我希望能够在上传时跟踪字节 这样我就可以在上传过程中更新进度条 安卓可以实现这个功能吗 现在 我正在使用org apache common net ftp我正在使用的代码如下 另外 我在 A
  • spring boot @Autowired 来自另一个模块的 bean

    我的问题是如何将包添加到组件列表中以扫描 ComponentScan basePackages io swagger com company project like add it here 但该包位于另一个模块中在我的项目中 这是我的项目
  • 在哪里存储 Java 的 .properties 文件?

    The Java教程 http download oracle com javase tutorial essential environment properties htmlon using Properties 讨论如何使用 Prop

随机推荐

  • cd 命令不会更改目录。命令提示符

    我有 Windows 8 并且正在使用命令提示符 它说 C Windows System32 gt 如果我尝试更改目录 它会显示 系统找不到指定的路径 在这种情况下 我输入以下命令 cd desktop 它在我的 Windows 7 计算机
  • Asp.net MVC ModelState.Clear

    谁能给我一个关于 ModelState 在 Asp net MVC 中的作用的简洁定义 或者一个链接 我特别需要知道在什么情况下有必要或需要打电话ModelState Clear 有点开放式结局吧 抱歉 我认为告诉您我实际上在做什么可能会有
  • 是否有与 InlineModelAdmin 等效的 get_form 方法?

    我试图在继承的类中显示不同的字段堆叠内联取决于它是新实例还是现有实例 即添加或更改表单 我知道在继承的类中模型管理员我可以重写 get form 方法来实现此目的 如下所述here https stackoverflow com quest
  • Django:在自定义 URL 后面提供媒体服务

    所以我当然知道通过 Django 提供静态文件会让你直接下地狱 但我对如何使用自定义 url 来使用 Django 掩盖文件的真实位置感到困惑 Django 在通用视图中提供下载服务 https stackoverflow com ques
  • 错误消息指出变量未定义(当变量已定义时)。 Python

    我正在尝试使用函数在 python 中创建一个简单的加密程序 但是遇到一个问题 当我运行程序时 我收到一条错误消息 提示 msgReversed 未定义 该程序的工作原理是首先反转用户消息 然后将字母转移到密钥的值 alphabet abc
  • 使用部分索引元组列表对多索引数据帧进行切片的最佳方法是什么?

    我想使用部分匹配的索引或元组列表对数据帧进行切片 ix foo a 1 foo a 2 foo b 1 foo b 2 foo c 1 foo c 2 df pd DataFrame np ones 6 1 index pd MultiIn
  • 在 xslt 中嵌入标签

    我有这个正在运行的 xslt
  • 打印自己的源代码作为输出的 C/C++ 程序

    维基百科 http en wikipedia org wiki Quine 28computing 29说它叫做 quine 有人给出了下面的代码 char s char s c s c main printf s 34 s 34 main
  • 谷歌云和谷歌云端硬盘之间转移需要花钱吗?

    我有兴趣在 Google 的计算引擎 或 App Engine 上运行一些代码 这些代码将从 Google 云存储读取数据并将其写入 Google Drive 从Google的定价页面来看 在同一地区访问Google云服务不会产生网络传输费
  • 从 JSON 读取时出现异常

    我有这个代码 JSONObject obj try obj new JSONObject readUrl http dleel ps ss txt List
  • Java 中带参数的 XSL 转换

    我有一个 xsl 文件 我需要在其中使用来自外部源的参数 我正在使用 Java 我的代码如下所示 TransformerFactory transformerFactory TransformerFactory newInstance Tr
  • Python:找到最小整数

    我有以下代码 l 1 2 0 0 1 x 100 0 for i in l if i lt x x i print x 该代码应该找到我的列表中的最低值 1 2 但是当我打印 x 时 它发现该值仍然是 100 0 我的代码哪里出错了 要找到
  • 在 iframe 中丢失会话状态,但在弹出窗口中不丢失

    我们正在开发一个网上商店 并使用第三方 UI 处理付款 我们选择在结帐页面内的 iframe 内显示支付 UI 尽管 我们现在意识到 支付解决方案提供商建议使用顶级窗口 现在发生的情况是 在 IE7 IE8 中 支付 UI 在第一次回发 i
  • 如何修复 Android .aar 项目中的“java.lang.NoClassDefFoundError”

    我有一个安卓 aar已构建库 我正在尝试将其与其中一个项目集成 当应用程序尝试打开初始屏幕时 aar我使用改造进行 API 调用的库 我收到以下异常 java lang NoClassDefFoundError 解决失败 of Lokhtt
  • AngularJS Kendo Treeview 未更新

    感谢 Words Like Jared 的回答Angularjs kendo ui 树视图 https stackoverflow com questions 18386992 angularjs kendo ui treeview 我的树
  • 如何使用批处理文件删除换行

    我想使用批处理文件删除文本文件中的换行符 是否可以做 请提供一些帮助 如果您的意思是从文本文件中删除空行 请在批处理文件中尝试以下操作 for f delims tokens x in inputfile txt do echo x gt
  • 无法在 4.2.2 AVD 上使用意图选择器从相机获取图像

    我正在开发应用程序的一部分 该部分允许用户使用意图选择器从相机或图库中选择图像 它在我的 2 2 1 android 手机上工作正常 但是当我在 4 2 2 AVD 上编译它时 当我使用相机时它会返回空指针错误 public void on
  • PHPMailer 仅在 SMTPDebug = true 时发送电子邮件

    我正在使用 PHPmailer 当 mail gt SMTPDebug true 时有效 但是当我删除该行时 它会默默地失败 我说静默失败 因为它没有给出任何错误 但电子邮件似乎没有送达 mail new PHPMailer mail gt
  • string::compare 确定字母顺序可靠吗?

    简而言之 如果输入始终采用相同的大小写 此处为小写 并且字符始终为 ASCII 是否可以使用 string compare 来可靠地确定两个字符串的字母顺序 因此 随着stringA compare stringB 如果结果为0 则它 们相
  • 使用 junit test 将命令行参数传递给 Spring Boot 应用程序

    我有一个非常基本的 Spring Boot 应用程序 它需要来自命令行的参数 没有它就无法工作 这是代码 SpringBootApplication public class Application implements CommandLi