mardi 7 janvier 2020

spring unit test repository save not working

I'm trying to write a unit test, for my spring server.

First it will check if a username is present or not in repository, if not so the username is available, then it will return true, and then I will save that username to my repository and check if available ot not it should return false.

Here is my code:

@Test
    public void availableTest() {
        String username="some_username";
        LoginCredential lc=new LoginCredential();
        lc.setUsername(username);
        lc.setPasswordHash("1");
        lc.setSessionID(0);
        assertEquals(true, loginCredentialService.available(username));
        loginCredentialRepository.save(lc);
        assertEquals(false, loginCredentialService.available(username));
    }

But for some reason, for the last assertEquals it gives me error. So I can say, the data is not being saved in repository, as I have tested my APIs using postman.

So how this can be resolved?

I think testing class is properly annotated:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MainApplicationTests {

    @Autowired
    private LoginCredentialService loginCredentialService;

    @MockBean
    private LoginCredentialRepository loginCredentialRepository;
    ...

Aucun commentaire:

Enregistrer un commentaire