gpt4 book ai didi

java - JUnit5 - @CsvSource - 将源代码转换为 POJO

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:00:12 27 4
gpt4 key购买 nike

我一直在学习一些关于 JUnit 测试的在线类(class),并且遇到了一个 @CsvSource 的示例。以下示例效果很好。

@DisplayName("CSV input test")
@ParameterizedTest(name = DISPLAY_NAME_PLACEHOLDER + " - [" + INDEX_PLACEHOLDER + "] " + ARGUMENTS_PLACEHOLDER)
@CsvSource({
"FL, 1, 1",
"OH, 2, 2",
"MI, 3, 3"
})
void csvInputTest(String stateCode, int val1, int val2) {
System.out.println(stateCode + " - " + val1 + " - " + val2);
}

我开始想知道我是否可以使用一些 POJO 来代替无数的参数。所以我创建了带有 setter 和 getter 的 POJO 类,并将我的方法更改为:

void csvInputTest(StateInfo stateInfo) {
System.out.println(stateInfo.getStateCode() + " - " + stateInfo.getVal1() + " - " + stateInfo.getVal2());
}

但这导致:

org.junit.jupiter.api.extension.ParameterResolutionException: Error converting parameter at index 0: No implicit conversion to convert object of type java.lang.String to type StateInfo

我在 @CsvSource 中找不到任何字段来指定任何转换器,所以我的问题是 - 我可以这样做吗?还是我必须坚持N-arguments 方法?

最佳答案

The official documentation解释一下:

To use a custom aggregator, implement the ArgumentsAggregator interface and register it via the @AggregateWith annotation on a compatible parameter in the @ParameterizedTest method. The result of the aggregation will then be provided as an argument for the corresponding parameter when the parameterized test is invoked. Note that an implementation of ArgumentsAggregator must be declared as either a top-level class or as a static nested class.

它会给出:

public class StateInfoAggregator implements ArgumentsAggregator {
@Override
public StateInfo aggregateArguments(ArgumentsAccessor arguments, ParameterContext context) {
return new StateInfo(arguments.getString(0),
arguments.getInteger(1),
arguments.getInteger(1));

}
}

@ParameterizedTest
@CsvSource({
"FL, 1, 1",
"OH, 2, 2",
"MI, 3, 3"
})
void csvInputTest(@AggregateWith(StateInfoAggregator.class) StateInfo stateInfo) {
System.out.println(stateInfo.getStateCode() + " - " + stateInfo.getVal1() + " - " + stateInfo.getVal2());
}

文档还添加了:

If you find yourself repeatedly declaring @AggregateWith(MyTypeAggregator.class) for multiple parameterized test methods across your codebase, you may wish to create a custom composed annotation such as @CsvToMyType that is meta-annotated with @AggregateWith(MyTypeAggregator.class).

它会给出:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AggregateWith(StateInfoAggregator.class)
public @interface CsvToStateInfo {
}

@ParameterizedTest
@CsvSource({
"FL, 1, 1",
"OH, 2, 2",
"MI, 3, 3"
})
void csvInputTest(@CsvToStateInfo StateInfo stateInfo) {
System.out.println(stateInfo.getStateCode() + " - " + stateInfo.getVal1() + " - " + stateInfo.getVal2());
}

关于java - JUnit5 - @CsvSource - 将源代码转换为 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54866818/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com