How to implement Junit deletion with Spring Boot?

Asked

Viewed 87 times

0

I need to know how to implement registration deletion through Junit(Unit Tests) with Spring Boot, I’m having difficulty, the inclusion is working as you can see below;

@Autowired
    private PessoaRepository pessoaRepository;

    @Autowired
    private PessoaService pessoaService;

    @Test
    public void testAdicionarPessoa() throws Exception {
        Pessoa pessoa1 = new Pessoa("Douglas", "399.536.640-64");
        this.pessoaService.salvar(pessoa1);
        // Assertions.assertThat(pessoa1.getName()).isNotNull();
    }

I tried to delete the record this way, but I was unsuccessful.

@Test
    public void testeDeletePessoa() throws Exception {
        Pessoa pessoa2 = new Pessoa();
        this.pessoaRepository.delete((long) 4);

    }

I accept suggestions and am open to questions

Trying to enter the code generated this error;

inserir a descrição da imagem aqui

  • Hello, an error message has appeared, if yes, report the error, which makes it easier to find the solution.

  • Hi, all good? I just updated my post, could you please take a look?

  • Are you using that database ?

  • I’m yes, it’s the Postgres database.

1 answer

1

The error is giving because it is not finding in the database a record with an id 4 for deletion.

You have probably already run the test once and have deleted the record.

A solution to this type of problem is the use of databases that run in memory for testing, that is, the database only has works while the application is running, for example H2 or HSQLDB.

Here are some complementary links:

https://cezbatistao.wordpress.com/2016/05/05/comecando-com-o-spring-boot-parte-2/

https://medium.com/@josevieiraneto/tdd-com-spring-boot-b5bf7ec725e

Browser other questions tagged

You are not signed in. Login or sign up in order to post.