gpt4 book ai didi

java - 如何使用 SpringExtension 让我的测试与 JUnit5 一起工作?

转载 作者:行者123 更新时间:2023-12-01 18:01:08 27 4
gpt4 key购买 nike

我尝试使用 JUnit5 进行测试,但在自动连接的 RestTemplate bean 上遇到 NullPointerException

测试配置:

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class TestConfig {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}

使用 JUnit4 类:

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import lombok.extern.log4j.Log4j2;

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {TestConfig.class, RestTemplateAutoConfiguration.class})
@Log4j2
public class Test {

@Value("${service.url.base}/security-marking/count")
private String countUrl;

@Autowired
private RestTemplate restTemplate;

@Test
public void testCount() throws Exception {
LOG.info(countUrl);
ResponseEntity<String> response = restTemplate.getForEntity(countUrl, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(NumberUtils.isDigits(response.getBody()));
assertTrue(NumberUtils.toInt(response.getBody()) >= 0);
}
}

调用restTemplate.getForEntity(...)时失败并出现NullPointerException:

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.commons.lang3.math.NumberUtils;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.*.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;

import lombok.extern.log4j.Log4j2;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {TestConfig.class, RestTemplate.class})
@Log4j2
@AutoConfigurationPackage
public class Test {

@Value("${service.url.base}/bob/count")
private String countUrl;

@Autowired
private RestTemplate restTemplate;

@Test
public void testCount() throws Exception {
LOG.info(countUrl);
ResponseEntity<String> response = restTemplate.getForEntity(countUrl, String.class);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(NumberUtils.isDigits(response.getBody()));
assertTrue(NumberUtils.toInt(response.getBody()) >= 0);
}
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath />
</parent>

<artifactId>api-integration</artifactId>
<groupId>group.id</groupId>
<name>Integration Test</name>
<description>Integration Test</description>
<version>0.0.1-SNAPSHOT</version>

<properties>
<main.basedir>${basedir}/..</main.basedir>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- HELPERS -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

堆栈跟踪:

java.lang.NullPointerException
at Test.testCount(Test.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

最佳答案

米桑托普是正确的。我必须修复 @Test 的导入路径,并从 TestConfig.java 中删除 restTemplate bean 定义方法。

修复 #1 - 修复导入和使用 RestTemplateAutoConfiguration:

import org.junit.Test;更改为import org.junit.jupiter.api.Test;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {TestConfig.class, RestTemplateAutoConfiguration.class})
@Log4j2

修复 #2 - 修复导入并使用从 RestConfig 中删除 bean:

import org.junit.Test;更改为import org.junit.jupiter.api.Test;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {TestConfig.class, RestTemplate.class})
@Log4j2
public class SecurityMarkingsTest {
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class TestConfig {

// @Bean
// public RestTemplate restTemplate(RestTemplateBuilder builder) {
// return builder.build();
// }

}

关于java - 如何使用 SpringExtension 让我的测试与 JUnit5 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60624873/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com