不断获得使用Spring JPA派生查询时的空指针异常。

huangapple go评论79阅读模式
英文:

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&lt;User, String&gt;{
	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(&quot;jim.gordon@gmail.com&quot;);
		}

}

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(&quot;/your-spring-config-file.xml&quot;)
public class UserDAOTest {
    ...

huangapple
  • 本文由 发表于 2020年8月29日 07:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63642054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定