Search code examples
testng

TestNG annotated superclass in a different package


I have a superclass and a subclass in different packages. The superclass has @BeforeClass and @BeforeMethod annotations.

However, these methods never get called. If I move both classes to a common package then both @Before_xx methods get called just fine.

Super class:

package com.blah.focus.test.integration;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;

public class BookSuper {

@BeforeClass(groups = "unit")
void prepareDataSet() throws Exception {
    System.out.println("Inside prepareDataSet");        
}

/**
 * 
 * @throws Exception
 */
@BeforeMethod(groups = "unit")
void beforeTestMethod() throws Exception {
    System.out.println("Insde beforeTestMethod yy");

}
} 

Sub class:

package com.blah.focus.domain;

public class BookTest extends BookSuper{
private Book book;

@Test(groups = "unit")
public void testGoodBookConstruction() {
    book = new Book();
    book.setAuthor("Henry");
    book.setTitle("Good Title");
    book.setPublished(new Date());
    book.setPublisher("Rodale");
}
}

Is this by design?


Solution

  • Nope, it should work, and it does work for me:

    Inside prepareDataSet
    Insde beforeTestMethod yy
    
    ===============================================
    SingleSuite
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================
    

    You are probably omitting some information.