Easy mock : Reseting mock objects state


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Easy mock : Reseting mock objects state

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Posts
    4

    Easy mock : Reseting mock objects state

    if I have multiple test cases such as :

    public void testA () {
    ...
    }


    public void testA () {
    ...
    }

    From test case to test case, when are the mock objects reset()? or are they ever reset()? or must I manaully reset them at the end of each test case.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    This doesn't seem to be a java programming question.
    Are you working in a special environment?
    What's a mock object?
    What does "reset an object" mean?

  3. #3
    Join Date
    Sep 2005
    Posts
    4
    I apologize, most of the terminology I was using is pretty standard with the concept of mock objects, but then you would need to know what a mock object is wouldn't you?

    Easymock is a package that I am using to "mock up" objects. If you google "mock objects" you could found oodles of resources. I guess I was hoping for some help from someone that has used "easymock" before...

    thanks

  4. #4
    Join Date
    Aug 2004
    Posts
    46
    What testing framework are you using? If you're using JUnit, just override the protected setUp() method in TestCase and initialize your mocks there. You may also put logic in the tearDown() method to clean up anything that needs it. These methods get called by the framework for *each* test method.

    Example:
    Code:
    public class ExampleTest extends TestCase {
        public ExampleTest(String s) {
            super(s);
        }
    
        protected void setUp() throws Exception {
            // test init goes here...
        }
    
        protected void tearDown() throws Exception {
            // cleanup goes here...
        }
    
        // test methods follow...
    }
    Hope this helps...
    Last edited by yawmark; 09-15-2005 at 09:34 AM.

  5. #5
    Join Date
    Sep 2005
    Posts
    4
    Thanks, I appreciate the help.. so for every test method that is called, the setup and tear down are executed?

    sorry, I just wanted to reiterate over that concept one more time. So if I have 5 test cases, both methods are executed 5 times in the order of:
    setup(), myTestCase(), tearDown()?

  6. #6
    Join Date
    Aug 2004
    Posts
    46
    Thanks, I appreciate the help.. so for every test method that is called, the setup and tear down are executed?
    Yeppers. You can easily check this yourself, you know...

    Code:
    import junit.framework.TestCase;
    
    public class DemoTest extends TestCase {
    
        public DemoTest(String s) {
            super(s);
        }
        
        protected void setUp() throws Exception {
            System.out.println("Setting up");
        }
        
        protected void tearDown() throws Exception {
            System.out.println("Tearing down");        
        }
        
        public void testOne() throws Exception {
            System.out.println("test one...");
        }
    
        public void testTwo() throws Exception {
            System.out.println("test two...");
        }
    }
    Output:

    Setting up
    test two...
    Tearing down
    Setting up
    test one...
    Tearing down

  7. #7
    Join Date
    Sep 2005
    Posts
    4
    k, thanks for the quick reply too

Similar Threads

  1. Java applet to VB
    By Maurice in forum Java
    Replies: 0
    Last Post: 02-11-2003, 02:10 AM
  2. Retaining State in COM+ objects?
    By Stephen in forum VB Classic
    Replies: 3
    Last Post: 08-15-2002, 05:34 PM
  3. Replies: 5
    Last Post: 05-08-2002, 10:23 AM
  4. Confused...Objects versus Datasets
    By Paul F in forum .NET
    Replies: 13
    Last Post: 03-22-2002, 07:10 AM
  5. OO Considered Harmful
    By Richard Dalton . in forum Architecture and Design
    Replies: 33
    Last Post: 07-30-2001, 03:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center