Junit @value 注释字段的用法Java

问题描述 投票:0回答:1

如果我需要给一个值的两个不同的 @Value 在Junit测试类中,有一个注解的字段。这里我有一个布尔字段,它是 @Value 所以我必须写Junit的测试案例,以适应既可以是真也可以是假的情况。

我使用了 @TestPropertySource(properties = "underwriting.skip=true").

对于以下领域

@Value("${underwriting.skip}")
protected Boolean skipUnderwriting;

但是为了测试一些代码,我需要在某个点上使值为false。Pls suggest.

junit4
1个回答
0
投票

你可以为不同的测试类有不同的@TestPropertySource注解。根据你的需求,你可以在一些测试上使用@TestPropertySource(properties="underwriting.skip=true"),而在其他测试上使用@TestPropertySource(properties="underwriting.skip=false")。

(注意:@TestPropertySource属于你的测试类,而不是实际类。)


0
投票

下面的解决方案对我来说是有效的。我使用ReflectionTestUtils为每个测试用例方法中的属性字段设置值。

ReflectionTestUtils.setField(classToTest, "skipUnderwriting", Boolean.TRUE)。

© www.soinside.com 2019 - 2024. All rights reserved.