Re: JUNIT questions



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));
}
}

.



Relevant Pages

  • Re: junit fail/error
    ... Here's a free tutorial that shows you how to develop and run JUnit ... And yes, an exception will rate as an error,not a failure. ... Author of What is WebSphere? ... public void testGetElapsedTime() { ...
    (comp.lang.java.help)
  • Junit - testing instance of a Test class
    ... is there a way to run an instance/object of this type using Junit. ... reason is that I want to call setVar for setting var. ... public void Before{ ...
    (comp.lang.java.programmer)
  • Re: JUNIT questions
    ... my assignment is to write a JUNIT testing program to go with it. ... The red bar flashes at me and I am clueless how ... with past debuggers is that some step you thru the program's execution ...
    (comp.lang.java.programmer)