问题标签 [spring-boot-test]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
668 浏览

java - SpringBootTest 配置中忽略的 Bean

我有以下结构;

主配置类;

和两个独立的配置类;

测试如下;

使用这种结构,我对 bean 的测试定义EtcDaoSomeBean被忽略,并且使用这些 bean 的主要上下文定义(来自MainApplication.class)。@SpringBootTest但是,如果我在like中包含这些单独的配置@SpringBootTest(classes = {ConfigA.class, ConfigB.class}),那么它可以正常工作。不允许@Include初始化这些单独的配置类中的 bean?或者罪魁祸首是我MainApplication.class和他们一起包含,但我需要其他配置,所以我必须以这种方式实现。

0 投票
4 回答
16060 浏览

java - @Value("${local.server.port}") 在 Spring Boot 1.5 中不起作用

我正在将 Spring Boot 从 1.3 升级到 1.5。为了升级到 1.5,我更换了

@SpringApplicationConfiguration(classes = TestConfig.class) @WebIntegrationTest

@SpringBootTest(classes = TestConfig.class)

另外,我正在使用

@Value("${local.server.port}") protected int port;

获取application.properties文件中定义的端口号。我进一步使用这个端口号来构建一个 REST URL。

但是在升级之后我得到了下面的错误,而在1.3Spring Boot Test 中同样可以正常工作。

原因:java.lang.IllegalArgumentException:无法在 org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) 中解析值“${local.server.port}”中的占位符“local.server.port”

我是否错过了为此工作需要做的任何更改。

0 投票
2 回答
618 浏览

java - SpringBootTest @EnabledWebSocket 被忽略

我想测试我的 WebSocket 应用程序。

测试类:

WebConfig 类:

和 WebSocketConfig 类:

当我开始测试时,Tomcat 正在启动并正在侦听指定的 Port 1234。但我无法连接 websocket 客户端。调用 WebSocketConfig。但我认为 websocket 映射不起作用。我忘了配置什么吗?

当我WebSocketApp.class使用带有注释的应用程序类 ( )开始测试时@SpringBootApplication,websocket 映射工作正常。

WebSocketApp 也使用相同的配置。

我认为第二种方法有效,因为@EnableWebSocket使用了。当我不使用 WebSocketApp.class (用 注释@SpringBootApplication)时,@EnableWebSocket将被忽略。

有人有想法让测试运行吗?如何在没有注释的情况下手动启用 websocket?

编辑:

我发现,那里TomcatEmbeddedContext使用的是默认 servlet 映射而不是dispatcherServlet映射。是否可以设置这种类型的映射?

0 投票
1 回答
1627 浏览

spring-boot - 在集成 springboot 测试中无法识别自定义 Jackson2ObjectMapperBuilder

全部

Jackson2ObjectMapperBuilder在我的主要 Spring Boot 应用程序类中进行了自定义,如下所示。

ExampleFilter的扩展在哪里SimpleBeanPropertyFilter。我的 POJO 用 Ja​​ckson 的@JsonFilter("customFilter").
我已经使用@RestController@GetMapping注释通过 HTTP 公开了 POJO。这可以正常工作,并且当我运行应用程序并访问端点时会调用自定义过滤器。

现在我有一个如下编写的集成测试。

当我运行测试时,obj 为 null,我看到一个错误no FilterProvider configured

我做错了什么,我该如何纠正?

我的文件夹结构是:
src/main/java/com/a/b/c/Application.java
src/main/java/com/a/b/c/controller/ControllerA.java

src/test/java/com/a/b/c/controller/IntegrationTest.java

0 投票
2 回答
10434 浏览

java - 如何在没有嵌入数据源配置的春季启动测试期间执行`schema.sql`?

有用作主数据库的带有 h2 数据库的 spring boot 应用程序。还有一个resource/schema.sqlwich 在启动时由 spring boot 加载。

@SpringBootTest但是在使用spring boot的集成测试期间不会加载这个schema.sql。相反,它需要在h2已经有 db 的情况下设置嵌入式数据库。

有没有办法在schema.sql没有嵌入数据源配置的情况下执行?并且只为所有测试做一次(例如,为所有测试使用@Sql模式创建不是解决方案)?

0 投票
1 回答
1386 浏览

spring - JUnit 测试中的 @SpringBootApplication 注解效果

我有一个 Spring Boot 非 Web 应用程序。

我为我的应用程序添加了一些运行良好的集成测试。

我的主类用@SpringBootApplication和 注释,我的集成测试类用@RunWith(SpringRunner.class)和注释@SpringBootTest

如果我将主类注释更改为@Configurationand @ComponentScan and@EnableAutoConfiguration明确,我的测试类会给出一个编译时错误,说它无法检测到正在使用的类之一,并迫使我添加更多信息。

所以我必须更改如下注释@RunWith(SpringRunner.class)@SpringBootTest(classes = EligibilityJobRunner.class).

我读过这@SpringBootApplication是上述三个注释的便利注释,那么为什么会有这种差异?非常感谢。

0 投票
1 回答
839 浏览

spring-boot - SpringBootTest 支持运行不同的 SpringBoot 应用程序实例

使用 SpringBoot,我可以轻松地将相同的应用程序作为具有不同配置的不同实例运行,例如:

