gpt4 book ai didi

java - 测试Java服务

转载 作者:行者123 更新时间:2023-12-01 19:01:14 24 4
gpt4 key购买 nike

我在测试Java服务时遇到问题我的服务:

@Service
public class UserService {
private final UserRepository userRepository;
private final BCryptPasswordEncoder passwordEncoder;

public UserService(UserRepository userRepository, BCryptPasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}

public void saveUser(User user) {
user.setPassword(passwordEncoder.encode(user.getPassword()));
userRepository.save(user);
}

public User findByEmail(String email) {
return userRepository.findByEmail(email);
}

public User findById(Integer id) {

return userRepository.findById(id).orElse(null);
}

public void save(User user) {
userRepository.save(user);
}


我的测试课:

public class UserServiceTest {
private UserService userService;

@Test
public void saveUser() {

//given
User user = new User(1, "Adam", "Nowak", "j@gmail.com", "abcd");
//when
userService.saveUser(user);
User userToFind = userService.findById(1);
//then
assertNotNull(userToFind);
}


尝试进行测试时,我出现错误:

java.lang.NullPointerException
at pl.coderslab.projectfilmweb.service.UserServiceTest.saveUser(UserServiceTest.java:18)
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:566)
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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)


有人可以帮助解决问题

最佳答案

 @RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {

@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;

@Test
public void shouldSave() {

//given
User user = new User(1, "Adam", "Nowak", "j@gmail.com", "abcd");
//when
userService.save(user);
User userToFind = userService.findById(1);
//then
assertNotNull(userToFind);

}

}

关于java - 测试Java服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59636222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com