测试类
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>
本文探讨了SpringBoot环境下单元测试的配置与执行过程,包括如何避免因资源重复加载导致的测试错误,以及如何正确地使用SpringBoot的测试模块进行类的测试。通过实例演示了如何在测试中注入依赖,以及正则表达式的简单应用。
1715

被折叠的 条评论
为什么被折叠?



