This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Sep 10, 2008 1:33 PM by 807589 RSS

JUnit 4

807589 Newbie
Currently Being Moderated
Hi all,
I’m writing a test unit class, using JUnit 4. I have a variable that needs to be shared among all @Test methods; since I can not pass any parameter to any of the @Test methods, is there any other way that this variable can be shared among all @Test methods except that defining this variable as a static variable?

Any help is greatly appreciated.
  • 1. Re: JUnit 4
    796254 Newbie
    Currently Being Moderated
    you can make it a member of the class. all methods have access to it then. need not be static.

    %
  • 2. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    Thanks for the reply. If I’m not mistaking, by member of a class, you mean define that variable as an instance field; the point is that I’m not creating a new instance of my class anywhere in the code, and even if I create that instance, still can not pass that created object to all @Test methods, so they still won’t have access to it.
    // A new ReportTest won’t be created, even if thats get created, how can I share that object among all @Test methods?
    
    // All the @Test methods do some test on xmlNode, so all need to have access to it, but each can not create it themselves as creating this object is expensive
    
    public class ReportTest{
         private Node xmlNode = null;
    
    @Test public void testNode_0(){
    }
    
    @Test public void testNode_1{
    }
    Edited by: RonitT on Sep 10, 2008 11:26 AM
  • 3. Re: JUnit 4
    796254 Newbie
    Currently Being Moderated
    i'd create it in setup once and get rid of it in teardown.

    %
  • 4. Re: JUnit 4
    800322 Newbie
    Currently Being Moderated
    But JUnit creates a new instance of your test class.

    Although I'm not sure whether it re-creates one for each new test, so storing stuff in attributes wouldn't help. Try it.
  • 5. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    CeciNEstPasUnProgrammeur wrote:
    Although I'm not sure whether it re-creates one for each new test
    No, just one. That's why it's typically important to override the setUp() and tearDown() methods with appropriate initialization code (when appropriate). It helps keep everything clean and tidy.

    ~
  • 6. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    creating this object is expensive
    In dollars?
  • 7. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    Thanks a lot for all replies. I can have a setup() method, that I need to run only once, so I can use a method with @BeforeClass annotation. I can create an instance if my class in that setup() method and initialize the xmlNode filed for that object; however, when I get to @Test methods, they start running the test on an instance of the class which is NOT initialized and so its null. I can not share the initialized object among all @Test methods.

    Sorry if my question has already been answered and I’m missing a point here…
  • 8. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    I think you want to use parameterised tests: [http://www.devx.com/Java/Article/31983]
  • 9. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    Thanks for the reply. That ex. uses:
    private static Calculator calculator =  new Calculator();
    In order to share the "calculator' among all @Test methods, which is what I mentioned in my original posting: how can I share a variable among all @Test methods except that defining it as static variable.
  • 10. Re: JUnit 4
    mlk Newbie
    Currently Being Moderated
    Sorry, confused. What is wrong with a static variable and @BeforeClass?
  • 11. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    RonitT wrote:
    Thanks for the reply. That ex. uses:
    private static Calculator calculator =  new Calculator();
    In order to share the "calculator' among all @Test methods, which is what I mentioned in my original posting: how can I share a variable among all @Test methods except that defining it as static variable.
    I avoid static fields because they leak between tests. I prefer to use instance fields, but I can't stop you from using static fields.
  • 12. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    Sorry, confused. What is wrong with a static variable and @BeforeClass?
    Nothing is wrong with that. I can define the xmlNode as a static varaible that can be shared among all @Test methods, but the point is that, each ReportTest class creates this node based on its own characteristics, on the other word, this xmlNode needs to be unique for each instance of the ReportTest class; now if only one instance of this class is running, then defining that variable as static won't cause any problem; however, if multiple instances of this test class running concurrently, then all share the same static xmlNode among themselves which is not correct. Since its the test class, I assume that the chances of running multiple instances concurrently in the same JVM is slim, so define this node as static won't cause problems.

    Edited by: RonitT on Sep 10, 2008 12:59 PM

    Edited by: RonitT on Sep 10, 2008 1:00 PM
  • 13. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    I avoid static fields because they leak between tests. I prefer to use instance fields, but I can't stop you from using static fields.
    Then how do you share this instance field(s) among all @Test methods if you need to do so when you can not send any parameter to @Test methods either?
  • 14. Re: JUnit 4
    807589 Newbie
    Currently Being Moderated
    Ok, this is the answer: I need to get rid of the static variable and learn and use: org.junit.runners Class Parameterized instead.

    I just noticed this Class in JUnit API; it’s even mentioned in one of the replies! Sorry for my lack of attention.

    Thanks all for your time and all replies. Greatly appreciated.
1 2 Previous Next