In this post We will be discussing that.
Recently, I came across a requirement where there are multiple functionalities present as separate methods enclosed in a single class. The class is a kind of Utililty class type and I need to create Test each of its methods with all possible input types to judge the integrity of each of the functionalities of the class. So, what I came up with approach to create JUnit Parameterized Classes of each of the functionalities, but the problem is that, there are too many JUnit Parameterized Classes are getting created as there are lots of util methods in the class. So, I need to find a way to try to consolidate all the Test Cases in a Single class.
I decided to create Inner classes for testing (in a Parameterized way) each of the functionalities and enclose all of them in a Outer class.
org.junit.experimental.runners.Enclosed provided me the control that I needed.
Let's go through the Code:
@RunWith(Enclosed.class)
public class ParameterizedTest{
@BeforeClass
public static void init()
{
System.out.println(" Setup Class Wide Accessible Resources Here");
}
@AfterClass
public static void release()
{
System.out.println(" Release Class Wide Accessible Resources after all the tests are successfully completed");
}
@RunWith(value=Parameterized.class)
public static class TestLogon{
private String numberA;
private String numberB;
public TestLogon(String numberA, String numberB) {
this.numberA = numberA;
this.numberB = numberB;
}
@Parameterized.Parameters
public static Iterable
The output is:
Setup Class Wide Accessible Resources Here Intialize Configuration which is to be done before each test methods null:asd|asd|||ASD||ASD Release Configuration which is to be done after each test methods Intialize Configuration which is to be done before each test methods Dummy Tests method to test @Before and @After annotated methods Release Configuration which is to be done after each test methods 1:asd111|asd99|||ASD990||ASD9897 999:asd111|asd99|||ASD990||ASD9897 Release Class Wide Accessible Resources after all the tests are successfully completedFrom the output it is quite apparent, the order of execution of the code and the various lifecycle methods of JUnit. Please note that @Before and @After annotated methods we have used with TestLogon, but it could also be implemented in other Inner Classes too. I am not describing how the Parameterized Test Cases work for JUnit as there are lots of good descriptive resources available. Hope this post provides you some help. Any suggestions or opinions are always welcome. Please share your views. Keep Coding ang Keep sharing.....
I really liked your Information. Keep up the good work. Enclosed
ReplyDelete