Hi so there's a test for a constructor for a vehicle. The test initializes a vehicle with a driver without a driving license and it should throw an Exception. code constructor:
public Voertuig(String Merk, Datum datumEersteIngebruikname, int Aankoopprijs, int Zitplaatsen, Mens bestuurder, Mens ... ingezetenen) {
this.nummerplaat = div.getNummerplaat();
this.Zitplaatsen = Zitplaatsen;
try {
this.Merk = Merk;
this.datumEersteIngebruikname = datumEersteIngebruikname;
this.Aankoopprijs = Aankoopprijs;
if (!Arrays.asList(bestuurder.getRijbewijs()).contains(Rijbewijs.B) || !Arrays.asList(bestuurder.getRijbewijs()).contains(Rijbewijs.BE)) {
throw new MensException("Geen correct rijbewijs");
} else {
this.bestuurder = bestuurder;
Ingezetenen.add(bestuurder);
}
Mens[] a = ingezetenen;
if (a.length > Zitplaatsen - 1) {
throw new MensException("te veel ingezetenen");
} else {
for (int i = 0; i < a.length; i++) {
ingezetenenExclBestuurder.add(a[i]);
Ingezetenen.add(a[i]);
}
}
} catch (MensException e) {
System.out.println(e.getMessage());
}
}
code test:
@Test(expected = be.vdab.util.mens.MensException.class)
public void test_constructor_zonder_Rijbewijs() {
//VOERTUIG B,BE//bestuurder:---
Voertuig voertuig = new TestVoertuig("auto", datum, 18300, AANTAL_INZITTENDEN, INGEZETENE_A);
}
and when i run this focused test method this is the outcome.
------------- Standard Output ---------------
Geen correct rijbewijs
Testcase: Testcase: test_constructor_zonder_Rijbewijs(be.vdab.voertuigen.VoertuigTest): FAILED
Expected exception: be.vdab.util.mens.MensException
junit.framework.AssertionFailedError: Expected exception: be.vdab.util.mens.MensException
So according to the Output the Exception is caught and displayed but yet the test failed. Anybody knows why? Thanks in advance.
EDIT: I fixed it by not including a try-catch block but just throwing the exception resulting in having to add 'throws MensException' in every test-method where it was creating an object. I fixed this by adjusting my custom MensException, instead of extending Exception i had it extend RuntimeException so i didn't have to add 'throws MensException' in every test-method.