java 测试时带有数据源的 Spring Boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34756264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:17:09  来源:igfitidea点击:

Spring Boot with datasource when testing

javaspringspring-boot

提问by HereToLearn

I am using Spring Boot application and the auto cofiguration is enabled. The main Application file is marked as @EnableAutoConfiguration. The datasource is lookedup from JNDI is configured using java config and the class which create the datasource is marked as @Configuration.

我正在使用 Spring Boot 应用程序并启用了自动配置。主应用程序文件标记为@EnableAutoConfiguration. 从 JNDI 查找的数据源是使用 java config 配置的,并且创建数据源的类被标记为@Configuration.

I have a test class as below.

我有一个测试类,如下所示。

@RunWith( SpringJUnit4ClassRunner.class )
@WebAppConfiguration
@ContextConfiguration( classes = Application.class )
public class TestSomeBusiness {}

The issue is when I run the test case, the datasource jndi lookup happens, which fails because the test case is not running inside a server environment. As far as I know the classes in classpath marked with @Configurationare executed and that the reason the datasource lookup is being called.

问题是当我运行测试用例时,会发生数据源 jndi 查找,但由于测试用例未在服务器环境中运行而失败。据我所知,类路径中标有 的类@Configuration已执行,并且调用数据源查找的原因。

The work around for now I have found is instead of JNDI lookup create the datasource using DriverManagerDataSource, so that even if its not a server environment the datasource lookup won't fail.

现在我发现的解决方法是使用 代替 JNDI 查找创建数据源DriverManagerDataSource,这样即使它不是服务器环境,数据源查找也不会失败。

My questions are:

我的问题是:

1) How do we generally deal with datasource (when looking up from JNDI) in spring boot application for testing ?

1)我们通常如何处理spring boot应用程序中的数据源(从JNDI查找时)进行测试?

2) Is there a way to exclude the datasource configuration class from being called when executing test case ?

2)有没有办法在执行测试用例时排除调用数据源配置类?

3) Should I create an embedded server so that the JNDI lookup can be done when executing test case ?

3) 我应该创建一个嵌入式服务器,以便在执行测试用例时可以完成 JNDI 查找吗?

采纳答案by Ali Dehghani

2) Is there a way to exclude the datasource configuration class from being called when executing test case ?

2)有没有办法在执行测试用例时排除调用数据源配置类?

You can add a application.propertiesconfig file into your src/test/resourcesand spring boot would pick those configurations in test environments. I suppose, you have application.propertiesin your src/main/resourceslike this:

您可以在您的application.properties配置文件中添加一个配置文件src/test/resources,spring boot 会在测试环境中选择这些配置。我想,你有application.propertiessrc/main/resources这样的:

spring.datasource.jndi-name=some_jndi

This JNDIresource will be used in your production environment. For your test environment you can use a, say MySQL database, by adding these configurations into your test application.properties:

JNDI资源将在您的生产环境中使用。对于您的测试环境,您可以使用 MySQL 数据库,方法是将这些配置添加到您的测试中application.properties

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3) Should I create an embedded server so that the JNDI lookup can be done when executing test case ?

3) 我应该创建一个嵌入式服务器,以便在执行测试用例时可以完成 JNDI 查找吗?

As i said, you can totally bypass the fact that you're using JNDIfor production by adding test specific configurations.

正如我所说,您可以JNDI通过添加测试特定配置来完全绕过您用于生产的事实。

1) How do we generally deal with datasource (when looking up from JNDI) in spring boot application for testing ?

1)我们通常如何处理spring boot应用程序中的数据源(从JNDI查找时)进行测试?

You can mock JNDIresources using facilities available in org.springframework.mock.jndipackage. For example by using SimpleNamingContextBuilderyou can:

您可以JNDI使用org.springframework.mock.jndi包中可用的工具来模拟资源。例如,通过使用SimpleNamingContextBuilder您可以:

SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("jndi_name", dataSource);
builder.activate();

The other option is, of course, using Non JNDIresources in test environments.

当然,另一种选择是Non JNDI在测试环境中使用资源。