Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@BeforeClass of superclass is not executed #394

Closed
gonzen opened this issue Mar 6, 2012 · 7 comments
Closed

@BeforeClass of superclass is not executed #394

gonzen opened this issue Mar 6, 2012 · 7 comments

Comments

@gonzen
Copy link

gonzen commented Mar 6, 2012

Contrary to the statement in @BeforeClass annotation javadoc :
/** The @BeforeClass methods of superclasses will be run before those the current class. */
The @BeforeClass annotated method of superclass is not called if there is another @BeforeClass is subclass.

public abstract class SuperTest {
    @BeforeClass
    public static void beforeClass() throws Exception {
        fail("why don't you call me ?");    // never fails
    }
}

public class MyTestA extends SuperTest {
    @BeforeClass
    public static void beforeClass() {
        fail("Thanks for calling");     // fails here
    }

    @Test
    public void test() {
        fail("Failure expected from MyTestA.beforeClass()");  
    }
}

public class MyTestB extends SuperTest {
    @BeforeClass
    public static void beforeClass() {
    }

    @Test
    public void test() {
        fail("Failure expected from SuperTest.beforeClass()");    // fails here
    }
}
@taral
Copy link

taral commented Mar 6, 2012

The method in the superclass is shadowed by the method with the same name in the subclass. If you give the @BeforeClass methods different names, they will run in the correct order.

@gonzen
Copy link
Author

gonzen commented Mar 7, 2012

thanks taral, issue is settled then. A little mention in the javadoc of @BeforeClass could be helpful though.

@gonzen gonzen closed this as completed Mar 7, 2012
@amalakar
Copy link
Contributor

I learnt the same thing the hard way. As @gonzen has said a little mention in javadoc could help us avoid the situation altogether. If it makes sense I can add it in the javadoc and submit a patch.

@dsaff
Copy link
Member

dsaff commented Sep 14, 2012

@amalakar, a javadoc patch would definitely be appreciated!

amalakar added a commit to amalakar/junit that referenced this issue Sep 14, 2012
@amalakar
Copy link
Contributor

Here is the pull request: https://github.com/KentBeck/junit/pull/504

amalakar added a commit to amalakar/junit that referenced this issue Sep 17, 2012
… special case when subclass overrides/shadows superclass methods
dsaff pushed a commit that referenced this issue Sep 20, 2012
[Issue #394] Added javadoc mentioning special case when subclass overrides/shadows superclass methods
@gvazqu1
Copy link

gvazqu1 commented Apr 1, 2014

Me too.

@cirosantilli
Copy link

I with there was some big note on: http://junit.sourceforge.net/javadoc/org/junit/Before.html saying that you should see http://junit.org/javadoc/latest/ instead, + a link to the new docs from the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants