【问题标题】:NullPointerException in the BeforeClass (spring boot)BeforeClass 中的 NullPointerException (spring boot)
【发布时间】:2017-03-10 15:53:34
【问题描述】:

我想在所有测试之前在我的数据库中插入一次。这就是我在 setUp() 方法中尝试做的事情。但是得到了空指针。 Spring Boot 版本是 1.5.1.RELEASE。有任何想法吗? 这是来自我的测试课:

@Autowired
    private static UserRepository userRepository;

    @BeforeClass
    public static void setUp() {
        User user = User.
                .builder()
                .id(10)
                .name("John")
                .build();

        userRepository.deleteAll(); //NullPointerException at this step
        userRepository.save(user);
    }

这是我的实体类:

@Entity
@Table(schema="test", name = "TBL_USERS")
@AllArgsConstructor
@Builder
@Data public class User implements Persistable<String>{

    @Id
    @Column(name = "ID", columnDefinition = "decimal")
    private String id;

    @Column(name = "NAME", columnDefinition = "char", nullable = false)
    private String name;

    ...
    }

这是界面:

public interface UserRepository extends JpaRepository<User, String> {}

堆栈跟踪:

java.lang.NullPointerException
    at com.company.myapp.get.GetUserTest.setUp(GetUserTest.java:58)
    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:497)
    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.RunBefores.evaluate(RunBefores.java:24)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

【问题讨论】:

  • 注入不适用于static 参数,因此永远不会注入,即使它被允许也不会在@BeforeClass 中可用。
  • 好的。那么在这种情况下,@BeforeClass 的合适替代品是什么?

标签: spring-boot junit nullpointerexception automated-tests


【解决方案1】:

简答:不能注入静态UserRepository (@Autowired)

请阅读这里Reason we could not inject static field

【讨论】:

    【解决方案2】:

    在调用 deleteAll() 之前,您似乎没有将 userRepository 初始化为 beforeClass 方法中的任何内容。这就是你获得 NPE 的原因。

    【讨论】:

      【解决方案3】:

      你的测试类是用@RunWith(SpringRunner.class)注解的吗?

      【讨论】:

      • 我从一个抽象类扩展而来,它有这个注解@RunWith(SpringRunner.class) @SpringBootTest(classes = MyServiceStarter.class) public abstract class AbstractIntegrationTest { ... }
      • 可以看看JPA切片测试:zoltanaltfatter.com/2016/04/16/…
      【解决方案4】:

      你需要做这样的事情..

      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class XXXTest {
      
          @Autowired
          private UserRepository userRepository;
      
          @Before
              public void setUp() {
                  ......
          }
      

      【讨论】:

      • 谢谢。但在这种情况下,setUp() 将在每次测试之前执行。我希望它在所有测试之前只运行一次。
      猜你喜欢
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-09
      • 2018-11-12
      • 2021-12-14
      • 2015-08-04
      相关资源
      最近更新 更多