首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Autowired SpringRunner SpringBootTest单元测试用例

@Autowired SpringRunner SpringBootTest单元测试用例
EN

Stack Overflow用户
提问于 2017-08-29 14:59:03
回答 1查看 1.5K关注 0票数 5

我们的目前有一个相当大的单元测试存储库。单元测试将通用的可重用测试代码重构到TestUtil类中,这些类是@Component注释的。

看来,SpringRunner单元测试用例只能在@Autowired TestUtil类作为@SpringBootTest注释的类参数的一部分导入时才能找到它们。

此外,@Autowired类中的所有TestUtil变量也需要在@SpringBootTest注释的类参数中导入。

由于我们有大约30个单元测试用例类,而且每个类都需要在@SpringBootTest注释中导入大约40个其他类,因此可以想象这是多么难以维护。

如果类未作为@SpringBootTest类参数的一部分导入,则引发以下错误

org.springframework.beans.factory.UnsatisfiedDependencyException:错误创建名为“com.company.FeeTest”的bean :通过字段‘accountTestUtils’表示的不满意的依赖关系;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException: No,类型为'com.company.accountTestUtils‘类型的限定bean :预期至少有一个bean可以作为自动测试候选。依赖性注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

有没有人知道在单元测试用例中使用@Autowired注释而不必在@SpringBootTest注释中显式导入它们的更好方法?

下面是一个代码示例

FeeTest.java

代码语言:javascript
复制
package com.company;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
        AccountTestUtils.class,
        ... need to list any @Autowired component in AccountTestUtils
})
public class FeeTest {
    @Autowired
    private AccountTestUtils accountTestUtils;

    @Test
    public void basicFeeTestExpectSuccess() {
       accountTestUtils.createAccount();
       ...
    }
}

TransferTest.java

代码语言:javascript
复制
package com.company;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
        AccountTestUtils.class,
        ... need to list any @Autowired component in AccountTestUtils
})
public class TransferTest {
    @Autowired
    private AccountTestUtils accountTestUtils;

    @Test
    public void basicTransferTestExpectSuccess() {
       accountTestUtils.createAccount();
       ...
    }
}

AccountTestUtils.java

代码语言:javascript
复制
package com.company;

@Component
public class AccountTestUtils {
    @Autowired
    private IService1 someService1;

    @Autowired
    private IService2 someService2;

    @Autowired
    private SomeRepository someRepository1;

    public void createAccount() {
      someService1.doSomething();
      someService2.doSomething();
        someRepository2.doSomething();
    }
}    

我们的包结构是常见的maven结构。

代码语言:javascript
复制
/src
    /main
        /java
        /resources
    /test
        /java
        /resources
EN

回答 1

Stack Overflow用户

发布于 2020-01-15 11:53:38

让我从TransferTest.java类的pov中谈谈。

这个类测试AccountUtilsTest类。因此,只需将AccountUtilTest类的依赖项提供给TransferTest.Mock,每个依赖项都是自动连接的,并在AcccountUtilsTest中使用。

例:

代码语言:javascript
复制
package com.company;

@RunWith(SpringRunner.class)
public class TransferTest {

    @Mock
    IService1 someService1Mock;

    @Mock
    IService2 someService2Mock


    @InjectMocks
    private AccountTestUtils accountTestUtils;

    @Test
    public void basicTransferTestExpectSuccess() {
       accountTestUtils.createAccount();
       ...
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45942402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档