spring-test 相关问题

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

使用 H2 脚本初始化设置 Spring 测试环境在插入简单 SQL 数据时失败:“唯一索引或主键冲突”

我有一些旧的 MySQL 脚本,我已将其迁移到 H2,现在我想用该数据初始化测试数据库。 首先是我的 src/test/resources/application.yaml: 春天: 数据源: 网址:jd...

回答 2 投票 0

在Spring中使用@TestPropertySource时发生绑定异常

我正在尝试加载 application-test.yaml 文件进行测试 这是我的代码, @Provider(“提供者”) @PactBroker //@ActiveProfiles({"测试"}) @TestPropertySource("类路径:

回答 1 投票 0

在 Spring Boot 应用程序上为我的服务实现生成单元测试

经过数小时的尝试和失败后,我来找您希望能找到解决方案。 我正在努力为我的 Spring Boot 应用程序进行单元测试。我正在使用mockito和Junit 5。 我的建筑就像...

回答 3 投票 0

我应该在每个类上使用@DirtiesContext吗?

我有几个junit测试, @ContextConfiguration(locations = { "文件:../business/src/test/resources/application-context-test.xml", “文件:src/main/webapp/WEB-INF/confA.xml&qu...

回答 1 投票 0

如何在Springboot 3.1.10中启用单元测试的DEBUG级别?

我需要全局启用调试模式,因此应用程序类的输出会记录在应用程序和单元测试中。 我在 src\main 中启用了 DEBUG 模式 esources application.properties 和它...

回答 1 投票 0

为 JpaRepository 创建测试 bean

我有一个 Spring Boot 应用程序,正在从使用 SessionFactory 的旧式 Hibernate DAO 迁移到 Spring 的 JpaRepositories。 我面临的问题是,在单元测试中,我有一些

回答 1 投票 0

模拟弹簧组件

我正在使用 Mockito 来模拟 spring beans。 当我模拟接口时它工作得很好。 在我们的应用程序中,很少有 @Component bean 没有实现任何接口。 当我试图嘲笑这样的人时

回答 2 投票 0

将 Springframework-security 从 6.1.1 升级到 6.2.2 中断测试

我在 org.springframework.security 代码深处得到了这个异常 java.lang.IllegalArgumentException:无法在 servlet 上下文中找到 servlet [] 什么可能导致这种情况? 我打电话给@RestControl...

回答 1 投票 0

如何通过unitTest中的每个测试覆盖属性

我的单元测试中有下一个配置: @SpringBootTest(webEnvironment = WebEnvironment.NONE) @TestPropertySource(locations="classpath:itest.properties", 属性 = "服务器。

回答 2 投票 0

模拟数据库和来自端点的响应

我想为此代码创建一个 JUnit 测试: 私人服务列表服务; @RequestMapping(方法 = {RequestMethod.GET}, 路径 = {"id/{id}"}) 公共响应实体 我想为此代码创建一个 JUnit 测试: private Service listsService; @RequestMapping(method = {RequestMethod.GET}, path = {"id/{id}"}) public ResponseEntity<Object> find(@PathVariable String id) { LookupResponse lookupResponse = listsService.findById(id); return new ResponseEntity<>(lookupResponse, HttpStatus.OK); } ................. @Override public LookupResponse findById(String id) { Optional<Lists> list = ListsRepository.findById(id); if(list.isPresent()) { Lists lists = binList.get(); LookupResponse response = LookupResponse.builder() .countryCode(lists.getCountry()) .category(lists.getType()) .build()) .build(); return response; } return null; } 我试过这个: public class ControllerTest { @MockBean private ListsService listsService; @Autowired private MockMvc mockMvc; @Test void justAnExample() { LookupResponse lookupResponse = LookupResponse.builder() .type("123456") .build(); Mockito.when(listsService.findById(Mockito.anyString())).thenReturn(lookupResponse); } } 当我运行代码时,JUnit 代码正在向数据库发出请求。我如何模拟连接? 为了使用 @MockBean,您需要使用 或 @ExtendWith(SpringExtension.class) 或 @RunWith(SpringRunner.class)(如果使用 JUnit 4)注释测试类。您还可以使用 @SpringJUnitConfig,它将 @ExtendWith(SpringExtension.class) 与 @ContextConfiguration 结合在一起,请参阅 https ://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/ContextConfiguration.html 其余部分保持不变: @ExtendWith(SpringExtension.class) public class ControllerTest { @MockBean private ListsService listsService; @Autowired private MockMvc mockMvc; @Test void justAnExample() { LookupResponse lookupResponse = LookupResponse.builder() .type("123456") .build(); Mockito.when(listsService.findById(Mockito.anyString())).thenReturn(lookupResponse); } 以下是测试中 WebClient 模拟的示例: https://www.baeldung.com/spring-mocking-webclient 对于测试中的 RestTemplate 模拟: https://www.baeldung.com/spring-mock-rest-template 要模拟数据库,这取决于您想要实现什么。 您可以使用模拟数据库存储库 Mockito.when(mock.findById(id)).thenReturn(mockedresponse) 此外,还可以覆盖以下属性 src/main/resources/application.properties 与 src/test/resources/application.properties 并使 Spring 测试上下文连接到 H2 数据库 请阅读示例:https://www.baeldung.com/spring-testing-separate-data-source 您正在使用 ListsService 模拟 @MockBean,但没有使用 @Autowired 或 @InjectMocks 将其注入到控制器中。结果,控制器仍然使用真实的ListsService而不是模拟的,因此进行真正的数据库调用。 要解决此问题,请尝试使用 @Autowired 或 @InjectMocks 注释将模拟的 ListsService 注入到控制器中。 它看起来与此代码类似 @Mock ListsRepository listsRepository; @InjectMocks ListsService listsService = new ListsServiceImpl(); @Test void justAnExample() { LookupResponse lookupResponse = LookupResponse.builder() .type("123456") .build(); when(listsRepository.findById(anyString())).thenReturn(Optional.of(new Lists())); when(listsService.findById(anyString())).thenReturn(lookupResponse); mockMvc.perform(get("/id/1")) .andExpect(status().isOk()) .andExpect(jsonPath("$.type", equalTo("123456"))); } 要模拟与数据库的连接,可以使用 Spring Boot 提供的 @MockBean 注解,它允许您在应用程序上下文中模拟 bean。在您的情况下,您可以模拟 ListsRepository bean,它负责访问数据库。 以下是如何操作的示例: @RunWith(SpringRunner.class) @WebMvcTest(controllers = Controller.class) public class ControllerTest { @Autowired private MockMvc mockMvc; @MockBean private ListsService listsService; @MockBean private ListsRepository listsRepository; @Test public void testFind() throws Exception { // create a LookupResponse object to be returned by the mock service LookupResponse lookupResponse = LookupResponse.builder() .countryCode("US") .category("foo") .build(); // configure the mock service to return the LookupResponse object when(listsService.findById("1")).thenReturn(lookupResponse); // configure the mock repository to return a Lists object when findById is called with "1" Lists lists = new Lists(); lists.setCountry("US"); lists.setType("foo"); when(listsRepository.findById("1")).thenReturn(Optional.of(lists)); // perform the HTTP GET request mockMvc.perform(get("/id/1")) .andExpect(status().isOk()) .andExpect(content().json("{countryCode: 'US', category: 'foo'}")); } } // Fixed by FixThisCode.com 在此示例中,我们使用 @WebMvcTest 注解来测试 Controller 类,这意味着仅测试应用程序的 Web 层,而不是整个上下文。我们还使用 @MockBean 注释来模拟 ListsService 和 ListsRepository beans。 在 testFind() 方法中,我们首先创建一个由模拟服务返回的 LookupResponse 对象。然后,我们将模拟服务配置为在使用参数“1”调用其 findById() 方法时返回该对象。 我们还配置模拟存储库,以在使用参数“1”调用其 findById() 方法时返回 Lists 对象。服务将使用该对象来创建 LookupResponse 对象。 最后,我们使用 MockMvc 对象对 /id/1 端点执行 HTTP GET 请求,并验证响应的状态代码为 200 以及与预期 LookupResponse 对象匹配的 JSON 正文。

回答 4 投票 0

当我尝试使用 Testcontainers 测试多个 TestClass 时,其他的总是失败

问题是: 当我尝试使用 singleton-testcontainer 时,我的所有测试类首先因断言而失败。 这意味着,即使我在日志中看到我的场景完全成功,断言

回答 1 投票 0

org.springframework.web.reactive.function.UnsupportedMediaTypeException:不支持内容类型“application/json;charset=UTF-8”

当我的代码如下时出现错误: @测试 公共无效 getTemplateByIdTest() 抛出异常 { client.get().uri("/template/getTemplate/7") 。交换() .expectStat...

回答 7 投票 0

如何在 Spring Integration 测试之间删除内存中的 h2db?

我在 Spring Web 应用程序中使用 Liquibase。我有一堆实体,在每个实体(如用户、帐户、发票、许可证等)的集成测试中对 REST API 进行了数百次测试...

回答 4 投票 0

在Spring配置中实现带有可读属性的自定义注解(测试)

我熟悉复合注释。然而,即使经过一些研究,它们似乎还不足以满足我的特定需求。 一般情况 我想创建一个注释来测试它,戴上...

回答 1 投票 0

如何模拟环境接口?

我正在尝试测试我的服务,如下所示: 导入 org.springframework.core.env.Environment; @服务 公共类我的服务{ @Autowired环境环境; ... ... } 我如何模拟环境

回答 4 投票 0

在 JUnit 集成测试中启动 Spring Integration 轮询器的简洁方法

我有一个 Spring Integration 流程,它利用 MessageSource 定期填充来自数据库查询的消息(不,我不打算使用 Spring Integration JPA/JDBC) @豆 公开

回答 1 投票 0

spring批处理+spring boot+java配置+测试用例

spring批处理+spring boot+java配置+测试用例 我遵循了下面的示例,我的用例与此匹配,我已经使用类似的设置实现了该项目,一切正常。 我结构...

回答 3 投票 0

Spring boot测试@Transactional不保存

我正在尝试使用 Spring Boot Test 进行简单的集成测试,以测试 e2e 用例。我的测试不起作用,因为我无法使存储库保存数据,我想我有一个

回答 7 投票 0

MockMvc 不再使用 Spring Boot 2.2.0.RELEASE 处理 UTF-8 字符

在我升级到新发布的 Spring Boot 2.2.0.RELEASE 版本后,我的一些测试失败了。看来 MediaType.APPLICATION_JSON_UTF8 已被弃用并且不再返回...

回答 11 投票 0

Spring MVC 测试中的快照测试

在Spring MVC测试(MockMvc)中是否有任何现有的解决方案可以进行快照测试[1]? 就像是: this.mockMvc.perform(get("/users") .andExpect(status().isOk()) .andExpect(内容().

回答 2 投票 0

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