Hi,
Let's say i have the following class:
class class1
{
public tableX getInfo(tableY _tableY)
{
tableX tableX;
this.validate(_tableY);
int x = 2;
if (x == 2)
{
tableX = this.insertTableX(_tableY.Id);
}
return tableX;
}
public void validate(tableY _tableY)
{
if (!_tableY.Id )
{
throw Error(strFmt("@label:Error1" " " "@label:Error2",_tableY.desc));
}
if ( _tableY.Age != 0 )
{
throw Error(strFmt("label:Error1" " " "@label:Error3",_tableY.desc));
}
}
}
///
///
///
public tableX insertTableX(tableY _tableY)
{
ttsbegin;
_tableX.Id = _tableY.Id;
_tableX.desc = _tableY.desc;
_tableX.Age = _tableY.Age;
_tableX.insert();
ttscommit;
return tableX;
}
}
As you can see in method getInfo (i call the other two methods) so when i do unit testing 1) should i test the method getInfo or only the other two methods?
This is my first time doing unit testing. So i wanted to start with only the method Validate():
[SysTestTargetAttribute('class1',UtilElementType::Class)]
class class1Test extends SysTestCase
{
tableX tableX;
tableY tableY;
class1 class1;
///
///
///
public void setUp()
{
class1 = new class1();
super();
}
[SysTestMethodAttribute]
public void testValidate_ValidationPassed() //it passed
{
tableY.Id = "1";
tableY.desc = "desc";
tableY.Age = 3;
class1.validate(tableY);
this.parmExceptionExpected(false);
}
[SysTestMethodAttribute]
public void testValidate_IdMissing() //this is failing why?
{
tableY.Id = "";
tableY.desc = "desc";
tableY.Age = 3;
class1.validate(tableY);
this.parmExceptionExpected(true,strFmt("@label:Error1" " " "@label:Error2",tableY.desc);
}
[SysTestMethodAttribute]
public void testValidate_AgeMissing() //this is failing why?
{
tableY.Id = "1";
tableY.desc = "desc";
tableY.Age = 0;
class1.validate(tableY);
this.parmExceptionExpected(true,strFmt("@label:Error1" " " "@label:Error3",tableY.desc);
}
}
As you can see i made three scenarios for method validate 2) is that correct? and i read about AAA concept, 3) am i doing it right? or i should have defined tableX,table Y in the setUp method? Also is it correct that i'm redefining the values of tableY in each method?
4) testValidate method passed but testValidate_IdMissing() and testValidate_AgeMissing() failed can someone tell me why????? I noticed that this is because the actual method throws an error before it reaches parmExceptionExpected so how should i do my unit testing for this?
5) how to do unit testing for the insert method?