Coder Social home page Coder Social logo

@BeforeClass rule about junit4 HOT 8 CLOSED

junit-team avatar junit-team commented on August 22, 2024
@BeforeClass rule

from junit4.

Comments (8)

aisrael avatar aisrael commented on August 22, 2024

I'm recklessly trying to do this myself:

  1. Added org.junit.rules.ClassRule, with apply(Statement next, TestClass testClass).
  2. Modified ParentRunner#classBlock() to call withClassRules() which looks for fields that are public, static, and implement ClassRule.
  3. For each of those fields, call apply() to append to the statement 'chain'.
  4. Also, modify BlockJUnit4ClassRunner to only apply @Rule fields that implement MethodRule.

Am I on the right track?

Here're my changes:
http://github.com/AlistairIsrael/junit/commit/79ef5a7e1d7fa144cc81c9414f4791aaea8b3d75

For the record, it works. I've successfully split a Derby+Hibernate+DbUnit @Rule into a static, ClassRule that initializes Derby and Hibernate, and a regular MethodRule that delegates to DbUnit loading/unloading of database fixtures using a local 'build'.

from junit4.

brolund avatar brolund commented on August 22, 2024

Hi,
I think I did a similar thing in my fork http://github.com/brolund/junit

In addition to what you describe, my goal was to be able to register rules for methods, classes and suites on a root suite and then be able to propagate them to an entire suite hierarchy. My primary reason is to be able to generate documentation like in my pet project
http://www.agical.com/bumblebee/bumblebee_doc.html
but without having to make my own suite implementations.

Of course, this approach could be used for a number of reasons. Suite-scope dependency injection, logging, resource creation and management. It won't work well for running parallel tests (as of now).

from junit4.

aisrael avatar aisrael commented on August 22, 2024

Yup, the natural extension of rules on methods, then rules on classes, would've been rules on suites. My other suggestion along this line was to let rules be declaratively specified, without the end-user having to instantiate a no-arg, non-interactive rule.

I also noticed you used the RunNotifier to pass around 'ancestry' because of the "lack of a context-like structure in JUnit". I ran into a similar issue when I experimented with declarative, class-level rules. There was no easy way for the ParentRunner to pass the test instance, and instances of rules to children.

So I stopped myself there, and contented myself with the smallest change that could apply to my still limited needs. I tend to agree, though, that JUnit might need some kind of "test context" to pass around in the future.

from junit4.

maxb avatar maxb commented on August 22, 2024

Hi. I've just discovered the problem of a lack of a class-based @rule myself, and located this message: http://tech.groups.yahoo.com/group/junit/message/22522 - I notice there's been no activity on this issue since. Is there any likelyhood of it landing soon? Is there anything I can do to help it along?

from junit4.

aisrael avatar aisrael commented on August 22, 2024

Just an update, updated my class rules tree and 'pulled' from latest JUnit master in to http://github.com/AlistairIsrael/junit/tree/435195876d559ec780e21f4f53e8db6849a65b23

from junit4.

dsaff avatar dsaff commented on August 22, 2024

Thanks. Kent and I will review Monday, if not before.

from junit4.

dsaff avatar dsaff commented on August 22, 2024

Fixed and accepted at HEAD

from junit4.

dsaff avatar dsaff commented on August 22, 2024

Close

from junit4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.