Extended class methods running twice, in JUnit | Selenium Forum
M
Posted on 16/06/2016
I have created a small code using JUnit framework.
I have create total 3 classes
a)Parent class containing 2 methods
b)Child class extends Parent class containing 2 another methods specific to child class
c)Main class with RunWith to execute the classes Parent and Child

Issue:Execution, when Parent and child are called through main method, methods in parent executing twice.
Say Parent have methods A() and B()
Child have methods C() and D()
When running main method with Parent.class, Child.class the methods in Parent executing twice

Actual Output:
A()
B()
C()
D()
A()
B()

Expected Output:
A()
B()
C()
D()

Below is code for classes

MainMethod Class
package jUnit;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

@RunWith(Suite.class)
@SuiteClasses({
GenericClasses.class,
Compose.class,
})

public class Main {

public static void GmailMain() {

}

}


Parent Class
package jUnit;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class GenericClasses {

@Test
public void testA1LaunchApplication(){
System.out.println("In Launch");
}

@Test
public void testA2Login() throws InterruptedException{
System.out.println("In Login");
}

}


Child Class which extends parent class
package jUnit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Compose extends GenericClasses{

@Test
public void test1(){
System.out.println("Printing");
}

@Test
public void test2(){
System.out.println("Will be Ignored");
}

}



OUTPUT:
In Launch
In Login
Printing
Will be Ignored
In Launch
In Login


EXPECTED OUTPUT:
In Launch
In Login
Printing
Will be Ignored

M
Replied on 17/06/2016

when you call compose.java it calls genericclass.java. so, generic class is being called twice. once by main class and once by compose.java