Mockito Test Exception Expected
Solution 1
In your failing testcase you are not calling loadUserByUsername
function at all. This should work.
@Test(expected = UsernameNotFoundException.class)
public void should_Throw_UserNameNotFoundException_If_UserIsNotFound() {
Mockito
.when(userRepository.findByUsername(fakeUser))
.thenThrow(UsernameNotFoundException.class);
userServiceImpl.loadUserByUsername(fakeUser);
}
Since you don't call the function, exception is never thrown, But you have told Mockito that there will be an exception and hence Mockito says
java.lang.AssertionError: Expected exception: org.springframework.security.core.userdetails.UsernameNotFoundException
Solution 2
You need to call the function you are trying to test. Looks like you missed this in the failing test -
userServiceImpl.loadUserByUsername(username);
Solution 3
I would like to tell you that there are better way to verify the exception then @Test(expected). Lets look at the assertj sample
assertThatThrownBy(() -> userRepository.findByUsername(fakeUser))
.isInstanceOf(UsernameNotFoundException.class)
.hasMessage("some message")
.hasNoCause();
Christiaan
Updated on June 04, 2022Comments
-
Christiaan almost 2 years
I'm trying to test my service layer and repository in spring-boot, and to do this, I'm using Mockito, as part of my test, I validate any log in request and if the username or password is incorrect, an exception should be thrown.
I'm testing that a user exist and my test passes, but, I can't seem to get my test to pass when a username does not exist. Here is my code for the 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); }
This is the code I'm trying to run the test on
@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; }
When I run my tests I get the following
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)
The line below is makes me confused because on my test I am stating that the
UsernameNotFoundException
should be thrown if the user does not existjava.lang.AssertionError: Expected exception: org.springframework.security.core.userdetails.UsernameNotFoundException
I followed the tutorial found here on number 2
Non-Void Return Type
but no luck, I know that I can accomplish my goal using
JUnit
but I want to try and do this usingMockito
firstI know I'm missing something, can anyone please help me understand whats happening here