• 利用 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
  • 相关阅读:
    独孤思维:自动得3w,这样赚钱颠覆你的认知
    代码随想录 | Day46
    Android开发之指南针
    行业洞察 | 谁动了艺术家的奶酪?
    onnxruntime android版build & 使用
    发明专利是什么
    .NET Core中JWT+Auth2.0实现SSO,附完整源码(.NET6)
    Docker Desktop 界面功能介绍,添加国内镜像源
    解码Java的垃圾回收:掌握GC机制,提升代码的稳定性与可伸缩性
    后端Boy转为数据库Boy基础最流行的SQL题,学完可以巩固几乎所有的查询,推荐分享
  • 原文地址:https://blog.csdn.net/nathan_csdn/article/details/133278264