What's the best way (i.e. standard practice) to implement unit testing in my code?
-
If I'm designing a program in, let's say Java, how do I include the ability to unit test? If I have a method, like: public int add(int a, int b) { return a + b; } ...do I just need a separate Class (i.e. UnitTest.java) that calls that method and checks that the response is 3 when I input a=1 and b=2? Essentially I'm wondering if I actually need to do all of this manually, or if there's a simpler way to find the source of LOGICAL errors in my code (as opposed to syntax and other errors like NullPointerException, etc).
-
Answer:
How it is usually done, you have not only separate Class, but separate project, where you have all your test classes. If you want to be strict and go with test first approach, you should start with test first, so you would write something like public void addTest() { .....//class constructor and so on int expectedResult = 2; int actualResult = class.add(1,1); assertEquals(expectedResult,actualResult); } If you want to do this properly, you should see the test fail first, if you want to learn more, watch this step by step and you will see how the flow works.
Filip Tuhy at Quora Visit the source
Related Q & A:
- What's the best way to get into the Graphic Design industry?Best solution by Yahoo! Answers
- What's the best way of returning damaged NIKE shoes to NIKE for another pair?Best solution by Yahoo! Answers
- What's the best way to hook up an overhead projector to a laptop?Best solution by Yahoo! Answers
- What's the best way to whiten your teeth?Best solution by Yahoo! Answers
- What's the best way to start a small clothing line business?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.