junit跳过datasource_maven – spring集成测试无法加载上下文“另一个资源已存在,名称为dataSource”...

在Spring Boot 1.4.3的集成测试中,使用@MockBean注解模拟bean行为导致缓存的应用上下文出现'Another resource already exists with name dataSource'错误。解决方案可能涉及为@MockBean指定限定符或使用@Mock配合@Autowired以控制依赖注入。
摘要由CSDN通过智能技术生成

我正在使用

spring-boot 1.4.3中引入的测试注释进行集成测试

@RunWith(SpringRunner.class)

@SpringBootTest

public class MyServiceIT { }

根据documentation,测试上下文被缓存并重用以加速集成测试.这种行为是我想要的,因为它需要大量的时间来初始化应用程序上下文.我的故障安全插件配置了

1

true

允许集成测试在同一进程中运行,以利用应用程序上下文缓存.

最近,我编写了一个使用@MockBean注释的集成测试来模拟某些bean的行为.

@RunWith(SpringRunner.class)

@SpringBootTest

public class AnotherServiceIT {

@MockBean

SomeService service1

}

虽然测试运行良好,但是当通过maven验证运行时,多个集成测试会因错误消息而失败

javax.naming.NamingException: Another resource already exists with

name dataSource – pick a different name

如果我使用JUnit @Ignore注释跳过此特定测试,一切都恢复正常.

此行为似乎表明使用@MockBean更改了缓存行为,并且每个测试都尝试创建自己的数据源.我还要提一下,我正在使用通过XADataSourceAutoConfiguration创建的AtomikosDataSourceBean.

如何克服此问题,以便我的集成测试仍然可以使用缓存上下文并同时使用@MockBean?

最佳答案 嗯,SomeService会以任何方式与您的数据源相关吗?

因为您的上下文被缓存而@MockBean执行以下操作:

used to add mocks to a Spring ApplicationContext … Any existing single bean of the same type defined in the context will be replaced by the mock,

If there is more than one bean of the requested type, qualifier metadata must be specified at field level:

@RunWith(SpringRunner.class)

public class ExampleTests {

@MockBean

@Qualifier("example")

private ExampleService service;

编辑:

因此,如果您的SomeService是DataSource的实现,请尝试添加限定符.如果SomeService中包含DataSource,并且您需要访问其中的某些方法,则可以尝试使用@Mock并指定需要通过自己的mock或autowire返回的任何对象.

@Mock

SomeService someService;

@Mock

SomeDependency mockDependency;

@Autowired

OtherDependency realDependency;

@Before

public void setUp() {

MockitoAnnotations.initMocks(this);

doReturn(mockDependency).when(someService).getSomeDependency();

doReturn(realDependency).when(someService).getOtherDependency();

}

在搭建数据源时,首先需要通过Maven创建一个基础的项目结构。以下是基本步骤: 1. **初始化项目**: - 创建一个新的Maven项目,选择"Quick Start"模板开始。 - 添加适当的编码格式(如UTF-8),并设置项目名称。 2. **添加依赖**: - 配置`pom.xml`文件: - 添加```<dependencies>```标签。 - 添加MyBatis的依赖,例如: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> <!-- 根据实际版本更换 --> </dependency> ``` - 添加日志管理依赖,比如Log4j: ```xml <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.1</version> <!-- 根据实际版本更换 --> </dependency> ``` - 添加MySQL数据库驱动(JDBC连接)依赖: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> <!-- 根据实际版本更换 --> </dependency> ``` - 如果你需要集成单元测试,添加JUnit依赖: ```xml <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <!-- 根据实际版本更换 --> <scope>test</scope> </dependency> ``` 3. **配置文件**: - 在resources目录下创建`mybatis-config.xml`(MyBatis的配置文件)和`log4j.properties`(日志配置)。 - 编辑`mybatis-config.xml`,配置数据源、事务等信息。 4. **环境变量或配置类**: - 可能需要设置数据库连接字符串、用户名、密码等环境变量。 - 或者在applicationContext.xml或Spring Boot的配置类中配置DataSource。 5. **整合到测试环境**: - 使用Maven的`@RunWith(SpringRunner.class)`注解启动单元测试,可以注入并测试数据源。 完成上述步骤后,你就有了一个基本的数据源搭建环境,可用于MyBatis与MySQL的交互以及相应的测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值