从springboottest注解读取的属性为null,而不是配置的值

jtw3ybtb  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(331)

我有一个简单的测试代码可以从注解中读取属性,但是maven测试执行给出了“org.opentest4j.assertionfailederror:expected:但是was:“有人能帮忙吗?”?我是java和spring的初学者。
这是我的密码:

package org.xyz.mytest;

import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;

@SpringBootTest(properties = "foo=bar")
public class ConcreteTest {

    @Value("${foo}")
    String foo;

    @Test
    public void testT4() {
        Assertions.assertEquals(foo, "bar");
    }
}
uurv41yg

uurv41yg1#

@Value 与以这种方式进行springboottest不兼容。使用testpropertysource注解 @Valid 工作

@SpringBootTest
@TestPropertySource(properties = {
    "foo=bar"   // separated by comma in case there are more properties.
})

相关问题