0

I want to make an insert in my DB before all tests just once. That's what I'm trying to do in the setUp() method. But got nullpointer. Spring boot version is 1.5.1.RELEASE. Any ideas? This is from my test class:

@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);
    }

This is my entity class:

@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;

    ...
    }

This is the interface:

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

The stack trace:

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)
IKo
  • 4,998
  • 8
  • 34
  • 54
  • Injection doesn't work for `static` arguments and as such is never injected and even if it would be allowed wouldn't be available in a `@BeforeClass`. – M. Deinum Mar 10 '17 at 16:03
  • ok. so what is an appropriate replacement for @BeforeClass in such a case? – IKo Mar 10 '17 at 16:11

4 Answers4

1

Short answer: You can not inject static UserRepository (@Autowired)

Please read here Reason we could not inject static field

Community
  • 1
  • 1
Lam Le
  • 809
  • 8
  • 14
0

It doesn't look like you initialized userRepository to anything in your beforeClass method before calling deleteAll() on it. That's why you got your NPE.

0

Is your test class annotated with @RunWith(SpringRunner.class)?

mixiul__
  • 395
  • 1
  • 2
  • 12
  • I extend it from an abstract class which has this annotation `@RunWith(SpringRunner.class) @SpringBootTest(classes = MyServiceStarter.class) public abstract class AbstractIntegrationTest { ... }` – IKo Mar 10 '17 at 16:00
  • Could look at JPA slice testing: http://zoltanaltfatter.com/2016/04/16/trying-out-spring-boot-1.4.0-new-features-and-enhancements/ – mixiul__ Mar 10 '17 at 16:01
0

You need to do something like this..

@RunWith(SpringRunner.class)
@SpringBootTest
public class XXXTest {

    @Autowired
    private UserRepository userRepository;

    @Before
        public void setUp() {
            ......
    }
  • Thanks. but in such a case the setUp() will be executed before each test. And I want it to be run just once before all tests. – IKo Mar 13 '17 at 09:03