What is the advantage of using Junit to test methods in a class?

Asked

Viewed 736 times

7

I created an application that is basically a class that tests if the methods of another class are returning the expected values. My teacher asked me to remake this class as a Junit test case. I did what he asked, but in the end I get the same result I would when I use only a normal class. What advantages can I take from Junit in this case?

  • 1

    In this specific case, no advantage. One can even argue that it is an exaggeration to use a whole framework to test a single class. But as your need for testing increases, you will have to implement other types of checks and outputs and integrations... Then Junit makes sense because it is already implemented in everything you would have to implement on your own - other people had the same needs and make available to you the solution ready for a common problem that admits a common solution.

  • @Caffé Thanks. That was the answer I was looking for.

2 answers

7


Junit is a framework to produce tests. It knows better than programmers how to do this correctly, it is all ready for its use. Of course the programmer needs to know how to use.

The advantage of doing tests is knowing if the method meets the requirements specified in the test itself. You can modify your normal methods and run Junit to see if your modifications have not compromised the result of the modified method. You will see more advantages when dealing with large code bases. But you will learn to do even from the beginning in exercise codes.

Specifically Junit has a structured way to manipulate the tests and deal with his results. If you write them the way Junit requires the tool you can make the most of this information and determine if everything is ok, all without human intervention. This way can help you integrate with other Junit-compatible tools.

Good use of your resources can better document problems encountered if a test fails. This is useful for various tools and for the whole team. Whenever you have a standard shape you will have the most organized work.

It, as a whole, can help create tests and data to confront behavior, as well as facilitate certain types of tests that would require a specific infrastructure to run. It’s obviously just a process automation tool. It doesn’t make your code any better, nor does it make your tests right. If the tests are badly written, little or nothing helps. It can give productivity and decrease the boring part of writing tests in addition to avoiding some common mistakes and forgetfulness.

In addition it encourages creating the tests before the code, the so-called TDD.

  • Not to mention that when using a framework for this later you can use it in an IC tool, such as the famous Jenkins.

  • I didn’t specifically mention this one, I talked about tools in general.

  • Yes, I only mentioned Jenkins specifically since the question pointed to Junit.

0

I understand that the big advantage is mainly one, the same one that makes me use JAVA: Standardization.

If you use a "mini framework" that you made to test instead of using one that is already tested, documented and widely used what will happen when you need 300 (or 3 need not be exaggerated) programmers developing and testing your application? They will have to learn how to use this testing framework, they will not know if it will continue to serve in the future, etc. Things that when you use market frameworks are much easier, cheaper and standardized to work with.

So the moral of the story is: It’s best to train the way the market works to be a good professional in the future

Browser other questions tagged

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