当前位置: 首页 > 面试题库 >

Spring测试未从application.properties填充数据库配置

谭铭
2023-03-14
问题内容

在我的spring boot应用程序中,我有以下调用存储过程的类

public class FmTrfUtil {
    static int returnVal;

    public static int insertFmTrfs(List<String> trfs, String source) {
        EntityManager em = Persistence.createEntityManagerFactory("RIStore_FM").createEntityManager();
        Session session = em.unwrap( Session.class );
        final String[] trfArray = trfs.toArray(new String[trfs.size()]);
        final String src = source;

        session.doWork( new Work(){
            public void execute(Connection conn) throws SQLException {
                CallableStatement stmt = null;        
                OracleConnection oraCon = conn.unwrap(OracleConnection.class);
                Array array = oraCon.createARRAY("VARCHAR2_TAB_T", trfArray);
                stmt = conn.prepareCall("{? = call FM_TRF_UTIL.process_fm_trf(?,?)}");
                stmt.registerOutParameter(1, Types.INTEGER);
                stmt.setArray(2, array);
                stmt.setString(3, src);
                stmt.execute();
                returnVal = stmt.getInt(1);
            }
        });
        return returnVal;
    }
}

现在,我想使用Spring Test 集成测试Spring Boot
Application中的
示例,在该类上运行集成测试。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RistoreWebApplication.class)
@WebAppConfiguration
public class FmTrfUtilTest {

    static EntityManagerFactory factory = null;
    static EntityManager manager = null;

    @Test
    public void test() {
        List<String> trfs = new ArrayList<String>();
        trfs.add("TRF000001");
        trfs.add("TRF000002");
        int ret = FmTrfUtil.insertFmTrfs(trfs, "SARC");
        assertTrue(ret > 0);
    }
}

RistoreWebApplication是主应用程序类的名称,它是api的入口点。根据该网页,“
@SpringApplicationConfiguration注释将触发用于读取特定于Spring Boot的配置,属性等的逻辑。”
我的理解是,这还将在我的application.properties中加载数据库连接信息:

spring.datasource.url=jdbc:oracle:thin:@ldap://xxx:389/risdev3, cn=OracleContext,dc=yyy,dc=com
spring.datasource.username=rowner
spring.datasource.password=rowner987
spring.datasource.driverClassName=oracle.jdbc.OracleDriver

但是,当我运行此测试时,我得到了UnsupportedOperationException: The application must supply JDBC connections。我如何准确地加载运行测试的数据库连接配置?我正在使用Spring Boot 1.3

进行编辑, 因为application.properties位于src / main / resources中,而我的测试类位于src / test
/ java中,所以我认为src/test/resources基于SpringjUnitTesting属性文件的答案,我还会有另一个属性文件。因此,我这样做了,并@PropertySource("classpath:application-test.properties")在测试类中添加了注释,但是仍然遇到相同的错误。有没有办法知道是否正在读取属性文件和/或当前的类路径是什么?


问题答案:

修改您的测试类,如下所示:

@SpringApplicationConfiguration(classes = RistoreWebApplication.class, initializers = ConfigFileApplicationContextInitializer.class)

请参考以下内容:

http://docs.spring.io/spring-
boot/docs.old/current/api/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.html

编辑:关于以下评论。

通过存储库访问数据存储。

http://docs.html" target="_blank">spring.io/spring/docs/current/spring-framework-
reference/html/integration-
testing.html

@Autowired
YourRepository repository;
repository.save(Arrays.asList("value_1", "value_2", "value_3"));


 类似资料:
  • 我有一个非常简单的spring boot应用程序,我正在尝试使用一些外部配置。我试着按照spring boot文件上的信息去做,但是我遇到了一个障碍。 当我运行下面的应用程序时,application.properties文件中的外部配置不会填充到bean中的变量中。我肯定我在做傻事,谢谢你的建议。 mybean.Java(位于/src/main//foo/bar/中) application.J

  • 但是,当我运行使用maven从命令行生成的jar时,它不会读取application.properties,默认情况下,tomcat是在8080上启动的,我无法识别上下文。其他的一切都很好。 在eclipse中,我将:VM参数提供为: 我文章和问题看起来很相似,我已经引用了这篇文章,只有我引用了Application.Properteis来配置spring boot应用程序的自定义上下文和端口。我

  • 简介 Laravel 可以用 seed 类轻松地为数据库填充测试数据。所有的 seed 类都存放在 database/seeds 目录下。你可以任意为 seed 类命名,但是更应该遵守类似 UsersTableSeeder 的命名规范。Laravel 默认定义的一个 DatabaseSeeder 类。可以在这个类中使用 call 方法来运行其它的 seed 类从而控制数据填充的顺序。 编写 See

  • 我看到了许多类似问题的答案,人们说为了获得组合框中加载的项目的值,需要使用 但这种东西不起作用…… 这是我所拥有的…… 我从表中选择了Code1和Code2,我希望能够显示Code1,当被选中时,我希望能有Code2的值,但对于displayMember和ValueMember,我看不到任何结果。 编辑:这是我的所有代码: 暗淡的行=dt. Load(com. Ex 错误提示:表达式不产生值 编辑

  • 我有一个有两列的表,一个是string,另一个是double Type。例如, 我正在尝试读取所有的值,并将它们加载到中 我在读取表数据方面没有任何问题,但不能按我的要求将它们放入HashMap中。 我编写了一个示例代码来从两个不同的数组(string和double)填充一个HashMap,但仍然无法处理HashMap的值部分,它是一个数组。 任何建议我如何处理HashMap的值部分,它是一个数组

  • 我需要添加测试到我的Spring Boot项目。使用rails框架有开发和测试数据库,它们是在新项目启动时创建的。迁移应用于两个数据库以保持数据库模式相等。不幸的是,我没有找到关于如何在Spring Boot中配置测试环境数据库以及如何使数据库结构等于dev的留档。 在Spring Boot测试的最佳实践是什么?如何为测试环境设置和添加测试数据?有什么例子吗?