• 利用 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
  • 相关阅读:
    jquery ajax 提交json格式数据
    作为运维你还在想要不要学Python?听听运维老司机怎么说
    《红蓝攻防对抗实战》一. 隧道穿透技术详解
    2021年JAVA 精心整理的常见面试题-附详细答案【持续更新~~】
    1110 区块反转 分数 25
    基于IMX8MPlus SMARC核心板的便携式床旁超声诊断仪应用解决方案
    Windows MFC 工程应用开发与框架原理完全剖析视频教程(上)
    OpenStack云计算(十一)——OpenStack网络管理,验证OpenStack网络资源模型,验证来巩固和加深对OpenStack网络资源模型的理解
    CYarp:力压frp的C#高性能http内网反代中间件
    一.STM32F030C8T6 MCU开发之系统时钟
  • 原文地址:https://blog.csdn.net/nathan_csdn/article/details/133278264