当前位置: 首页 > 知识库问答 >
问题:

如何在Junit5中从配置中获取数据源bean

程毅
2023-03-14

亲爱的,

我已经创建了一个junit5测试用例(UserDaoTests.java),但无法在spring配置文件(data.xml)中定义DataSource Bean。

数据junit的xml配置文件。我在其中定义了数据源bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:component-scan
        base-package="com.caveofprogramming.spring.test.tests">
    </context:component-scan>
    <context:annotation-config></context:annotation-config>

    <mvc:annotation-driven></mvc:annotation-driven>

    <context:property-placeholder
        location="test/com/caveofprogramming/spring/test/config/jdbc.properties" />

    <beans profile="dev">
        <bean id="dataSource"
            class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="username" value="${jdbc.username}"></property>
        </bean>
    </beans>
</beans>

jdbc。属性:

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = TESTD01)))
jdbc.password=1234
jdbc.username=offerTest

JUnit测试(UserDaoTests.java)

    package com.caveofprogramming.spring.test.tests;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

import com.offers.DAO.UserDAO;

@ActiveProfiles("dev")
@ContextConfiguration(locations = {
        "classpath:test/com/caveofprogramming/spring/test/config/data.xml",
        "classpath:com/offers/config/offerDAO-context.xml", 
        "classpath:com/offers/config/offerService-context.xml" })



public class UserDaoTests {

    @Autowired
    private UserDAO userDao;
    
    private static  DataSource dataSource; 
    
    private  JdbcTemplate jdbc;
    
    @Autowired
    public UserDaoTests() {
        ApplicationContext context = new FileSystemXmlApplicationContext("test/com/caveofprogramming/spring/test/config/data.xml");
        this.dataSource = (DataSource) context.getBean("dataSource");
    }
    
    @Test
    public void testCreateUser() {
        if ( dataSource == null) {
            System.out.println("DataSource is null");
        }
        else {
            System.out.println("DataSource is not null!!!!  ");
        }
    }
}

日志:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:816)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

我还想澄清一下,我使用静态数据源,因为我需要使用@BeforeAll,它只需要静态字段。

我也测试了非静态数据源,但结果仍然相同。

--更新--

我移除了<代码>

---更新2(更新布山的答案)-----

UserDaoTests-更新的代码

@ExtendWith(SpringExtension.class)
//Add all required depency xml
@ContextConfiguration(locations = { "classpath:test/com/caveofprogramming/spring/test/config/data.xml",
        "classpath:com/offers/config/offerDAO-context.xml", 
        "classpath:com/offers/config/offerService-context.xml" })
@ActiveProfiles("dev")
public class UserDaoTests {
    @Autowired
    private UserDAO userDao;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private JdbcTemplate jdbc;

    @Test
    public void testCreateUser() {
        Assertions.assertNotNull(dataSource);
        // Assertions.assertNotNull(jdbc);
    }
}

堆栈跟踪错误:

org.opentest4j.AssertionFailedError: expected: not <null>
    at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:39)
    at org.junit.jupiter.api.Assertions.fail(Assertions.java:109)
    at org.junit.jupiter.api.AssertNotNull.failNull(AssertNotNull.java:47)
    at org.junit.jupiter.api.AssertNotNull.assertNotNull(AssertNotNull.java:36)
    at org.junit.jupiter.api.AssertNotNull.assertNotNull(AssertNotNull.java:31)
    at org.junit.jupiter.api.Assertions.assertNotNull(Assertions.java:283)
    at com.caveofprogramming.spring.test.tests.UserDaoTests.testCreateUser(UserDaoTests.java:38)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:43)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:82)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:73)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:141)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

共有1个答案

阳德润
2023-03-14

在这里,您正在创建多个应用程序上下文,一个是使用@ContextConfiguration创建的,另一个是使用FileSystemXmlApplicationContext创建的,因此请避免使用FileSystemXmlApplicationContext,并按照以下步骤更新代码

@ExtendWith(SpringExtension.class)
//Add all required depency xml
@ContextConfiguration(locations = {
        "classpath:test/com/caveofprogramming/spring/test/config/data.xml",
        "classpath:com/offers/config/offerDAO-context.xml", 
        "classpath:com/offers/config/offerService-context.xml" })
@ActiveProfiles("dev")
public class UserDaoTests {
    @Autowired
    private UserDAO userDao;
    
    @Autowired
    private DataSource dataSource; 
    
    @Autowired
    private  JdbcTemplate jdbc;
    
    
  @Test
    public void testCreateUser() {
           Assertions.assertNotNull(dataSource);
           Assertions.assertNotNull(jdbc);
    }
}

另外,还要避免@BeforeAll,因为它与静态内存相结合,我们的bean由spring管理,所以不能使用它,取而代之的是,你可以为@beforeach执行一些逻辑操作,如果你想对所有测试用例只运行一次,这样你就可以进行类似的hack操作

private AtomicBoolean executed = new AtomicBoolean(false);

@BeforeEach
public void runOnce() {
    if(!executed.getAndSet(true)) { 
        // write you logic which you want will run only once for all test cases
    }
}
 类似资料:
  • 问题内容: 这是我的类,用于从数据库中获取数据 这是我的文件: 当我运行该程序时,出现异常后,请帮助我如何解决它。我是Hibernate的新手,尝试学习但被卡住了。 虽然我能够将数据存储在数据库中,但我有2个用于第一和第二类的数据获取数据,但在获取数据时遇到了问题PLZ帮助。 问题答案: 让我引述一下: 据我所知,您正在使用表名。 所以应该是这样的:

  • 问题内容: 我正在尝试在http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html上遵循Sun的JDBC教程。 它提供了以下示例代码: 该代码无法编译,因为除了最后调用的方法外,接口没有这些方法。 (以下是Javadoc:http : //java.sun.com/javase/6/docs/api/javax/sql/D

  • 我想知道的是: 是否可以为本地宿主数据库获取对象? 类需要发布吗?还是像那样,只获得连接而不创建新类? 你能举个例子吗?

  • 我有一张狗的名单,需要取一些数据。在一种情况下,我需要在列表中显示名称行,在其他情况下,我需要来自一只狗的全部或部分数据(名称、性别、速度)。我相当肯定我应该使用数组,尽管我是从字典开始的。我计划添加更多参数,并允许用户添加更多的狗,所以我试图找到最可扩展的选项

  • 在建造中。我有: