【问题标题】:Testing @ConfigurationProperties with Mockito使用 Mockito 测试 @ConfigurationProperties
【发布时间】:2019-08-01 07:46:26
【问题描述】:

我有配置类:

@ConfigurationProperties(prefix = "myConfig")
public class MyConfig {
    protected String config;
}

我的服务使用这个配置类(从application.yml获取值):

@Service
public class myService {

    @Inject
    private MyConfig myConfig;

    Public String getInfo (String param) {
        if (isEmpty(param)) { throw new InvalidParameterException; }
        return myConfig.getConfig();
    }
}

我正在尝试使用 Mockito 对其进行测试:

@RunWith(MockitoJUnitRunner.class)
public class myTest {

    @InjectMocks
    private MyService myService;

    @Mock
    private MyConfig myConfig;

    @Test
    public void myTest1() {
        myService.getInfo("a");
    }

    @Test
    public void myTest2() {
        assertThrows(InvalidParameterException.class, ()->myService.getInfo(null));
    }
}

myTest 失败,因为配置类被模拟,因此具有空值。 使用 Mockito 测试配置类的正确方法是什么?

编辑: 我有许多配置类,例如上面的配置类,它们在 myService 中使用。

【问题讨论】:

  • 您必须定义 myConfig 模拟的行为。例如:when(myConfig.getConfig()).thenReturn( ... ); 其中... 是您想要返回的任何内容。但是,如果要定义行为,则需要 MyConfig 对象中的方法。您可能还想在您的问题中添加一些代码,以便我们可以看到 MyService 实际在做什么。
  • 你可能想看看这个问题:stackoverflow.com/questions/31745168/…

标签: spring testing mockito


【解决方案1】:

您需要创建可以被 Mockito 模拟的 getter。

@ConfigurationProperties(prefix = "myConfig")
public class MyConfig {
    protected String config;

    public String getConfig() {
        return config;
    }
}

。

@RunWith(MockitoJUnitRunner.class)
public class myTest {

    @InjectMocks
    private MyService myService;

    @Mock
    private MyConfig myConfig;

    @Before
    private void initializeConfig() {
        when(myConfig.getConfig()).thenReturn("someValue");
    }

    @Test
    public void myTest1() {
        myService.getInfo("a");
    }
}

但是,如果您不想在测试中显式设置值,则应该创建一个 Spring 集成测试,该测试将创建整个上下文并使用真实对象。但这超出了这个问题的范围。

【讨论】:

  • 这个配置类从我的application.yml获取数据,所以我想使用这个文件中的数据,不想在测试中手动设置。有没有办法从application.yml 设置它?
  • 恐怕 Mockito runner 做不到。也许尝试使用 SpringRunner.class 应该创建整个上下文并注入依赖项。您可能还需要创建一些仅在测试中使用的上下文。
  • 如何使用实物?
【解决方案2】:

您应该使用@Configuration 标记MyConfig,以便Spring 在应用程序上下文中创建一个Spring bean。

你还需要使用@RunWith(SpringRunner.class)

SpringRunner 支持加载 Spring ApplicationContext 并将 bean @Autowired 放入您的测试实例。

还要用@Autowired 更改@Inject,因为我不确定spring 将如何支持@inject

服务类

@Service
public class myService {

    @Autowired
    private MyConfig myConfig;

    Public String getInfo {
        return myConfig.getConfig();
    }
}

测试类

@RunWith(SpringRunner.class)
public class myTest {

    @Mock
    private MyService myService;

    @InjectMocks
    private MyConfig myConfig;

这是你应该在测试中获得真实对象的方式

编辑

考虑到你不想模拟 myservice 那么我们应该用下面替换你的测试类并告诉我

@RunWith(SpringRunner.class)
public class myTest {

@autowired
private MyService myService;

@Test
public void myTest1() {
    myService.getInfo("<NAME_OF_PROPERTIES>");
}

@Test
public void myTest2() {
    assertThrows(InvalidParameterException.class, ()->myService.getInfo(null));
}

【讨论】:

  • 我仍然收到带有这些异常的 NullPointerException。这是如何运作的? InjectMocks 不是为您提供了一个具有空值的模拟对象吗?
  • 您甚至可以在测试类中的 MyConfig 上使用 @autowired,然后使用 myService.setMyConfig(>) 获取该实例设置。
  • @InjectMocks 创建一个真实对象并尝试注入带有@Mock 注释的字段。但是,在您的情况下,您不想模拟 MyService 类(因为您希望拥有真实值)。
  • @psi:您在示例中切换了注释。
  • @second 我同意.. 我这样做只是为了将真实对象 Myconfig 注入到服务模拟对象中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多