从 JUnit4 迁移到 JUnit5 在 @Autowired 存储库上抛出 NullPointerException
Migrating from JUnit4 to JUnit5 throws NullPointerException on @Autowired repositories
我有一个非常简单的存储库测试,它在我使用时运行得很好
JUnit 的 4“@RunWith(SpringRunner.Class)”。当我尝试像提供的示例中那样使用“@ExtendWith”时,在尝试使用存储库时出现 NullPointerException。使用后一个注释时,“@Autowire”似乎没有注入存储库。这是 pom.xml 文件和堆栈跟踪:https://pastebin.com/4KSsgLfb
实体Class:
package org.tim.entities;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NonNull;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
@Data
public class ExampleEntity {
@Id
@Setter(AccessLevel.NONE)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@NonNull
private String name;
}
存储库Class:
package org.tim.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.tim.entities.ExampleEntity;
@Repository
public interface ExampleRepository extends JpaRepository<ExampleEntity, Long> {
}
测试Class:
package org.tim;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.tim.entities.ExampleEntity;
import org.tim.repositories.ExampleRepository;
@ExtendWith(SpringExtension.class)
@DataJpaTest
public class exampleTestClass {
@Autowired
private ExampleRepository exampleRepository;
@Test
public void exampleTest() {
exampleRepository.save(new ExampleEntity("name"));
}
}
在文档中说:
If you are using JUnit 4, don’t forget to also add @RunWith(SpringRunner.class) to your test, otherwise the annotations will be ignored. If you are using JUnit 5, there’s no need to add the equivalent @ExtendWith(SpringExtension) as @SpringBootTest and the other @…Test annotations are already annotated with it.
Testing Spring Boot Applications
所以尝试删除测试类中的@extendWith
您使用了错误的 @Test
注释。
当使用 SpringExtension
和 JUnit Jupiter (JUnit 5) 时,您必须使用 import org.junit.jupiter.api.Test;
而不是 import org.junit.Test;
。
我有一个非常简单的存储库测试,它在我使用时运行得很好 JUnit 的 4“@RunWith(SpringRunner.Class)”。当我尝试像提供的示例中那样使用“@ExtendWith”时,在尝试使用存储库时出现 NullPointerException。使用后一个注释时,“@Autowire”似乎没有注入存储库。这是 pom.xml 文件和堆栈跟踪:https://pastebin.com/4KSsgLfb
实体Class:
package org.tim.entities;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NonNull;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
@Data
public class ExampleEntity {
@Id
@Setter(AccessLevel.NONE)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@NonNull
private String name;
}
存储库Class:
package org.tim.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.tim.entities.ExampleEntity;
@Repository
public interface ExampleRepository extends JpaRepository<ExampleEntity, Long> {
}
测试Class:
package org.tim;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.tim.entities.ExampleEntity;
import org.tim.repositories.ExampleRepository;
@ExtendWith(SpringExtension.class)
@DataJpaTest
public class exampleTestClass {
@Autowired
private ExampleRepository exampleRepository;
@Test
public void exampleTest() {
exampleRepository.save(new ExampleEntity("name"));
}
}
在文档中说:
If you are using JUnit 4, don’t forget to also add @RunWith(SpringRunner.class) to your test, otherwise the annotations will be ignored. If you are using JUnit 5, there’s no need to add the equivalent @ExtendWith(SpringExtension) as @SpringBootTest and the other @…Test annotations are already annotated with it.
Testing Spring Boot Applications
所以尝试删除测试类中的@extendWith
您使用了错误的 @Test
注释。
当使用 SpringExtension
和 JUnit Jupiter (JUnit 5) 时,您必须使用 import org.junit.jupiter.api.Test;
而不是 import org.junit.Test;
。