英文:
Keep getting nullpointerexception using Spring JPA derived query
问题
我的实体类如下所示:
@Entity
public class User {
@Id
@Column(length = 50)
private String email;
@Column(length = 50, nullable=false)
private String name;
@Column(length = 50, nullable=false)
private String password;
}
我的仓库类如下所示:
public interface UserRepository extends CrudRepository<User, String>{
User findByEmail(String email);
}
我正在使用类似以下的 Junit 测试:
public class UserDAOTest {
@Autowired
private UserRepository userRepository;
@Test
@Transactional
public void validateUserTest1() {
User u = userRepository.findByEmail("jim.gordon@gmail.com");
}
}
在课程中看到类似的内容后,我自己创建了这个用户类。然而,我无法使其正常工作。我是否遗漏了什么?我尝试了许多不同的方法并进行了搜索,但无法找出为什么我一直收到空指针异常。
从 Junit 测试案例中,完整的堆栈跟踪如下所示:
java.lang.NullPointerException
at com.BarShelf.tests.UserDAOTest.validateUserTest1(UserDAOTest.java:22)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
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:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
英文:
My entity class looks like this
@Entity
public class User {
@Id
@Column(length = 50)
private String email;
@Column(length = 50, nullable=false)
private String name;
@Column(length = 50, nullable=false)
private String password;
My Repository looks like this
public interface UserRepository extends CrudRepository<User, String>{
User findByEmail(String email);
}
and I am using a Junit test like this
public class UserDAOTest {
@Autowired
private UserRepository userRepository;
@Test
@Transactional
public void validateUserTest1() {
User u = userRepository.findByEmail("jim.gordon@gmail.com");
}
}
I have made this user class on my own after seeing something similar in class. However, I can't get this to work. Am I missing out on something here? I have tried a bunch of different things and googling this but can't find why I keep getting nullpointerexception.
The full stack trace looks like this from the junit test case.
java.lang.NullPointerException
at com.BarShelf.tests.UserDAOTest.validateUserTest1(UserDAOTest.java:22)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
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:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
答案1
得分: 1
默认情况下,Junit 测试类不了解 Spring 上下文。因此,在您的代码中,@Autowired
注解会被忽略,userRepository
保持为空。
为了使 JUnit 使用 Spring 上下文,您需要在测试类上添加注解:
如果您使用 Spring Boot 并且有一个主配置类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { YourConfigurationClass.class })
public class UserDAOTest {
...
}
如果您使用 XML 上下文文件:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/your-spring-config-file.xml")
public class UserDAOTest {
...
}
英文:
By default, Junit test classes do not know of the spring context. Hence, in your code, the @Autowired
annotation is disregarded and the userRepository
remains null.
In order to have JUnit use the spring context you have to add annotations to your test class :
If you use spring boot and have a main configuration class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { YourConfigurationClass.class })
public class UserDAOTest {
...
If you use xml context files:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/your-spring-config-file.xml")
public class UserDAOTest {
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论