dcsimg
Require clarification where JUnit can be used and how?
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   priyanka_shah
Posted On:   Friday, February 3, 2006 06:08 AM

Can we check the value of any variable in middle of the method working using JUnit. Similarly value of any object. Consider simple code: public int addVertex(String typ, String lab) { Vertex tempVertex = new Vertex(typ, lab); vertexList.add(tempVertex); nVerts++; return nVerts-1; }//end of addVertex() In this method I want to check value of tempVertex. is that possible or rather appropraite to use Junit for that? Thanks :)    More>>

Can we check the value of any variable in middle of the method working using JUnit. Similarly value of any object.

Consider simple code:


public int addVertex(String typ, String lab)

{

Vertex tempVertex = new Vertex(typ, lab);

vertexList.add(tempVertex);

nVerts++;

return nVerts-1;

}//end of addVertex()



In this method I want to check value of tempVertex. is that possible or rather appropraite to use Junit for that?



Thanks :)

   <<Less

Re: Require clarification where JUnit can be used and how?

Posted By:   Robert_Impey  
Posted On:   Wednesday, February 22, 2006 10:32 PM

Why not add a test for the constructor to the test class?


...
private String typ, lab;
public void setUp() {
typ = "foo";
lab = "bar";
}
public void testAddVertex() {
int expected = 1;
int actual = addVertex(typ, lab);
assertEquals(expected, actual);
}
public void testVertex() {
Vertex testVertex = new Vertex(typ, lab);
...
assertTrue(testVertex.equals(...));
}

Re: Require clarification where JUnit can be used and how?

Posted By:   ankush_purwar  
Posted On:   Friday, February 3, 2006 05:11 PM

Junit can't check the intermediate objects in your method.
And of course it shouldn't.
I think you have forgot what JUnit provides.
Basically it provides a unit testing framework to test your API's.And before writing test case you need to know only things that the API you are going to test, What it is supposed to do.
How it does it is out of the scope of Junit ?

Re: Require clarification where JUnit can be used and how?

Posted By:   Anonymous  
Posted On:   Friday, February 3, 2006 08:11 AM

Junit helps to check the function namely addVertex here, by
passing some values to its arguments and checking its
returned value.


eg,

Assume the above method in class X,


public void testAddVertex(){

X x = new X();

int actualResult = x.addVertex("hi","hello");

int exceptedResult ="HI" ;

Assert.equals(exceptedResult,actualResult);

}



if actualResult and exceptedResult are same, then the test
is passed , if not same it throws AssertFailedError
About | Sitemap | Contact