Java 服务类Spring引导中出现空指针异常
你好。。我正在尝试创建一个spring boot web应用程序,在该应用程序中,我必须在spring boot的用户详细信息中找到用户,但在使用测试时出现错误。以下是我的配置: 用户类别:Java 服务类Spring引导中出现空指针异常,java,spring,spring-boot,junit,spring-data-jpa,Java,Spring,Spring Boot,Junit,Spring Data Jpa,你好。。我正在尝试创建一个spring boot web应用程序,在该应用程序中,我必须在spring boot的用户详细信息中找到用户,但在使用测试时出现错误。以下是我的配置: 用户类别: import java.io.Serializable; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; impor
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "USERS")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "USERNAME")
private String username;
@Column(name = "PASSWORD", nullable = false)
private String password;
@Column(name = "ENABLED", nullable = false)
private boolean enabled;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String username, String password, boolean enabled, Set<Authorities> authorities, Customer customer) {
super();
this.username = username;
this.password = password;
this.enabled = enabled;
this.authorities = authorities;
this.customer = customer;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private Set<Authorities> authorities = new HashSet<>();
@OneToOne(cascade = CascadeType.ALL)
private Customer customer;
//Getter and Setter methods
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Set<Authorities> getAuthorities() {
return authorities;
}
public void setAuthorities(Set<Authorities> authorities) {
this.authorities = authorities;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
因此,我运行的测试如下:
用户详细信息测试:
package com.vishal.project;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.security.core.userdetails.UserDetailsService;
import com.vishal.project.entities.User;
import com.vishal.project.services.UserService;
public class UserDetailsTest {
private static Logger logger = LoggerFactory.getLogger(UserDetailsTest.class);
private GenericApplicationContext ctx;
private UserDetailsService userDetailsService;
private UserService userService;
@Before
public void SetUp() {
ctx = new AnnotationConfigApplicationContext(OnlineBookShoppeApplication.class);
userDetailsService = ctx.getBean(UserDetailsService.class);
logger.info("starting to run tests now............");
assertNotNull(userDetailsService);
}
@Test
public void testfindByUsername() {
User user = userService.findByUsername("admin");
String Username = user.getUsername();
logger.info("Username=" + Username);
}
@After
public void teardown() {
ctx.close();
}
}
但是得到一个n错误,比如:
java.lang.NullPointerException
at com.vishal.project.UserDetailsTest.testfindByUsername(UserDetailsTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
用户服务接口:
package com.vishal.project.services;
import com.vishal.project.entities.User;
public interface UserService {
User findByUsername(String username);
User save (User user);
}
非常感谢您的帮助…您的
userService
字段为null
,因为您尚未为其赋值。您的测试似乎没有使用您的userDetailsService
字段,因此您可以删除它,然后替换此行:
userDetailsService = ctx.getBean(UserDetailsService.class);
为此:
userService = ctx.getBean(UserService.class);
java:37哪一行?要么userService为null,要么user为null谢谢先生。现在我得到了这个错误,它说没有quaorg.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的'com.vishal.project.services.UserService'类型的合格bean,但我已经为UserService定义了UserdetailsServiceImpl。刚刚在问题中添加了服务方法
userDetailsService = ctx.getBean(UserDetailsService.class);
userService = ctx.getBean(UserService.class);