Monday 19 July 2021

Junit5: Parameterize a test: EnumSource

@EnumSource is used to run a test with different values from an enumeration.

 

For example,

@ParameterizedTest
@EnumSource
void getQuoteOfTheDayTest (Day day) {
	String actualQuote = QuotesOfTheDay.getQuoteOfTheDay(day);

	if (day == Day.SATURDAY || day == Day.SUNDAY) {
		assertEquals("Happy Holiday", actualQuote);
	} else if (day == Day.MONDAY) {
		assertEquals("You always pass failure on the way", actualQuote);
	} else if (day == Day.TUESDAY) {
		assertEquals("Winning doesn't always mean being first", actualQuote);
	} else {
		assertEquals("every day is a great day!!!", actualQuote);
	}

}

 

Above snippet run the ‘getQuoteOfTheDayTest’ with all the possible enumerations of the enum ‘Day’.

 

Find the below working application.

 

Day.java

 

package com.sample.app;

public enum Day {

	SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}


QuotesOfTheDay.java

package com.sample.app;

public class QuotesOfTheDay {

	public static String getQuoteOfTheDay(Day day) {

		switch (day) {
		case SATURDAY:
		case SUNDAY:
			return "Happy Holiday";

		case MONDAY:
			return "You always pass failure on the way";
		case TUESDAY:
			return "Winning doesn't always mean being first";

		default:
			return "every day is a great day!!!";
		}

	}

}


EnumSourceTest1.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

public class EnumSourceTest1 {

	@ParameterizedTest
	@EnumSource
	void getQuoteOfTheDayTest(Day day) {
		String actualQuote = QuotesOfTheDay.getQuoteOfTheDay(day);

		if (day == Day.SATURDAY || day == Day.SUNDAY) {
			assertEquals("Happy Holiday", actualQuote);
		} else if (day == Day.MONDAY) {
			assertEquals("You always pass failure on the way", actualQuote);
		} else if (day == Day.TUESDAY) {
			assertEquals("Winning doesn't always mean being first", actualQuote);
		} else {
			assertEquals("every day is a great day!!!", actualQuote);
		}

	}

}

Run ‘EnumSourceTest1.java’, you can observe that the test method 'getQuoteOfTheDayTest' called with every enum constant.



You can even specify which constants to be used for testing using ‘names’ attribute.

@ParameterizedTest
@EnumSource(names = { "MONDAY", "FRIDAY"})
void getQuoteOfTheDayTest(Day day) {
	String actualQuote = QuotesOfTheDay.getQuoteOfTheDay(day);

	if (day == Day.SATURDAY || day == Day.SUNDAY) {
		assertEquals("Happy Holiday", actualQuote);
	} else if (day == Day.MONDAY) {
		assertEquals("You always pass failure on the way", actualQuote);
	} else if (day == Day.TUESDAY) {
		assertEquals("Winning doesn't always mean being first", actualQuote);
	} else {
		assertEquals("every day is a great day!!!", actualQuote);
	}

} 


EnumSourceTest2.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

public class EnumSourceTest2 {
	
	@ParameterizedTest
	@EnumSource(names = { "MONDAY", "FRIDAY"})
	void getQuoteOfTheDayTest(Day day) {
		String actualQuote = QuotesOfTheDay.getQuoteOfTheDay(day);

		if (day == Day.SATURDAY || day == Day.SUNDAY) {
			assertEquals("Happy Holiday", actualQuote);
		} else if (day == Day.MONDAY) {
			assertEquals("You always pass failure on the way", actualQuote);
		} else if (day == Day.TUESDAY) {
			assertEquals("Winning doesn't always mean being first", actualQuote);
		} else {
			assertEquals("every day is a great day!!!", actualQuote);
		}

	}

}


Run above test, you will observe that the test method ‘getQuoteOfTheDayTest’ is executed for constants "MONDAY", "FRIDAY".




EnumSource with mode

@EnumSource annotation also provides an optional mode attribute that enables fine-grained control over which constants are passed to the test method.

 

For example, below snippet run the test method ‘getQuoteOfTheDayTest’ will all Day enum constants except "MONDAY", "FRIDAY".


@ParameterizedTest
@EnumSource(mode=Mode.EXCLUDE, names = { "MONDAY", "FRIDAY"})
void getQuoteOfTheDayTest(Day day) {
	.......
	.......
}


EnumSourceTest3.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.EnumSource.Mode;

public class EnumSourceTest3 {
	
	@ParameterizedTest
	@EnumSource(mode=Mode.EXCLUDE, names = { "MONDAY", "FRIDAY"})
	void getQuoteOfTheDayTest(Day day) {
		String actualQuote = QuotesOfTheDay.getQuoteOfTheDay(day);

		if (day == Day.SATURDAY || day == Day.SUNDAY) {
			assertEquals("Happy Holiday", actualQuote);
		} else if (day == Day.MONDAY) {
			assertEquals("You always pass failure on the way", actualQuote);
		} else if (day == Day.TUESDAY) {
			assertEquals("Winning doesn't always mean being first", actualQuote);
		} else {
			assertEquals("every day is a great day!!!", actualQuote);
		}

	}
}


Run above test class, you can observe that the test method ‘getQuoteOfTheDayTest’ gets executed for all Day enum constants except "MONDAY", "FRIDAY".




You even specify regular expression to run test method with matched enum constants.

@ParameterizedTest
@EnumSource(mode = Mode.MATCH_ALL, names = "^.*DAY$")
void getQuoteOfTheDayTest(Day day) {
	.......	
	.......
}

 

 

 






Previous                                                    Next                                                    Home

No comments:

Post a Comment