如果我需要给一个值的两个不同的 @Value
在Junit测试类中,有一个注解的字段。这里我有一个布尔字段,它是 @Value
所以我必须写Junit的测试案例,以适应既可以是真也可以是假的情况。
我使用了 @TestPropertySource(properties = "underwriting.skip=true")
.
对于以下领域
@Value("${underwriting.skip}")
protected Boolean skipUnderwriting;
但是为了测试一些代码,我需要在某个点上使值为false。Pls suggest.
你可以为不同的测试类有不同的@TestPropertySource注解。根据你的需求,你可以在一些测试上使用@TestPropertySource(properties="underwriting.skip=true"),而在其他测试上使用@TestPropertySource(properties="underwriting.skip=false")。
(注意:@TestPropertySource属于你的测试类,而不是实际类。)
下面的解决方案对我来说是有效的。我使用ReflectionTestUtils为每个测试用例方法中的属性字段设置值。
ReflectionTestUtils.setField(classToTest, "skipUnderwriting", Boolean.TRUE)。