【问题标题】:Junit integration testJunit集成测试
【发布时间】:2018-03-05 01:52:05
【问题描述】:

我正在尝试如下进行集成测试。 我的自动连线 MyService 有其他自动连线依赖项为 BCryptPasswordEncoder,这会导致 myService 类创建失败。我不想注入模拟版本。任何的想法?谢谢

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/application-component-context.xml")
public class ServiceUnitTest {

    @Autowired
    MyService myService;

    @BeforeClass
    public static void init() throws Exception {
        TestJNDIDB.create();
    }

    @Before
    public  void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testProcessRegistration() throws Exception {    
        SomeObject object = myService.ProcessSomeObject;
    }

}
//this is real class and works while application run
@Component
MyService(){
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
}

编辑:我在集成测试中避免使用模拟的原因是因为我读到在使用过程中可能会发生一些意外行为。

【问题讨论】:

  • “我不想注入模拟版本”。为什么不想注入模拟版本?正如你所说,真实版本是有问题的。模拟是解决此问题的一种非常常见的方法。那么你为什么不想使用模拟呢?
  • @EJK,我想我想看看使用真实对象而不是 Mock 进行集成测试能走多远,如果没有其他选择,我会使用。还猜测当我需要在会话中保存某些内容时,我可能会遇到同样的问题。
  • 不确定问题有什么问题?

标签: spring junit integration-testing


【解决方案1】:

我终于找到了问题所在。我在 spring-security.xml 中定义了 bean BCryptPasswordEncoder,并且我只加载了 application-context xml。

作为解决方案: 我还在 application-context.xml 中定义了 bean,我能够使用真正的 db 和真正的 myService 类进行集成测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 2014-04-04
    • 1970-01-01
    相关资源
    最近更新 更多