Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing in Java - what is it? [closed]

Can you explain in a few sentences:

  1. Why we need it / why they make our life easier ?
  2. How to unit-test [simple example in Java] ?
  3. When do not we need them / types of projects we can leave unit-testing out?
  4. useful links
like image 811
EugeneP Avatar asked Mar 01 '10 11:03

EugeneP


People also ask

What is the unit testing in Java?

Unit testing refers to the testing of individual components in the source code, such as classes and their provided methods. The writing of tests reveals whether each class and method observes or deviates from the guideline of each method and class having a single, clear responsibility.

What is an end of unit test?

Unit testing efficiently checks for the functions or calculations that provide resulting data—a numerical value, a text string, etc. End-to-end testing tests all layers of the application at once; it's best-suited to make sure buttons, forms, changes, links, and generally entire workflows function without problems.

What happens if JUnit test method is declared as private?

If a JUnit test method is declared as "private", it compiles successfully. But the execution will fail. This is because JUnit requires that all test methods must be declared as "public".

Is unit testing end-to-end?

End-to-end testing is a testing process in which the tester tests a software application from the user's perspective. Unit testing is a testing process where the developer verifies that individual units of source code work correctly.


1 Answers

Why we need it / why they make our life easier ?

  • It allows you to check the expected behavior of the piece(s) of code you are testing, serving as a contract that it must satisfy.
  • It also allows you to safely re-factor code without breaking the functionality (contract) of it.
  • It allows you to make sure that bug fixes stay fixed by implementing a Unit test after correcting a bug.
  • It may serve as as a way to write decoupled code (if you have testing in mind while writing your code).

How to unit-test [simple example in Java] ?

Check out the JUnit website and the JUnit cookbook for details. There isn't much to writing JUnit test cases. Actually coming up with good test cases is surely harder than the actual implementation.

When do not we need them / types of projects we can leave unit-testing out?

Don't try to test every method in a class, but rather focus on testing the functionality of a class. Beans for example, you won't write tests for the getters and setters...

Links

JUnit - Unit testing

EclEmma - test coverage tool

link text - Wikipedia link to unit testing

like image 117
Hermann Hans Avatar answered Nov 10 '22 10:11

Hermann Hans