• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

wrong @BeforeClass behaviour

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got a JUnit test called AdminFacadeBeanTest which extends AdminFacadeBeanTestBase, and both have a method with a @BeforeClass annotation.

According to the javadoc I found at http://api.dpml.net/junit/4.2/org/junit/BeforeClass.html, "The @BeforeClass methods of superclasses will be run before those the current class."

However, when I run AdminFacadeBeanTest in Eclipse, AdminFacadeBeanTestBase's @BeforeClass method is not being run and I have to call AdminFacadeBeanTestBase.setUpBeforeClass() manually.





Output:

in AdminFacadeBeanTest



Any idea why AdminFacadeBeanTestBase.setUpBeforeClass() is not being run automatically?
[ April 22, 2008: Message edited by: Jan Van den bosch ]
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've passed this same situation.
The method annotated with @BeforeClass can not have the same name in both classes. The same applies to methos annotated with @AfterClass.
Rename the method in the subclass, so that it has a different name from the one in the superclass.
 
Jan Van den bosch
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's... pretty lame.

Thanks though!
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jan Van den bosch:
That's... pretty lame.



No, that's overriding.

By overriding the @BeforeClass annotated method, you hid it from the world outside and took over the responsibility to call it, if needed.
 
Rodrigo Lopes
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But static methods are not overriden...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid forgetting to call them yourself you can just mark your BeforeClass methods final. You just can't reuse the names.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by R Lopes:
But static methods are not overriden...



You're right, it's shadowing.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic