繁体   English   中英

SpringBoot单元测试中的模拟@Value不起作用

[英]Mock @Value in SpringBoot unit test not working

我正在尝试使用 Mockito 进行一些 junit 测试,以便在 SprinBoot 应用程序中工作。

现在我的服务有一些变量,通过@Value注释从application.properties中填充:

@Component
@Slf4j
public class FeatureFlagService {

  @Autowired
  RestTemplate restTemplate;

  @Value("${url.feature_flags}")
  String URL_FEATURE_FLAGS;

// do stuff
}

我正在尝试像这样使用TestPropertySource来测试它:

@ExtendWith(MockitoExtension.class)
@TestPropertySource(properties = { "${url.feature_flags} = http://endpoint" })
class FeatureFlagServiceTests {

  @Mock
  RestTemplate restTemplate;

  @InjectMocks
  FeatureFlagService featureFlasgService;

  @Test
  void propertyTest(){
    Assertions.assertEquals(featureFlasgService.URL_FEATURE_FLAGS, "http://endpoint");
  }

但是,该属性未被填充并保持null

这方面有很多问题,但我无法拼凑出一个解决方案。 我看到建议@SpringBootTest的解决方案,但随后它似乎想要进行集成测试,启动服务,但由于无法连接到数据库而失败。 所以这不是我要找的。

我还看到了建议我制作PropertySourcesPlaceholderConfigurer bean 的解决方案。 我尝试通过放置:

  @Bean
    public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
    return new PropertySourcesPlaceholderConfigurer();
  }

在我的Application.java中。 但这不起作用/不够。 我不确定我是否应该做不同的事情,或者是否还有更多我不知道的事情。

请指教。

您可以使用@SpringBootTest而无需运行整个应用程序,方法是将它传递给包含 @Value 的@Value但您必须使用 Spring 的扩展@ExtendWith({SpringExtension.class}) ,它包含在@SpringBootTest中,并且使用 Spring 的MockBean而不是@Mock@Autowired用于像这样自动装配 bean:

@SpringBootTest(classes = FeatureFlagService.class)
class FeatureFlagServiceTests {

  @MockBean
  RestTemplate restTemplate;

  @Autowired
  FeatureFlagService featureFlasgService;

  @Test
  void propertyTest(){
    Assertions.assertEquals(featureFlasgService.URL_FEATURE_FLAGS, "http://endpoint");
  }

我建议您尝试这种方法。 只需要稍微重构并向您的FeatureFlagService添加一个包私有构造函数。

FeatureFlagService.java

@Component
@Slf4j
public class FeatureFlagService {

    private final RestTemplate restTemplate;
    private final String URL_FEATURE_FLAGS;

    // package-private constructor. test-only
    FeatureFlagService(RestTemplate restTemplate, @Value("${url.feature_flags}") String flag) {
        this.restTemplate = restTemplate;
        this.URL_FEATURE_FLAGS = flag;
    }

    // do stuff
}

然后准备你的 mocks 和 url,并通过constructor-injection注入它们。

FeatureFlagServiceTests.java

public class FeatureFlagServiceTests {

    private FeatureFlagService featureFlagService;

    @Before
    public void setup() {
        RestTemplate restTemplate = mock(RestTemplate.class);
        // when(restTemplate)...then...
        String URL_FEATURE_FLAGS = "http://endpoint";
        featureFlagService = new FeatureFlagService(restTemplate, URL_FEATURE_FLAGS);
    }

    @Test
    public void propertyTest(){
        Assertions.assertEquals(featureFlasgService.getUrlFeatureFlags(), 
        "http://endpoint");
    }
}

显着的优势是,您的FeatureFlagServiceTests变得非常易于阅读和测试。 您不再需要 Mockito 的神奇注释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM