CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: junit onetime setup,teardown.

  1. #1
    Join Date
    Apr 2003

    junit onetime setup,teardown.

    Hi, all

    while studying abt junit programming, i come across one time setup, teardown methods.

    i think the following sample code from oreilly book works fine.
    but how does this one time call is happening exactly?

    i am really confused.

    Thanks 4 any ideas.

    package com.oreilly.javaxp.junit;
    import com.oreilly.javaxp.common.Person;
    import junit.extensions.TestSetup;
    import junit.framework.Test;
    import junit.framework.TestCase;
    import junit.framework.TestSuite;
    public class TestPerson extends TestCase {
    public void testGetFullName( ) { ... }
    public void testNullsInName( ) { ... }
    public static Test suite( ) {
    TestSetup setup = new TestSetup(new TestSuite(TestPerson.class)) {
    protected void setUp( ) throws Exception {
    // do your one-time setup here!
    protected void tearDown( ) throws Exception {
    // do your one-time tear down here!
    return setup;
    Thanks n Regards
    Harinath Reddy
    Learn Hello World Program
    A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible. There are no prima donnas in engineering. - Freeman Dyson

  2. #2
    Join Date
    Apr 2003
    Los Angeles area

    Re: junit onetime setup,teardown.

    Normally setup/tearDown means that before each testYourMethodName is invoked it will call setup and then after it is done it will call tearDown. TestSetup will call setup and tearDown once for the testsuite.

    TestSetup is a class that is meant to be subclassed, and in the example given it is done in what is called "inline" much like you might provide an inline class definition for an event handler. You don't really need to use TestSetup as TestCase provides setup and teardown for each test case method but here is the FAQ on it. In fact it says if you are using this then there might be a design issue with your program for needing this extra initializations. This is because tests should be independent of each other to ensure modularity in test results. If it isn't then a test could pass because of this outside initialization without the programmer realizing it. This is how I understand it.


    I think the morale of the story is don't be lazy when writing tests otherwise why are you bothering in the first place. Test cases seem to often require redundant code. You can use your debugger like normal when running tests and follow the execution path if you are still confused.
    "The Chicken and Rice MRE is not a personal lubricant."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)