我正在尝试在春季启动中测试我的服务层和存储库,为此,我使用了Mockito,作为测试的一部分,我将验证任何登录请求,以及用户名或密码是否正确,应引发异常。
我正在测试用户是否存在并且我的测试通过了,但是,当用户名不存在时,我似乎无法通过我的测试。这是我的tets代码
@Mock
UserRepository userRepository;
@InjectMocks
UserServiceImpl userServiceImpl;
Users mockUser = Mockito.mock(Users.class);
private String username = "real_user";
private String fakeUser = "fake_user";
@Before
public void setup() {
mockUser.setUsername(username);
mockUser.setPassword(new BCryptPasswordEncoder().encode("password"));
mockUser.setEnabled(true);
}
/**
* This Test Passes
*/
@Test
public void should_LoadUsersByUsername_IfUserExists() {
Mockito.when(userRepository.findByUsername(username)).thenReturn(mockUser);
userServiceImpl.loadUserByUsername(username);
Mockito.verify(userRepository, Mockito.atLeastOnce()).findByUsername(username);
}
/**
* This Test Fails
*/
@Test(expected = UsernameNotFoundException.class)
public void should_Throw_UserNameNotFoundException_If_UserIsNotFound() {
Mockito
.when(userRepository.findByUsername(fakeUser))
.thenThrow(UsernameNotFoundException.class);
}
这是我要在
上运行测试的代码@Service
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Users user = userRepository.findByUsername(username);
if (user == null) {//I'm trying to hit this line with my test that fails
throw new UsernameNotFoundException("The user was not found");
}
return user;
}
运行测试时,我得到以下信息
java.lang.AssertionError: Expected exception: org.springframework.security.core.userdetails.UsernameNotFoundException
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
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.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
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)
下面的一行让我感到困惑,因为在测试中,我指出如果用户不存在,应该抛出UsernameNotFoundException
java.lang.AssertionError: Expected exception: org.springframework.security.core.userdetails.UsernameNotFoundException
我遵循了在第2号上找到here的教程
非无效返回类型
但是没有运气,我知道我可以使用JUnit
来实现自己的目标,但是我想先尝试使用Mockito
来实现目标
我知道我缺少什么,任何人都可以帮助我了解这里发生的事情
答案 0 :(得分:2)
您需要调用要测试的函数。看起来您在测试失败中错过了这一点-
userServiceImpl.loadUserByUsername(username);
答案 1 :(得分:1)
在失败的测试用例中,您根本没有调用loadUserByUsername
函数。这应该工作。
@Test(expected = UsernameNotFoundException.class)
public void should_Throw_UserNameNotFoundException_If_UserIsNotFound() {
Mockito
.when(userRepository.findByUsername(fakeUser))
.thenThrow(UsernameNotFoundException.class);
userServiceImpl.loadUserByUsername(fakeUser);
}
由于您不调用该函数,因此永远不会引发异常,但是您已经告诉Mockito将会出现异常,因此Mockito会说
java.lang.AssertionError:预期的异常: org.springframework.security.core.userdetails.UsernameNotFoundException
答案 2 :(得分:0)
我想告诉您,比@Test(expected)更好的方法来验证异常。让我们看一下assertj示例
assertThatThrownBy(() -> userRepository.findByUsername(fakeUser))
.isInstanceOf(UsernameNotFoundException.class)
.hasMessage("some message")
.hasNoCause();