• 利用 spring test 实现自动启动spring 容器进行 JPA接口测试


    自动启动context JPA接口测试

    
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.junit.BeforeClass;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.mock.jndi.SimpleNamingContextBuilder;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.TestPropertySource;
    import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
    import org.springframework.test.context.transaction.TransactionConfiguration;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    
    @ContextConfiguration(locations = {
            "classpath:applicationContext-commons-configuration.xml",
            "classpath:applicationContext-commons-cache.xml",
            "classpath:applicationContext-commons-spring.xml",
            "classpath:applicationContext-oaml-core.xml"
    })
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
    @TestPropertySource(properties = "application.name = xxx-aml")
    public abstract class BaseTest extends AbstractTransactionalJUnit4SpringContextTests{
        
        static final Logger LOGGER = LoggerFactory.getLogger(BaseTest.class);
        
        @PersistenceContext
        protected EntityManager entityManager;
        @Autowired
        protected static ApplicationContext applicationContext;
        
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            LOGGER.info("loadJndiDataSource start");
            final SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
            
            final DruidDataSource profileDS = new DruidDataSource();
            profileDS.setDriverClassName("com.mysql.jdbc.Driver");
            profileDS.setUrl("jdbc:mysql://127.0.0.1:7954/dev_profile?characterEncoding=utf-8");
            profileDS.setUsername("xxx-dev");
            profileDS.setPassword("xxx");
            profileDS.setInitialSize(1);
            profileDS.setMaxActive(20);
            profileDS.setMaxWait(60000);
            profileDS.setMinEvictableIdleTimeMillis(300000);
    
    
            final DruidDataSource oamlDS = new DruidDataSource();
            oamlDS.setDriverClassName("com.mysql.jdbc.Driver");
            oamlDS.setUrl("jdbc:mysql://127.0.0.1:7954/dev_oaml?characterEncoding=utf-8");
            oamlDS.setUsername("xxx-dev");
            oamlDS.setPassword("xxxxx");
            oamlDS.setInitialSize(1);
            oamlDS.setMaxActive(20);
            oamlDS.setMaxWait(60000);
            oamlDS.setMinEvictableIdleTimeMillis(300000);
    
            builder.bind("java:comp/env/jdbc/PROFILEDS", profileDS);
            builder.bind("java:comp/env/jdbc/OAMLDS", oamlDS);
            builder.activate();
            LOGGER.info("loadJndiDataSource END");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    执行业务逻辑之前,动态添加bean到spring容器中

    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.context.ConfigurableApplicationContext;
    
    
            ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getBeanFactory();
            registry.registerBeanDefinition("blacklistFactTypeHandler", BeanDefinitionBuilder.genericBeanDefinition(BlacklistFactTypeHandler.class).getRawBeanDefinition());
            registry.registerBeanDefinition("modifyScoreResultHandler", BeanDefinitionBuilder.genericBeanDefinition(ModifyScoreResultHandler.class).getRawBeanDefinition());
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    分享 | 运营必备11大数据分析模型,建议收藏
    数据库学习02
    使用ChatGPT进行数据分析案例——贷款数据分析
    【Unity3D】运动模糊特效
    C++ list使用与模拟
    ERP编制物料清单 金蝶
    “亚马逊依赖”之下,傲基科技的品牌势能如何提升?
    Springboot项目多模块打包jar移动到指定目录,docker打jar包构建镜像部署并运行
    php获取前端ajax数据对服务器json文件进行增改
    【微信开发第一章】SpringBoot实现微信公众号创建菜单,同步菜单功能
  • 原文地址:https://blog.csdn.net/nathan_csdn/article/details/133278264