【问题标题】:SpringBoot - Create empty test class for demoSpring Boot - 为演示创建空测试类
【发布时间】:2021-11-20 06:00:30
【问题描述】:

如何创建一个空的测试类,如果运行mvn test,它应该返回Tests run: 0, Failures: 0, Errors: 0, Skipped: 0?现在没有任何测试mvn 报告no tests to run。我正在开发一个演示 jenkins 管道,脚本希望所有项目都返回 Tests run: 0 or more

[INFO] --- maven-surefire-plugin:2.4.2:test (default-test) @ demo-web ---
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.535 s
[INFO] Finished at: 2021-09-27T09:04:22Z
[INFO] ------------------------------------------------------------------------
[Pipeline] script

我尝试创建一个测试类,但它返回 1 个测试结果。我想要零测试结果。

BanksMvcControllerTest.java

package com.WebDemo.Controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.webDemo.WebApplication;

import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebApplication.class)
@WebAppConfiguration
public class BanksMvcControllerTest {

    @Test
    public void contextLoads() {
    }
}

【问题讨论】:

  • afaik 你不能。您要么有 1 个运行的测试,要么有 1 个被忽略的测试。所以不,你不能将所有的计数器都设置为 0。
  • 哦,好吧。我期待从 asnwer stackoverflow.com/a/69334020/13410618 得到这样的东西
  • 不。所以要么写一个测试,要么跳过它,但它会导致在一个地方输出 1。如果什么都没有,您会收到您现在收到的消息(不确定这是否可配置)。但是为什么不只进行一次测试,它会遵守你的 CI 规则......

标签: java spring spring-boot junit


【解决方案1】:

您可以使用 @Ignore(对于 JUnit 4)或 @Disabled(对于 JUnit 5)来注释测试

【讨论】:

  • 我在尝试@ignore 时收到错误java.lang.Exception: No runnable methods。能否请您显示如何忽略的代码?
  • @John hmm 我认为这会起作用,但如果你使用它,你似乎需要至少 1 次测试才能真正被忽略。
【解决方案2】:

你能试试下面的代码吗:

@Ignore
@Test
public void contextLoads() {
}

或者:

@Ignore
public class BanksMvcControllerTest

【讨论】:

  • 厌倦了。我得到错误 java.lang.Exception: No runnable methods
  • 你能在课前删除那个公众吗?或使其抽象
  • 我收到以下错误java.lang.Exception: Method contextLoads() should be public
  • @RunWith(SpringRunner.class) 试试这个也应该可以
猜你喜欢
  • 1970-01-01
  • 2021-08-01
  • 2020-04-06
  • 2020-05-29
  • 2020-04-19
  • 2018-06-10
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
相关资源
最近更新 更多