Are the unit tests correct?

1

I'm participating in a selective process, and one of the criteria is to implement any kind of unit testing, I just want to know if it's implemented right the code below, it's working perfectly, I just need to know if it's implemented right. p>

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:application.properties")
@ContextConfiguration(classes = { AppConfig.class })
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TesteApplicationTests {

    @Autowired
    private PessoaRepository pessoaRepository;

    @Autowired
    private PessoaService pessoaService;

    @Test
    public void testPessoa1Inclusao() throws Exception {
        Pessoa pessoa1 = new Pessoa("Marcelo Dultra", "840.622.210-71");
        this.pessoaService.salvar(pessoa1);
        // Assertions.assertThat(pessoa1.getName()).isNotNull();
    }

    @Test
    public void testPessoa2Exclusao() throws Exception {

        Pessoa pessoa = pessoaRepository.findOne((long) 3);
        pessoaRepository.delete(pessoa);    
    }

    @Test
    public void testPessoa3ExclusaoDaUltima() throws Exception {
        List<Pessoa> todasPessoas = pessoaRepository.findAll();
        Pessoa ultima = todasPessoas.get(todasPessoas.size() - 1);
        pessoaRepository.delete(ultima);    
    }

    @Test
    public void testPessoa4Atualizacao() throws Exception {
        Pessoa pessoa3 = new Pessoa("Ricardo Falcão1", "213.535.690-55");
        this.pessoaService.atualizar(pessoa3.getCodigo().valueOf(4), pessoa3);

    }



}
    
asked by anonymous 02.11.2018 / 16:47

1 answer

1
Based on my comment your question, in addition to reaffirming that the unit tests are for testing the business rule and should have both error and hit tests, it is nice to put that unit tests should test a small part of the rule of business.

My suggestion is that you create a validating class of people. This class will have validation method of CPF / CNPJ, State Registration, date of birth, e-mail, telephone number, the existence of the inserted CPF / CNPJ and etc. It depends on how rich you want your validations. In the case of deletion, you can place attachments of the deleted person with other person entities as addresses (1 - n) if the exclusion is not logical. This would cause database problems with foreign key .

Always try to make a test that the entered value passes and a test that the entered value does not pass (ie, the hit is a mistake). Add to your class validators friendly messages that suggest to the user how the same should correct the error and not simply saying that it is wrong.

More materials, if you'd like to read about it:

" Understand unit testing once and for all

" Software Test

TDD fundamentals of test-driven development

And how do you test your codes? "

    
02.11.2018 / 17:30