Testclasses
I have written a project with some classes and three interfaces belonging to three classes.
Now I want to creat three testclasses for the interfaces, but I am not sure how it works.
Ot would be very helpful if anyone could give me an example of a testclass which tests one of my interfaces.
I have read a lot about JUnit and Testclasses but all my trials fail.
Here is my first class
publicclass Osterauskunftimplements IOsterauskunft{
/** Implementiert Gauss' Algorithmus zur Berechnung des Osterdatums.
* Das berechnete Datum wird als String im Format TT.MM.JJJJ zur點kgegeben.
* @param das Jahr, f黵 das das Osterdatum berechnet werden soll.
*/
public String osternImJahr(int jahr){
if(jahr < 1800 || jahr > 2199){
return"falsche Jahreszahl!";
}
int m,n;
if(jahr < 1900){
m = 23; n = 4;
}elseif(jahr < 2199){
m = 24; n = 5;
}else{
m = 24; n = 5;
}
int a = jahr % 19;
int b = jahr % 4;
int c = jahr % 7;
int d = (19*a + m) % 30;
int e = (2*b + 4*c + 6*d + n) % 7;
int day = 22 + d + e;
int month = 3;
if(day > 31){
day = d + e - 9;
month = 4;
}
if(day == 26 && month == 4){
day = 19;
}
if(day == 25 && month == 4 && d == 28 && e == 6 && a > 9){
day = 18;
}
String day2;
if (day < 10)
{
day2 ="0" + day;
}
else
{
day2 ="" + day;
}
String month2;
if (month < 10)
{
month2 ="0" + month;
}
else
{
month2 ="" + month;
}
return day2 +"." + month2 +"." + jahr;
}
}
and the pertient interface :
/**
* Inface von Osterauskunft
*
* @hans
* @25.12.2006
*/
publicinterface IOsterauskunft
{
String osternImJahr(int jahr);
}
another example :
class :
publicclass Girokontoimplements IKonto
{
// der ganzzahlige Saldo
privateint _saldo;
// das Dispolimit
privateint _dispoLimit;
/**
* Ein neues Konto hat einen Anfangssaldo und ein Dispo-Limit
* von Null.
*/
public Girokonto()
{
_saldo = 0;
_dispoLimit = 0;
}
/**
* Liefert das Dispo-Limit dieses Kontos.
*/
publicint gibDispoLimit()
{
return _dispoLimit;
}
/**
* Setze das Dispo-Limit auf den gegebenen Wert.
* @param dispoLimit ein positiver Wert oder Null als
*neues Dispo-Limit
*/
publicvoid setzeDispoLimit(int dispoLimit)
{
if (dispoLimit >= 0)
{
_dispoLimit = dispoLimit;
}
else
{
System.out.println("Fehler: Negatives Limit!");
}
}
/**
* Zahlt einen Betrag auf das Konto ein.
*
* @param betrag
*der einzuzahlende Betrag
*/
publicvoid zahleEin(int betrag)
{
if (betrag >= 0)
{
_saldo = _saldo + betrag;
}else
{
System.out
.println("Fehler: Es kann kein negativer Betrag eingezahlt werden");
}
}
/**
* Hebt einen Betrag vom Konto ab.
*
* @param betrag
*der abzuhebende Betrag
*/
publicvoid hebeAb(int betrag)
{
if (betrag >= 0)
{
if ((_saldo - betrag) < -_dispoLimit)
{
System.out.println("Auszahlung nicht m鰃lich: Dispo-Limit w黵de 黚erschritten.");
}
else
{
_saldo = _saldo - betrag;
}
}
else
{
System.out.println("Fehler: keine negativen Betr鋑e!");
}
}
/**
* Liefert den Saldo des Kontos zur點k.
*
* @return der Saldo des Kontos
*/
publicint gibSaldo()
{
return _saldo;
}
}
and the interface :
publicinterface IKonto
{
interface Girokonto
{
void einzahlen(int betrag);
void auszahlen(int betrag);
int gibSaldo();
}
}

