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下了


    
        src/test/resources
    
    
        src/main/resources
    

你可能感兴趣的:(Java,C#,C++)