Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SpringExtension让我的测试与JUnit5一起工作?_Java_Spring_Spring Boot_Maven - Fatal编程技术网

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

Java 如何使用SpringExtension让我的测试与JUnit5一起工作?,java,spring,spring-boot,maven,Java,Spring,Spring Boot,Maven,我试图使用JUnit5获取测试,但是在自动连接的RESTTemplatebean上得到了NullPointerException 测试配置: import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; im

我试图使用JUnit5获取测试,但是在自动连接的RESTTemplatebean上得到了
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类: 米桑托普是正确的。 我必须修复
@Test
的导入路径,并从TestConfig.java中删除
restemplate
bean定义方法

修复#1-修复导入和使用RestTemplateAutoConfiguration: 更改
导入org.junit.Test
to
import org.junit.jupiter.api.Test

修复#2-修复导入并使用从RestConfig中删除bean: 更改
导入org.junit.Test
to
import org.junit.jupiter.api.Test


您可以尝试使用新的RestTemplate()而不是使用RestTemplateBuilder来创建bean吗?如果我这样做,它仍然会失败,因为它没有导入应用程序。yml configs。可能与您的实际错误无关,但是,在将测试类从JUnit4重构到JUnit5,但仍然从JUnit4包导入
@test
注释时,我遇到了不同类型的异常。使用
import org.junit.jupiter.api.Test
而不是
import org.junit.Test
,这就是问题所在。还必须从TestConfig中删除restTemplate方法。org.junit.Test中的注释是错误的。那是行不通的。。。
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);
    }
}
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);
    }
}
<?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)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {TestConfig.class, RestTemplateAutoConfiguration.class})
@Log4j2
@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();
//  }

}