gpt4 book ai didi

java - JUnit 断言 null - 使用 stub 和模拟

转载 作者:行者123 更新时间:2023-12-01 20:57:10 26 4
gpt4 key购买 nike

这是我的测试方法:

@Test
public void shouldReturnUser_whenPassedUser() {
// given
when(userStub.getUsername()).thenReturn(defaultName);
when(userStub.getPassword()).thenReturn(defaultPassword);
when(userStub.getEmail()).thenReturn(defaultEmail);

// when
User savedUser = userServiceBean.saveUser(userStub);

// then
assertNotNull(savedUser);
verify(userRepository, times(1)).save(any(User.class));
}

这是我的设置:

 @Spy
@InjectMocks
private UserServiceBean userServiceBean;

@Mock
private UserRepository userRepository;

@Mock
private Principal principal;

@Mock
private PasswordEncoder passwordEncoder;

@Mock
private User userStub;

private String defaultName = "user";
private String defaultPassword = "password";
private String defaultEmail = "example@example.com";

@Before
public void init() {
MockitoAnnotations.initMocks(this);
}

这是我的 userService.saveUser 方法:

public User saveUser(User user) {
User newUser = new User(user.getUsername(), user.getEmail(), passwordEncoder.encode(user.getPassword()));
return userRepository.save(newUser);
}

我的测试结果:

java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertNotNull(Assert.java:712)
at org.junit.Assert.assertNotNull(Assert.java:722)
at com.doublemc.services.UserServiceBeanTest.shouldReturnUser_whenPassedUser(UserServiceBeanTest.java:93)
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:498)
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.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.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:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

为什么我的方法返回 null?我正在使用一个stubUser,并且我准确地告诉他要返回给每个getter的内容(就像我在saveUser方法中所做的那样),但它仍然返回null?我应该如何更改该测试才能工作?

最佳答案

您忘记为 userRepository 模拟定义模拟行为。因此 Mockito 使用默认返回值 null 来保存。

定义这样的行为:

when(userRepository.save(any(User.class))).thenReturn(new User("saved name", "saved email", "saved password"));

关于java - JUnit 断言 null - 使用 stub 和模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42169647/

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