jeudi 15 avril 2021

org.springframework.security.core.userdetails.UsernameNotFoundException: User can't be found

I created a test class for some methods. I defined an user and then save it, setting its name and other values. I made a method which tells me that it has been saved correctly, but when i search it by username it says it couldn't be found.

This is my test class:

@Mock
private UserRepository userRepository;


@Autowired
private UserService userService;


private Data data;
private User user;


@Test
void shouldSaveUser() {
    Integer before = this.userService.findAllUsers().size();
    User u = new User();
    u.setId(23L);
    u.setUsername("name");
    u.setPassword("Pass1234");
    Data data= new Data();
    data.setId(3L);
    data.setUser(u);
    u.setData(data);
    this.userService.save(u);
    Integer after = this.userService.findAllUsers().size();
    Assertions.assertTrue(before +1 == after);
    
}

@Test
void testUserByUsername() {
UserDetails res = this.userService.loadUserByUsername("name");
Assertions.assertTrue(res != null);
}
    

}

But every time i run the tests the same error happens:

org.springframework.security.core.userdetails.UsernameNotFoundException: User can't be found

So i dont know where is the error. If u could help me please

Aucun commentaire:

Enregistrer un commentaire