Re: JUNIT questions
- From: "andrewmcdonagh" <andrewmcdonagh@xxxxxxxxx>
- Date: 28 Feb 2007 03:52:56 -0800
On Feb 28, 10:12 am, "Taria" <mche...@xxxxxxxxxxx> wrote:
Whee! Lol, sorry, I think I'm insane after reading a ton of
documentation trying to figure JUNIT out. :p
Aside from having problems figuring out a simple Linked List program,
my assignment is to write a JUNIT testing program to go with it. I
sat at the keyboard, fumbling through many different sites trying to
conjure something up and I'm having trouble trying to populate the
initial linked list. bleah.
Ok, so I went back to an easier type of program to implement JUNIT
since I'm already having trouble with initializing a linked list. I
followed a tutorial, copied their very simple "add money" program and
proceeded to write a JUNIT program to go with that (not part of my
assignment directly but I was hoping this would help.)
Did it? Well, sorta. The red bar flashes at me and I am clueless how
to go about debugging an error that JUNIT has found. All the
tutorials I have gone through usually addresses the green bar but not
what to do when the method is not functioning properly. My experience
with past debuggers is that some step you thru the program's execution
and you are able to set up breaks, examine values of variables, etc.
So, how do you use JUNIT the same way? Am I looking at JUNIT the
wrong way?
Any tips or advice on this subject is appreciated.
Marion
P.S. I did not include any code since I'm really using a tutorial's
code (which is flawless and works). My own JUNIT did not work, it
flashed red at me and this is where I'm stuck. :(
unit testing can be difficult when starting out...don't worry to much.
The basic premise you should keep in mind is the 3 As...
Arrange - your test data/objects
Act - perform the thing you want to test
Assert - check the expected results happened.
Andrew
Here's a set of Junit tests for the LinkedList class that is part of
Java 1.5 (Using generics).
package example;
import java.util.LinkedList;
import java.util.List;
import junit.framework.TestCase;
public class LinkedListTest extends TestCase {
public void testAddingOneElement() {
List<String> aList = new LinkedList<String>();
aList.add("One");
assertEquals("Wrong number of list elements", 1, aList.size());
assertEquals("Wrong value!", "One", aList.get(0));
}
public void testAddingMutlipleElements() {
List<String> aList = new LinkedList<String>();
aList.add("One");
aList.add("Two");
aList.add("Three");
assertEquals("Wrong number of list elements", 3, aList.size());
assertEquals("Wrong value!", "One", aList.get(0));
assertEquals("Wrong value!", "Two", aList.get(1));
assertEquals("Wrong value!", "Three", aList.get(2));
}
public void testRemovingOneElement() {
List<String> aList = new LinkedList<String>();
aList.add("One");
assertEquals("Setup check failed, should have one entry", 1,
aList.size());
aList.remove("One");
assertEquals("List not empty as expected!", 0, aList.size());
}
public void testRemovingMiddleElement() {
List<String> aList = new LinkedList<String>();
aList.add("One");
aList.add("Two");
aList.add("Three");
assertEquals("Setup check failed, should have one entry", 3,
aList.size());
aList.remove("Two");
assertEquals("Wrong size!", 2, aList.size());
assertEquals("Wrong value!", "One", aList.get(0));
assertEquals("Wrong value!", "Three", aList.get(1));
}
}
.
- References:
- JUNIT questions
- From: Taria
- JUNIT questions
- Prev by Date: Re: help with properties class
- Next by Date: Hibernate set to map
- Previous by thread: Re: JUNIT questions
- Next by thread: method to access the name of the object that got created.
- Index(es):
Relevant Pages
|