Are the Unitarian tests correct?

Asked

Viewed 688 times

0

I am participating in a selection process, and one of the criteria is to implement any kind of unit test, I just wonder if it is implemented right the code below, it is working perfectly, I just need to know if it is implemented right.

@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);

    }



}
  • How do we know? We don’t know the requirements. We don’t know the problem deeply. Testing is understanding the problem. Looking at the code from above it seems that not even the actual code seems to be right (without seeing it). When there is something conceptually wrong, what is the point of testing? And even looking only at the tests, they’re naive, trying to see if you do something in a specific situation too much, which is always going to work. Testing is much more complicated and almost everyone does it just to feel good and be fashionable, not to make the application really robust, so it’s a waste of time.

  • Cool I do not agree, assuming that one of the best companies in Brazil requested unit testing as part of the selection process, if companies request it is because it is important, but this is not a discussion forum, I would be very happy if you could give a more friendly response.

  • Cool, there’s a guy who calls himself an algorithm teacher, and he said that my tests are wrong, because unit tests are not to effect CRUD in the bank, as I have little experience do not know whether he is wrong. Are these unit tests I’ve done really wrong?

  • I made a friendly comment, but you are asking to hear what you want, this I do not do because this is deception, as you were deceived by " one of the best companies in Brazil requested the unit tests as part of the selection process". And "a guy who calls himself an algorithm teacher" that’s a wonder of credentials. But he’s right, only it’s not the only problem. I’m sorry I can’t be of more help, and you don’t want the help I can give.

  • @wladyband really the guy who says he’s a teacher is right. Unit tests are for testing business rules. For example, if the registration is person and has CPF, the system should validate if the CPF/CNPJ is valid, if the date of birth is not greater than 150 years for example. Which are validations that the database itself doesn’t do. I’m not very knowledgeable about testing, but I’ve worked with TDD. Always do fail tests and get it right, that you pass the test if you fail and pass the test if you succeed. More here: https://www.devmedia.com.br/tdd-fundamentos-development-orienteda-testes/28151

2 answers

1


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

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

Always try to do a test that the entered value passes and a test that the entered value does not pass (ie the correct is to see error). Add your class of validators friendly messages that suggest to the user how it should fix the error and not simply saying it is wrong.

More materials if you want to read about it:

"Understand once and for all what unit testing is"

"Software Testing"

"TDD Fundamentals of Test-Driven Development"

"What’s up? How do you test your codes?"

0

From what I’ve observed, what you’ve done is integration tests and not unit tests.

Unit tests cover a small part of the code, are simple and test internal logic of the application commonly related to the business involved, going from the simplest rules (if the field is in the correct format, if the character limit is correct, etc) to the most complex (if the user is blocked, if the user can make a purchase given his credit, etc).

These multi-layer tests are commonly known as integration tests, which is the case with the code you made. The code is making queries to a database (an H2 in memory, I presume). The code also apparently isn’t testing any logic created by you. I’m also not seeing any assert result (the only one in the code is commented), to ensure that the expected behavior has been achieved.

An example of unit testing, in the concept you presented, could be validating the person’s CPF, returning an Exception CpfInvalidoException if invalid:

class PessoaTest {

    @Test(expects = CpfInvalidoException.class);
    public void naoSalvarPessoaComCpfInvalido() {
        PessoaService pessoaService = new PessoaService(mock(PessoaRepository.class));
        pessoaService.salvar(new Pessoa("Marcelo Dultra", "840.622.210"));
    }

}

Browser other questions tagged

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