SpringBoot Test重复加载配置文件

测试类

package com.unionpay.pipbat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;


import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
//获取启动类,加载配置,确定装载 Spring 程序的装载方法,它回去寻找 主配置启动类(被 @SpringBootApplication 注解的)
//@SpringBootTest(classes = PipBatApplication.class)     //不能这样配 magpie重复初始化报错
//@ContextConfiguration(locations = "classpath*:magpiebeans.xml")
//@SpringBootApplication
@SpringBootTest(classes = PipBatApplicationTests.class)
@TestPropertySource("classpath:application-dev.properties")
@SpringBootApplication
 public class PipBatApplicationTests {
    Logger logger= LoggerFactory.getLogger(PipBatApplicationTests.class) ;

    @Autowired
    ApplicationContext applicationContext;
    
    @Test public void contextLoads() {

    }
    @Test
    public void  test()
    {
        System.out.println("test");
    }
     class Holder
     {
         int res;
     }

     @Test
     public void testSelectByPrimaryKey(){
         /*TblPipbatBatSt tblPipbatBatSt =  pipbatBatStService.selectByPrimaryKey(123456);
         Assert.assertNotNull(tblPipbatBatSt);*/
         System.out.println("OK");
         /*PipbatBatStService pipbatBatStService = (PipbatBatStService) applicationContext.getBean("pipbatBatStService");
         TblPipbatBatSt tblPipbatBatSt = pipbatBatStService.selectByPrimaryKey(123456);
         Assert.assertNotNull(pipbatBatStService);*/
         /*Itest itest = (Itest) applicationContext.getBean("testServiceA");*/

     }
     @Test
    public void Pattern()
     {
         String pattern="([a-zA-Z0-9]+)((\\.[a-zA-Z0-9]+)*)";
         boolean isMatch = Pattern.matches(pattern, "dege.rege.");
         System.out.println(isMatch);
     }
}

 

 

运行一个测试方法报错  XXX bean重复定义  发现定义xxx bean的配置文件在target/classes 和target/test_classes下都存在

怀疑可能是分别加载了这两个目录下的文件

删除pom.xml下红色的部分resources下的文件就不再生产到target/test_classes下了

<testResources>
    <testResource>
        <directory>src/test/resources</directory>
    </testResource>
    <testResource>
        <directory>src/main/resources</directory>
    </testResource>
</testResources>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值