使用参数化测试返回方法应该没有参数

Cha*_*les 5 java junit parameterized-tests

我有一个问题,我想通过测试方法来使用GET方法和POST发送请求。我使用了参数化,但我得到信息 java.lang.Exception: Method simpleMessage should have no参量

  @Test
    @ParameterizedTest
    @ValueSource(strings = {"true", "false"})
    public void simpleMessage (boolean isPost) {
        verifyIdOdpEqual(isPost,1243, "message");
    }
Run Code Online (Sandbox Code Playgroud)

小智 16

我不太明白你想要实现什么,但你的代码几乎没有问题:

  1. 由于存在@ParameterizedTest@ValueSource我假设您使用 JUnit 5。同时看起来您使用 JUnit 4 的注释标记了您的方法(因为只有在这种情况下,您引用的文本才会出现异常)。
  2. @Test当方法用 注释时是多余的@ParametrizedTest

您有 2 个选项来解决上述所有问题:

  1. 如果您想使用 junit5,那么您需要删除@Test注释并确保您的测试由支持 JUnit 5 的运行器启动(更多信息)。

    例子:

    package test;
    
    import org.junit.jupiter.params.ParameterizedTest;
    import org.junit.jupiter.params.provider.ValueSource;
    
    public class TestTest {
    
        @ParameterizedTest
        @ValueSource(booleans =  {true, false})
        public void test(boolean data) {
            System.out.println(data);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果您想使用 JUnit 4,那么您需要删除注释@ParameterizedTest@ValueSource重写测试以使用参数化运行程序(更多信息)。