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

}

}

[8698 byte] By [MasterofDesastera] at [2007-11-26 14:53:39]
# 1

I don't know why you can't JUnit test these. There's nothing special about them. You aren't clear about what failed. Why can't you test?

I have no idea what these classes do - I can't read German.

public class OsterauskunftTestCase extends TestCase

{

private IOsterauskunft ios;

public setIos(IOsterauskunft newIos) { this.ios = newIos; }

public void testosternImJahr()

{

int index = 4;

String expected = "value to test";

String actual = ios.osternImJahr(index);

assertEquals("wrong actual value", expected, actual);

}

}

I'd use Spring and dependency injection to give the appropriate implementation. Use your method of choice.

%

duffymoa at 2007-7-8 8:42:01 > top of Java-index,Java Essentials,New To Java...