@SpringBootTest 不工作(执行抛出 NullPointerException)
@SpringBootTest not working (execution throwing NullPointerException)
当我使用 @SpringBootTest
注释创建集成测试时,我的 Spring 引导应用程序未加载 Spring 上下文 class。
如何解决这个问题?
我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/>
</parent>
<groupId>br.com.example</groupId>
<artifactId>authorization-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>authorization-server</name>
<description>Authorization Server OAuth2</description>
<packaging>jar</packaging>
<properties>
<docker.repository>docker-repo.example.com.br</docker.repository>
<docker.buildArg.appId>${project.artifactId}-${project.version}.jar</docker.buildArg.appId>
<spring-jdbc.version>5.2.16.RELEASE</spring-jdbc.version>
<oauth.version>2.3.7.RELEASE</oauth.version>
<jwt.version>1.0.11.RELEASE</jwt.version>
<opentracing-spring-jaeger>3.3.1</opentracing-spring-jaeger>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-jdbc.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${oauth.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-jaeger-web-starter</artifactId>
<version>${opentracing-spring-jaeger}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*IT*.java</include>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/resources</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Version>${project.version}</Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.15.16</version>
<configuration>
<images>
<image>
<name>${docker.repository}/${project.artifactId}:${project.version}</name>
<build>
<from>${docker.repository}/my-image:0.0.1</from>
<maintainer>app@example.com.br</maintainer>
<env>
<APP_NAME>${project.build.finalName}.jar</APP_NAME>
</env>
<tags>
<tag>${project.version}</tag>
</tags>
<ports>
<port>8080</port>
</ports>
<assembly>
<user>app:app:root</user>
<mode>dir</mode>
<basedir>/</basedir>
<inline>
<fileSets>
<fileSet>
<directory>${project.basedir}/target</directory>
<outputDirectory>/home/app</outputDirectory>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
</fileSets>
</inline>
</assembly>
</build>
</image>
</images>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</reporting>
</project>
我的application-test.properties
:
# DATABASE CONFIGURATION
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
# POOL CONNECTIONS DATABASE
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=10000
# JPA CONFIGURATION
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
我的存储库class:
@Repository
public class UserJdbcRepository {
private final NamedParameterJdbcTemplate jdbcTemplate;
public UserJdbcRepository(NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public Optional<User> findByDocument(String document) {
// implementation
}
}
我的测试class:
@ActiveProfiles("test")
@SpringBootTest
public class UserJdbcRepositoryIT {
@Autowired
private UserJdbcRepository subject;
@Test
public void test() {
subject.findByDocument("000.000.001-91"); // subject throwing NullPointerException
}
}
我认为存储库没有正确自动连接。
如果您使用的是 JUnit 4
</p>
<pre><code>@ActiveProfiles("test")
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserJdbcRepositoryIT {
@Autowired
private UserJdbcRepository subject;
@Test
public void test() {
subject.findByDocument("000.000.001-91");
}
}
如果您使用的是 JUnit 5
<pre><code>@ActiveProfiles("test")
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class UserJdbcRepositoryIT {
@Autowired
private UserJdbcRepository subject;
@Test
public void test() {
subject.findByDocument("000.000.001-91");
}
}
当我使用 @SpringBootTest
注释创建集成测试时,我的 Spring 引导应用程序未加载 Spring 上下文 class。
如何解决这个问题?
我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/>
</parent>
<groupId>br.com.example</groupId>
<artifactId>authorization-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>authorization-server</name>
<description>Authorization Server OAuth2</description>
<packaging>jar</packaging>
<properties>
<docker.repository>docker-repo.example.com.br</docker.repository>
<docker.buildArg.appId>${project.artifactId}-${project.version}.jar</docker.buildArg.appId>
<spring-jdbc.version>5.2.16.RELEASE</spring-jdbc.version>
<oauth.version>2.3.7.RELEASE</oauth.version>
<jwt.version>1.0.11.RELEASE</jwt.version>
<opentracing-spring-jaeger>3.3.1</opentracing-spring-jaeger>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-jdbc.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${oauth.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-jaeger-web-starter</artifactId>
<version>${opentracing-spring-jaeger}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*IT*.java</include>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/resources</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Version>${project.version}</Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.15.16</version>
<configuration>
<images>
<image>
<name>${docker.repository}/${project.artifactId}:${project.version}</name>
<build>
<from>${docker.repository}/my-image:0.0.1</from>
<maintainer>app@example.com.br</maintainer>
<env>
<APP_NAME>${project.build.finalName}.jar</APP_NAME>
</env>
<tags>
<tag>${project.version}</tag>
</tags>
<ports>
<port>8080</port>
</ports>
<assembly>
<user>app:app:root</user>
<mode>dir</mode>
<basedir>/</basedir>
<inline>
<fileSets>
<fileSet>
<directory>${project.basedir}/target</directory>
<outputDirectory>/home/app</outputDirectory>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
</fileSets>
</inline>
</assembly>
</build>
</image>
</images>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</reporting>
</project>
我的application-test.properties
:
# DATABASE CONFIGURATION
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
# POOL CONNECTIONS DATABASE
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=10000
# JPA CONFIGURATION
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
我的存储库class:
@Repository
public class UserJdbcRepository {
private final NamedParameterJdbcTemplate jdbcTemplate;
public UserJdbcRepository(NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public Optional<User> findByDocument(String document) {
// implementation
}
}
我的测试class:
@ActiveProfiles("test")
@SpringBootTest
public class UserJdbcRepositoryIT {
@Autowired
private UserJdbcRepository subject;
@Test
public void test() {
subject.findByDocument("000.000.001-91"); // subject throwing NullPointerException
}
}
我认为存储库没有正确自动连接。
如果您使用的是 JUnit 4
</p>
<pre><code>@ActiveProfiles("test")
@SpringBootTest
@RunWith(SpringRunner.class)
public class UserJdbcRepositoryIT {
@Autowired
private UserJdbcRepository subject;
@Test
public void test() {
subject.findByDocument("000.000.001-91");
}
}
如果您使用的是 JUnit 5
<pre><code>@ActiveProfiles("test")
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class UserJdbcRepositoryIT {
@Autowired
private UserJdbcRepository subject;
@Test
public void test() {
subject.findByDocument("000.000.001-91");
}
}