Trying JUnit

Now, for the Software Quality and Testing class, we went into JUnit for unit testing in java. To try it out I followed a tutorial, more like a class, that was pretty nice, I’ll give you the link somewhere in the post so you can do it by yourself. The reason I am l¡hiding the link over there is to you to read some of my post. It will be short, I promise.

Okay, I begun with Eclipse, the IDE for developers, more like java developers. I have always liked to use this IDE since I was in my first semesters of college. Okay, Eclipse already have JUnit or you just install the plugin if not.

I tried JUnit before in my Architecture class, so this was more like a refresh of what JUnit is. Some useful concepts and tools are this:

  • JUnit promotes the idea of “first testing then coding”, which emphasizes on setting up the test data for a piece of code that can be tested first and then implemented. This approach is like “test a little, code a little, test a little, code a little.”
  • A Unit Test Case is a part of code, which ensures that another part of code (method) works as expected. To achieve the desired results quickly, a test framework is required. JUnit is a perfect unit test framework for Java programming language.
  • Fixtures is a fixed state of a set of objects used as a baseline for running tests.
  • A test suite bundles a few unit test cases and runs them together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test.
  • Test runner is used for executing the test cases.
  • JUnit classes are important classes, used in writing and testing JUnits.

So, what I learned is that for a good unit test set, you need your code (obviously), the one that will be tested; minimum one class where you will write your tests or how you will test your code, and a main class to run all the tests and return if you win or lose in this race we call code testing.

For example, what I did was a simple class that receives a string and prints it into the console.Screen Shot 2019-02-06 at 00.05.05

Then I have the class where you write your test cases, with simple methods that so what you expect your code to do.

Screen Shot 2019-02-06 at 00.06.09.png

And finally you have your test runner that is where you run your tests (sound duuh) and specify what the outputs will be in case the code passes the tests or fails them.

Screen Shot 2019-02-06 at 00.07.25.png

Before I show you te results of my tests, please try to figure it out by looking at my code, read it very carefully and let’s hope you can see the future.

The result is the following:

Screen Shot 2019-02-06 at 00.10.15.png

This is because in the test class I changed the value of my message to a new one after the code was executed so my test case was expecting a different message that the one was given.

That’s it, hope you liked this post. And, true, the link to the tutorial is in the word Eclipse, in the second appearance of the word in the entry.

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s