views:

264

answers:

9

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
A: 

junit.org

FarmBoy
A: 

unit tests are code snippets that test your classes/methods to make sure they work as expected. theyre useful if written before implementing the actual classes/methods and/or as regression testing - to make sure you dont break any existing functionality when you touch it later.

the easiest way to see a working example is to install maven, run "mvn archetype:generate" and select 15 (the simple hello world project) when prompted for archetype id. that will generate a simple working project with one class and one test case class

hatchetman82
+11  A: 
  1. Because that is your proof that the application actually works as intended. You'll also find regression bugs much easier. Testing becomes easier, as you don't have to manually go through every possible application state. And finally, you'll most likely find bugs you didn't even know existed even though you manually tested your code.

  2. Google for junit

  3. Unit tests should always be written, as said, it is your proof that the application works as intended. Some things cannot or can be hard to test, for example a graphical user interface. This doesn't mean that the GUI shouldn't be tested, it only means you should use other tools for it.

  4. See point 2.

Kim L
'it is your proof that the application works as intended'. This is a very common misconception. All they prove is that a particular 'unit' works as intended (not the application). When integrating many units there is still scope for integration bugs where the behaviour over multiple units is not as expected despite individual units having 100% success. That's not to say that unit tests are not incredibly useful in themselves.
Dolbz
good answer except for "google it"
EugeneP
If you're going to move into unit testing, it's also worth looking at mocking frameworks. Using junit is only part of the equation, often you'll want to predetermine/emulate the behaviour of certain classes for the test, e.g. for a test on a class that normally talks to a web service, you may mock out the web service to return the results you want, rather than making an actual call to a webserver. See this qestion: http://stackoverflow.com/questions/22697/whats-the-best-mock-framework-for-java and http://mockito.org/ for Mockito, a nice mocking framework.
Mike
+2  A: 

wikipedia

Andreas_D
That's a way more than "a few sentences" ;) Shorter answers are better as the question stated.
EugeneP
C'mon, the 'Benefits' section is not too long. And it has a lot of useful links inside ;)
Andreas_D
+2  A: 

It's probably work reading the Wikipedia article on Unit Testing, as this will answer most of your questions regarding why. The JUnit web site has resources for writing a Java unit test, of which the Junit Cookbook should probably be your first stop.

Personally, I write unit tests to test the contract of a method, i.e. the documentation for a particular function. This way you will enter into a cycle of increasing your test coverage and improving documentation. However, you should try to avoid testing:

  • Other people's code, including the JDK
  • Non-deterministic code, such as java.util.Random

JUnit is not the only unit testing framework available for Java, so you should evaluate other frameworks like TestNG before diving into it.

In addition to "top-level" frameworks, you will also find quite a few projects covering specific areas, such as:

David Grant
I think it's important to note that technically a unit test determines whether some small part (a unit) of a program is working properly. What most everyone here is talking about, and the usual modern usage, is an automated unit test, which runs as part of a build or on demand.
Jason
+1  A: 

One of the best books on the hows and whys of unit testing in Java is Pragmatic Unit Testing in Java with JUnit (Andy Hunt & Dave Thomas)

Ben Poole
+1  A: 

What I would have written is already covered in many of the responses here but I thought I'd add this...

The best article I ever read on when to use/not to use unit tests was on Steve Sanderson's blog. That is an excellent article covering the cost/benefit of unit testing on different parts of your code-base (i.e. a compelling argument against 100% coverage)

Dolbz
@Dolbz I'll check it. Thank you.
EugeneP
+6  A: 

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

Herminator
A: 

This is how how your programming should be :

  • Decide on an interface (not necessarily a java interface, but how method looks like to everybody
  • Write a test
  • Code the implementation
fastcodejava