Junit 集成测试
Junit integration test
我正在尝试进行如下集成测试。
我的自动装配 MyService
有其他自动装配依赖项 BCryptPasswordEncoder
导致 myService class 创建失败。我不想注入模拟版本。任何的想法?谢谢
@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;
}
编辑: 我在集成测试中避免模拟的原因是因为我读到在使用过程中可能会发生一些意外行为。
终于找到问题了。我在 spring-security.xml 中定义了 bean BCryptPasswordEncoder
,我只加载了 application-context xml。
解决方案:
我也在 application-context.xml 中定义了 bean,我能够使用真实的数据库和真实的 myService
class 进行集成测试。
我正在尝试进行如下集成测试。
我的自动装配 MyService
有其他自动装配依赖项 BCryptPasswordEncoder
导致 myService class 创建失败。我不想注入模拟版本。任何的想法?谢谢
@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;
}
编辑: 我在集成测试中避免模拟的原因是因为我读到在使用过程中可能会发生一些意外行为。
终于找到问题了。我在 spring-security.xml 中定义了 bean BCryptPasswordEncoder
,我只加载了 application-context xml。
解决方案:
我也在 application-context.xml 中定义了 bean,我能够使用真实的数据库和真实的 myService
class 进行集成测试。