【发布时间】: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