Pablo Matias Gomez
Pablo Matias Gomez

Reputation: 6823

How to run differents tests from one single method with junit

I have a test that does a cycle to run the same test but with different inputs. The problem is that when one assert fails, then the test stops, and it is marked as if one test failed.

Here is the code:

@Test
public void test1() throws JsonProcessingException {
    this.bookingsTest(Product.ONE);
}

@Test
public void test2() throws JsonProcessingException {
    this.bookingsTest(Product.TWO);
}

public <B extends Sale> void bookingsTest(Product product) {

    List<Booking> bookings = this.prodConnector.getBookings(product, 100);

    bookings.stream().map(Booking::getBookingId).forEach((bookingId) -> {
        this.bookingTest(bookingId);
    });
}

public <B extends Sale> void bookingTest(String bookingId) {
    ...
    // Do some assert:
    Assert.assertEquals("XXX", "XXX");
}

In that case, the methods test1 and test2, execute as two different tests, but inside them, I do a cycle to check some stuff on every item that is returned by the collection. But what I want is to make that test on each item to be treated as a different one, so that if one fails, the others continue executing and I can see which one failed and how many failed over the total.

Upvotes: 1

Views: 50

Answers (1)

piotrek
piotrek

Reputation: 14550

what you described is parameterized testing. there is plenty of frameworks that may help you. they just have different simplicity to power ratio. just pick the one that fits your needs.

junit's native way is @Parameterized but it's very verbose. other junit plugins that you may find useful are zohhak or junit-dataprovider. if you are not enforced to use junit and/or plain java, you can try spock or testng.

Upvotes: 1

Related Questions