spring DynamicDataSourceTest 之 main 与 junit

龙健民 发布于 2015/05/16 12:14
阅读 402
收藏 0

项目需要。要实现DynamicDataSource 用threadLocal 控制data 保存的DB目标

在测试的时候发现一个奇怪的问题:

如果使用JUnit测试,不能正确的切换DB

如果用main 测试则是可以正确的切换DB

风别贴一下代码:

JUnit:

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles(profiles = "development")
@ContextConfiguration(locations={"classpath*:beans/**/*.xml"})
//如需不用测试事物则将false 改为 true
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public abstract class AbstractServiceTest extends AbstractTransactionalJUnit4SpringContextTests{
}


public class TestEhcacheServiceImplTest extends AbstractServiceTest{

@Test
public void test() throws Exception{
TestEhcacheService ehcacheServiceImpl = (TestEhcacheService)ApplicationContextUtil.getBean("testEhcacheService");
M_test test = new M_test();
test.setName("zhuk");
test.setPw("zqack");
ehcacheServiceImpl.save(test);

CustomerContextHolder.setCustomerType("zack");
M_test test2 = new M_test();
test2.setName("test1");
test2.setPw("zqack");
ehcacheServiceImpl.save(test2);

CustomerContextHolder.setCustomerType("magic");
M_test test3 = new M_test();
test3.setName("test2");
test3.setPw("zqack");
ehcacheServiceImpl.save(test3);
}


main:

public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:beans/**/*.xml");
TestEhcacheService ehcacheServiceImpl = (TestEhcacheService)ac.getBean("testEhcacheService");
M_test test = new M_test();
test.setName("zhuk");
test.setPw("zqack");
ehcacheServiceImpl.save(test);

CustomerContextHolder.setCustomerType("zack");
M_test test2 = new M_test();
test2.setName("test1");
test2.setPw("zqack");
ehcacheServiceImpl.save(test2);


CustomerContextHolder.setCustomerType("magic");
M_test test3 = new M_test();
test3.setName("test2");
test3.setPw("zqack");
ehcacheServiceImpl.save(test3);

}

请各位前辈指教。谢谢!

加载中
0
龙健民
周末国软都出去HI了,只有单身狗在家写代码
0
龙健民

额,来个大神啊


0
龙健民
hello,有人吗??
返回顶部
顶部