assertEquals不正确地评估了Junit参数化测试。
创始人
2024-09-20 15:01:45
0

在Junit中进行参数化测试时,可以使用@ParameterizedTest注解和@MethodSource注解。这样可以将不同的测试参数传递给测试方法,并使用assertEquals来进行断言。

解决方法如下所示:

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ParameterizedTestExample {

    static Stream testData() {
        return Stream.of(
                Arguments.of(2, 1, 3),
                Arguments.of(5, 5, 10),
                Arguments.of(10, -5, 5)
        );
    }

    @ParameterizedTest
    @MethodSource("testData")
    public void testAddition(int num1, int num2, int expectedResult) {
        int result = num1 + num2;
        assertEquals(expectedResult, result);
    }
}

在上面的示例中,使用@ParameterizedTest注解来标记参数化测试方法,并使用@MethodSource注解来指定数据源方法。testData方法返回一个Stream,其中包含要测试的参数组合。然后,在测试方法中,可以使用assertEquals来断言预期结果和实际结果是否相等。

这样,每次运行测试时,将使用不同的参数组合进行测试,并且可以正确地评估assertEquals

相关内容

热门资讯

透视有挂!红龙poker透视工... 透视有挂!红龙poker透视工具,wpk透视辅助靠谱吗,热点讨论(有挂教程),您好,wpk透视辅助靠...
透视新版!wepoker辅助透... 透视新版!wepoker辅助透视软件,拱趴大菠萝有挂吗,软件教程(原先存在有挂);wepoker辅助...
透视教学!wepoker辅助器... 透视教学!wepoker辅助器怎么用,aapoker透视脚本下载,玩家实测(有挂规律)1、每个玩家都...
透视ai代打!wepoker底... WePoke高级策略深度解析‌;透视ai代打!wepoker底牌透视脚本下载,wepoker好友房开...
透视透视!aapoker透视脚... 透视透视!aapoker透视脚本,aapoker辅助插件工具,重要通知(有挂教学)是一款可以让一直输...
透视好友房!wepoker辅助... 《wepoker辅助透视软件软件透明挂》是一款多人竞技的wepoker辅助透视软件辅助透视游戏,你将...
透视app!wepoker透视... 透视app!wepoker透视脚本下载,hhpoker辅助下载,一分钟了解(有挂规律)1.wepok...
透视有挂!aapoker辅助工... 透视有挂!aapoker辅助工具存在吗,wepoker透视最简单三个步骤,普及教程(原生存在有挂);...
透视游戏!wpk有没有脚本,智... 透视游戏!wpk有没有脚本,智星德州菠萝插件官网,玩家攻略推荐(有挂介绍)1、在智星德州菠萝插件官网...
透视免费!红龙poker透视,... 1、透视免费!红龙poker透视,wepoker怎么看牌型,教你攻略(果然是有挂);详细教程。2、红...