【问题标题】:how to test springBoot main() method? [duplicate]如何测试spring Boot main()方法? [复制]
【发布时间】:2019-06-14 19:31:35
【问题描述】:
@RunWith(SpringRunner.class)
@SpringBootTest
public class BankMainAppTest {

    /**
     * Test App load without throwing an exception.
     */
    @Test
    public void contextLoads() {
    }//pass

    @Test
    public void applicationStarts() {
        BankMainApp.main(new String[] {});
    }//fail throw exception given below..
}

java.lang.IllegalArgumentException:无法实例化工厂类: org.springframework.boot.env.EnvironmentPostProcessor

【问题讨论】:

  • 您为什么要这样做?如果应用程序运行,可以说它的 main 方法有效
  • @Stultuske 根据发布的代码,可能是为了覆盖(我同意这不是公平的理由)。

标签: java spring-boot


【解决方案1】:

要么用@SpringBootTest注释测试类并声明一个空的测试方法,要么不注释测试类但在测试方法中调用main()方法。
但不要两者都做。
在这里您执行此操作,它会启动一个容器(在测试执行之前),然后启动另一个容器(在方法测试中)。

注意,如果你不需要断言,因为测试只是为了覆盖率,使用的成语是第二种方式:

public class BankMainAppIT {
   @Test
   public void main() {
      BankMainApp.main(new String[] {});
   }
}

【讨论】:

    猜你喜欢
    • 2018-08-26
    • 2021-12-13
    • 2021-12-26
    • 2018-06-27
    • 2017-11-02
    • 2016-11-06
    • 2020-08-05
    • 1970-01-01
    • 2020-04-27
    相关资源
    最近更新 更多