`spring-test`是Spring Framework的测试模块,为JUnit和TestNG提供单元和集成测试支持,包括用于单元测试的各种模拟和Spring TestContext Framework以及用于集成测试的Spring MVC测试框架基于Spring的应用程序。
我想用Junit5和Mockito编写控制器测试。我添加了存储库和服务测试,但是当我想尝试控制器测试时,jwt 服务抛出异常,我无法弄清楚。你可以吗
“org.springframework.boot.web.server.LocalServerPort”已弃用
自 Spring boot 2.7.1 起,@LocalServerPort(在 org.springframework.boot.web.server.LocalServerPort 包中)已被弃用。 我可以用什么来代替这个注释?
Spring ReflectionTestUtils 未设置原始数据类型字段
我使用 ReflectionTestUtils 在我的服务类中设置 int 字段来测试该类。 我的服务类别如下: @服务 公共类 SampleService { @Value("${app.count}") 私有整数计数; ...
我正在尝试为包含 Lambda 表达式的构造函数编写 Junit @成分 公共类子类扩展父类{ @Autowired 孩子( SessionFactory sessionFactory,属性 prop...
MockMvc 集成测试 - 模拟 Azure ShareFileClient 和 StorageFileInputStream 无限期挂起
我在这个测试中遇到了问题,在调用 return 后,此端点会挂起(也尝试使用 WebTestClient 并引发超时,因此问题与模拟 mvc 无关),更改了一些配置...
从JpaRepository获取数据时没有从WebTestClient收到数据
我有一个简单的集成测试用例,我想在其中找到特定的数据。 首先,我使用 .save() 插入数据。虽然它工作成功(正如我调试它以及 this.dataRepository.findAl...
任务“:compileTestJava”执行失败。 Java编译器不可用
我第一次尝试运行 Spring 测试。写完它们并在终端中输入 .\gradelw.bat test 后,我收到这样的错误 - 任务 ':compileTestJ...
Spring Boot 测试默认配置警告“Parameter 'systemProperties' is deprecated”是什么意思?
在Spring Boot测试中,当我用@SpringBootTest注解时, 我收到警告: 参数“systemProperties”已弃用:请改用 systemPropertyVariables。 这是为什么?我该如何解决它? 这...
我的测试类中有以下片段 @ContextConfiguration(初始化器=TestingV2.Initializer.class) 公共类TestingV2扩展BaseTest0IT { 私有静态流 getParams() {...
排除 Spring Boot 测试的 spring.factories 依赖项
有一个父项目 A,使用 A 依赖项的项目我们将其称为 B。 除了测试用例之外,一切都工作正常。我正在使用 SpringBootTest 来获取所有
我正在创建一个 Spring MVC 控制器测试。编译器显示下面以粗体显示的方法的错误。我的代码中是否缺少某些库或某些内容?有什么建议么? 我正在使用以下
对于上下文,我有 2 个服务类, 价值服务和应用服务, 在 ValService 中我有一个方法 validate ,它调用 AppService 中的方法 字符 c = appService.sendToAPI(listOfString, string...
在每个 Spring Boot @Test 上覆盖单个 @Configuration 类
在我的 Spring Boot 应用程序上,我想在我的所有测试中仅使用测试配置覆盖我的 @Configuration 类之一(特别是我的 @EnableAuthorizationServer @Configuration 类)...
从元注释重写 ContextHierarchy 和 ContextConfiguration
我们有使用元注释的测试类: @WebAppConfiguration @ContextHierarchy({ @ContextConfiguration(locations = {"/web/WEB-INF/spring.xml" }, name = "parent"), @ContextConfigurati...
Spring 测试 - 从测试类创建第一个 bean,然后创建真正的代码
问题:我如何确保测试类中的bean首先在Spring中创建? 情况: 我有一个在运行时提供的数据源,它是通过 jndi 获取的。 在编译时...
使用 Spring Boot 2.1,我使用 Java 配置在配置文件中定义 RedisCacheManager bean。一切正常,但我有时想禁用它,例如在
当我试图理解@SpringJUnitConfig时我迷失了方向。我知道@SpringJUnitConfig是@ExtendWith(SpringExtension.class)和@ContextConfiguration的组合。然而我进不去……
我的缓存配置如下; @配置 公共类缓存配置{ @豆 公共 CacheManager 缓存管理器(Ticker 代码){ CaffeineCache bookCache = buildCache("...
为什么Spring Boot 2升级后@SpringBootTest找不到任何bean?
我目前正在尝试将一个大型 Java 8 项目从 Spring Boot 1.5 升级到 Spring Boot 2.7(是的,我知道,已经很晚了)。我不明白的一件事是 SB2 se 中的 @SpringBootTest...
适用于 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>