spring-test 相关问题

`spring-test`是Spring Framework的测试模块,为JUnit和TestNG提供单元和集成测试支持,包括用于单元测试的各种模拟和Spring TestContext Framework以及用于集成测试的Spring MVC测试框架基于Spring的应用程序。

如何使用Spring Boot禁用Redis缓存?

使用 Spring Boot 2.1,我使用 Java 配置在配置文件中定义 RedisCacheManager bean。一切正常,但我有时想禁用它,例如在

回答 3 投票 0

SpringJUnitConfig配置类

当我试图理解@SpringJUnitConfig时我迷失了方向。我知道@SpringJUnitConfig是@ExtendWith(SpringExtension.class)和@ContextConfiguration的组合。然而我进不去……

回答 1 投票 0

测试 Spring Boot 缓存(咖啡因)

我的缓存配置如下; @配置 公共类缓存配置{ @豆 公共 CacheManager 缓存管理器(Ticker 代码){ CaffeineCache bookCache = buildCache("...

回答 1 投票 0

为什么Spring Boot 2升级后@SpringBootTest找不到任何bean?

我目前正在尝试将一个大型 Java 8 项目从 Spring Boot 1.5 升级到 Spring Boot 2.7(是的,我知道,已经很晚了)。我不明白的一件事是 SB2 se 中的 @SpringBootTest...

回答 1 投票 0

适用于 Azure 服务总线的 Spring Cloud Stream Binder 破坏了测试

添加依赖项 com.azure.spring spring-cloud-azure-stream-binder-servicebus 添加依赖项 <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-stream-binder-servicebus</artifactId> </dependency> 突然使我的 RestTemplate 客户端测试的 some 失败,并出现以下错误: java.lang.AssertionError: JSON path "$" Expected: (a collection containing <152> and a collection containing <153>) but: a collection containing <152> was String "<UnmodifiableRandomAccessList><item>152</item><item>153</item></UnmodifiableRandomAccessList>" 或 Expected :application/json Actual :application/xml;charset=UTF-8 我的测试如下: @Autowired private RestTemplate restTemplate; private MockRestServiceServer mockServer; @BeforeEach public void init() { mockServer = MockRestServiceServer.createServer(restTemplate); } public void test() throws Exception { mockServer.expect(requestTo(url)) .andExpect(method(HttpMethod.POST)) .andExpect(jsonPath("$", hasItems(152, 153))) .andRespond(withStatus(HttpStatus.OK) .contentType(MediaType.APPLICATION_JSON) .body("{}")); // actual code removed for brevity ResultDTO result = client.makeRestCall(Set.of(1L)); // beneath makes a RestTemplate call to the url assertNotNull(result); } 奇怪的是,测试类中只有部分测试失败,即该类有 8 个测试,其中 4 个测试失败,且没有明显的原因。我看到的是,我删除了 Azure 依赖项,测试通过了。有谁知道这种依赖会带来什么可能破坏我的代码? 似乎这是已知问题。问题在于 Azure 依赖项引入了 jackson-dataformat-xml 依赖项,它破坏了其余控制器响应的格式(使它们成为 XML 而不是默认的 JSON)。解决该问题的简单方法是排除依赖项: <exclusion> <artifactId>jackson-dataformat-xml</artifactId> <groupId>com.fasterxml.jackson.dataformat</groupId> </exclusion> 该线程提到依赖项已变为可选,但我找不到发生这种情况的版本。 您必须通过添加扩展来使您的 spring-cloud-azure-stream-binder-servicebus 依赖项如下: <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-stream-binder-servicebus</artifactId> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> </exclusion> </exclusions> </dependency>

回答 2 投票 0

“找到了不止一行具有给定标识符的行”。问题:它不是一个标识符

这是我的测试 @DataJpaTest @ActiveProfiles(“测试”) @Sql( 执行阶段 = Sql.ExecutionPhase.BEFORE_TEST_METHOD, 脚本 = {“/schema-h2.sql”,“/data-h2.sql...

回答 1 投票 0

WebMvcTest,从测试中排除Repository接口

设置 在我的 Spring Boot 2.7 应用程序中,我使用 ViewControllerRegistry 注册了一个简单的 ViewController,因此起始页没有专用的控制器: @覆盖 公共空间

回答 1 投票 0

运行集成测试时,Spring Boot 不会解码 URL 参数中的特殊字符

我在 Spring Boot RestController 中有一个 GET 端点,它接受电子邮件作为 URL 参数(我知道这不是一个好主意,但我现在无法更改它)。 电子邮件控制器.java 爸...

回答 1 投票 0

将 JUnit 测试套件添加到 Spring MVC 应用程序

我想将 JUnit 测试套件添加到已经运行的 spring mvc 应用程序中。 我特别想测试控制器 API。 您可以推荐一个特定的程序吗? 我不想...

回答 1 投票 0

Spring 测试 @Sql 抛出“未终止的美元报价开始......预计终止 $$”

我正在使用测试类(FoodE2eTest)测试我的 Spring Boot API,并且我有一个单独的 SQL 脚本文件(setup-test-schema.sql),我想在测试开始之前运行它来设置架构、表、还有...

回答 1 投票 0

Spring Security 6 中用于自动化测试的标头配置

我使用以下配置代码使用 Spring Security 进行自动化测试: @测试配置 公共静态类安全配置{ @豆 公开

回答 2 投票 0

测试驱动 Hystrix 断路器配置

我们的应用程序是通过使用 Hystrix 实现断路器模式以抗脆弱方式编写的。 整个应用程序是使用测试驱动实践创建的,但停留在这一点上

回答 1 投票 0

具有修改后的查询的 JPA 存储库。如何测试?

我没有经验。我开始学习测试。这个存储库正确吗?如何测试呢? 公共类 Person 扩展 Auditable 实现可序列化 { @ID @Tsid 优先...

回答 1 投票 0

Spring 静态上下文访问器和集成测试

我们有一个 spring 组件,它将应用程序上下文设置为静态字段。然后可以从应用程序的其他部分访问该静态字段。我知道不应该使用静态,但是有些......

回答 2 投票 0

如何在我的主测试课中模拟第三课

我的测试课如下。我已经使用 @BeforeEach 注释和 init() 方法为每个测试初始化了必要的对象。 @ExtendWith(MockitoExtension.class) 公共类 TokenServiceTest { @莫...

回答 1 投票 0

Spring 测试映射器返回 Null

我正在为我的后端项目编写测试单元。我还使用 ModelMapper 来实现地图功能。但是当我在管理器 ProductManager 的测试方法中编写 getAll() 或 update() 等时,Spring 说 组织。

回答 1 投票 0

如何模拟配置类进行测试

我面临着NPE。 我正在为我的证书课程编写测试,但遇到 NPE 错误。这是我的测试课 @ExtendWith(MockitoExtension.class) 公共类 CredentialServiceTest { @...

回答 1 投票 0

在 SpringBoot 中填充 MongoDb TestContainer 进行集成测试

我的问题类似于在 SpringBoot 集成测试中使用 TestContainers 填充数据库,但我有一个 mongo db 测试容器,如下所示: @容器 私有静态 MongoDBContainer

回答 3 投票 0

如何通过 SpringBootTest 调试 Spring Boot 应用程序

我是 Spring Boot 的新手,我真的很喜欢它,尤其是在消除样板代码方面。 我创建了一个测试类来测试我的 NBRController: @RunWith(SpringRunner.class) @SpringBoo...

回答 6 投票 0

多个测试应用程序上下文侦听竞争消费者队列会导致间歇性测试失败

我正在测试一个 JMSInboundGateway,它监听 Apache Artemis 队列(竞争消费者)。我的测试向 Artemis 服务器发送一条消息并模拟目标服务。如果...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.