枚举参数的参数化@EnumSource

  • 使用枚举类作为测试数据。
  • 枚举参数参数化注解 @EnumSource。
  • 必须与 @ParameterizedTest 结合使用。

需要添加@EnumSource注解

测试方法传入枚举类作为参数

package com.mytest;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import java.util.EnumSet;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class EnumTest {
    // 创建一个枚举类
    public enum HogwartsUnit {
        Harry("Harry", 18),
        AD("AD", 19);
        private final String name;
        private final Integer age;
        private HogwartsUnit(String name, Integer age){
            this.name = name;
            this.age = age;
        }
    }

    @ParameterizedTest
    // @EnumSource 注解表示使用枚举类型
    @EnumSource
    // 枚举类作为参数传入测试方法
    void testWithEnumSourceInclude(HogwartsUnit unit) {
        assertTrue(EnumSet.of(HogwartsUnit.Harry, HogwartsUnit.AD).contains(unit));
    }
}

 

通过 names 参数指定枚举值

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import java.util.EnumSet;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class EnumTest {
    // 创建一个枚举类
    public enum HogwartsUnit {
        Harry("Harry", 18),
        AD("AD", 19);
        private final String name;
        private final Integer age;
        private HogwartsUnit(String name, Integer age){
            this.name = name;
            this.age = age;
        }
    }
    @ParameterizedTest
    // @EnumSource 注解表示使用枚举类型, 
    // 通过 names 参数指定使用的枚举值
    @EnumSource(names = {"Harry"})
    // 枚举类作为参数传入测试方法
    void testWithEnumSourceInclude(HogwartsUnit unit) {
        assertTrue(EnumSet.of(HogwartsUnit.Harry, HogwartsUnit.AD).contains(unit));
    }
}

 


 

  • mode 参数指定规则
    • EXCLUDE 代表取反,即指定名称不等于的场景
    • MATCH_ALL 代表通过正则进行匹配
package com.mytest;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import java.util.EnumSet;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.EnumSource.Mode.EXCLUDE;
import static org.junit.jupiter.params.provider.EnumSource.Mode.MATCH_ALL;

public class EnumTest2 {
    // 创建一个枚举类
    public enum HogwartsUnit {
        Harry("Harry", 18),
        AD("AD", 19);
        private final String name;
        private final Integer age;
        private HogwartsUnit(String name, Integer age){
            this.name = name;
            this.age = age;
        }
    }
    @ParameterizedTest
    // @EnumSource 注解表示使用枚举类型,
    // 通过 mode 参数指定规则
    // mode 值为 EXCLUDE 代表取反,即指定名称不为Harry的枚举值
    @EnumSource(mode = EXCLUDE, names = {"Harry"})
    // 枚举类作为参数传入测试方法
    void testWithEnumSourceExclude(HogwartsUnit unit) {
        assertTrue(EnumSet.of(HogwartsUnit.Harry, HogwartsUnit.AD).contains(unit));
    }
    @ParameterizedTest
    // @EnumSource 注解表示使用枚举类型,
    // 通过 mode 参数指定规则
    // mode 值为 EXCLUDE 代表取反,即指定名称不为Harry的枚举值
    @EnumSource(mode = MATCH_ALL, names = {".*ry"})
        // 枚举类作为参数传入测试方法
    void testWithEnumSourceRegex(HogwartsUnit unit) {
        assertTrue(unit.name().endsWith("ry"));    }
}

 

posted @ 2023-08-23 23:15  Mr_sven  阅读(115)  评论(0编辑  收藏  举报