这太棒了,特别是用于测试应用程序间通信的东西。

我现在正在尝试使用 @SpringBootTest 来实现相同的目标,以针对正在运行的应用程序实例运行单元测试。

可能很容易,但我没有。

0 投票
0 回答
472 浏览

spring-boot - 本地 Spring Boot 集成测试通过,但在竹子上所有测试都失败

有一些 Spring Boot 集成测试,例如:

在本地运行时所有测试都成功,但在竹子上所有测试都失败。日志说:

构建 05-Mai-2017 15:20:46 2017-05-05 15:20:46.943 信息 6556 --- [测试工作者] .btcSpringBootTestContextBootstrapper : 既没有找到 @ContextConfiguration 也没有找到 @ContextHierarchy 用于测试类 [com.odm.integration。 SupplierToSapIntegrationTest],使用 SpringBootContextLoader build 05-Mai-2017 15:20:46 2017-05-05 15:20:46.943 INFO 6556 --- [测试工作者] ostcsupport.AbstractContextLoader:无法检测测试类的默认资源位置 [com .odm.integration.SupplierToSapIntegrationTest]:找不到后缀 {-context.xml, Context.groovy} 的资源。构建 05-Mai-2017 15:20:46 2017-05-05 15:20:46.943 INFO 6556 --- [测试工作者] tcsAnnotationConfigContextLoaderUtils:无法检测到测试类的默认配置类 [com.odm.integration。SupplierToSapIntegrationTest]:SupplierToSapIntegrationTest 不声明任何使用@Configuration 注释的静态、非私有、非最终、嵌套类。构建 05-Mai-2017 15:20:46 2017-05-05 15:20:46.943 INFO 6556 --- [测试工作者] .btcSpringBootTestContextBootstrapper:找到 @SpringBootConfiguration com.odm.OdmBackendApplication 用于测试类 com.odm.integration。 SupplierToSapIntegrationTest build 05-Mai-2017 15:20:46 2017-05-05 15:20:46.943 INFO 6556 --- [测试工作者] .btcSpringBootTestContextBootstrapper:从位置加载默认 TestExecutionListener 类名称 [META-INF/spring.factories] :[org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener,org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener,org.springframework.boot.test。20:46 2017-05-05 15:20:46.943 INFO 6556 --- [测试工作者] .btcSpringBootTestContextBootstrapper:无法实例化 TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]。指定自定义侦听器类或使默认侦听器类(及其所需的依赖项)可用。违规类:[javax/servlet/ServletContext] build 05-Mai-2017 15:20:46
2017-05-05 15:20:46.943 INFO 6556 --- [测试工作者] .btcSpringBootTestContextBootstrapper:使用 TestExecutionListeners:[org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@7beec6bf,org.springframework.boot.test.autoconfigure。 SpringBootDependencyInjectionTestExecutionListener@79dadc3e,org.springframework.test.context.support.DirtiesContextTestExecutionListener@128f4f9c,org.springframework.test.context.transaction.TransactionalTestExecutionListener@dad26fb,org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@fbd2cf,org.springframework。 boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@2fefc5c2,org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@47749c0c,org.springframework.boot.test.autoconfigure.web。client.MockRestServiceServerResetTestExecutionListener@230af0e7, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@7db923e3, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@2c293843, org.springframework.boot.test.autoconfigure.web。 servlet.WebDriverTestExecutionListener@4f5e5536]

任何想法,帮助?谢谢!!

0 投票
1 回答
1849 浏览

java - @SpringBootTest 和 @Sql:脚本执行顺序和上下文初始化

我有在内存数据库上执行的集成测试。每个测试的签名或多或少看起来像这样:

在测试上下文初始化期间,数据库模式由 Hibernate 重新创建:

我希望在上下文初始化和数据库模式生成之后执行 sql 脚本。然而,在某些情况下clean-data-in-all-tables.sql,在模式生成之前执行它会失败,因为它需要尚未创建的表。

我按照我解释的方式编写了 500 多个测试,它们都运行良好,直到我添加了一些类似的测试。

当我通过 Gradle 或 IntelliJ 一起执行测试时,测试失败。请注意,失败的测试不是最近添加的测试。这是与我添加的完全无关的旧测试。同样奇怪的是,如果我通过 IntelliJ 一个一个地运行失败的测试,它们运行良好。

它看起来像 spring-boot 的错误,但我仍然试图找到一种解决方法。与此同时,我尝试了很多方法来解决这个问题,但没有一个是有帮助的。

请分享您对我的代码有什么帮助以及我的代码有什么问题的想法。

更新: 找到解决方法:将spring.jpa.hibernate.ddl-autofrom更改create-dropcreate解决问题。

但问题仍然悬而未决,这种奇怪行为的原因是什么?

0 投票
1 回答
1219 浏览

integration-testing - org.dbunit.database.AmbiguousTableNameException:我的集成测试抛出的 PROFIL

我正在使用以下表格(我创建了一个小型数据库,因为它太大了)

在此处输入图像描述

在我的集成测试中,我将 DBUnit 配置为使用以下数据源:

我还将 mainDataSource 指定为 PRIMARY,如下所示:

这是我得到的错误

知道如何排除 PROFIL 表或整个视图模式,因为我的 DBUnit 仍然可以看到它吗?