Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Destructor or finally? Which is best in Java? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I'm a C++ guy, and I'm writing a Java app to test part of a product...
I already know I could do this by adding an extra function call, but I was looking for other ways to handle this -- basically I have a Runtest() function where I pass the test I want to run along with the test number... I want to print a header and footer with the test number, but I have multiple return statements in the Runtest() function.
Code:
	public static boolean RunTest( 			   final String  testNum,
								  pkgPreTestSetup.PreTestSetup  preTestSetup,
										pkgTestSteps.TestSteps  testSteps,
							pkgExpectedResults.ExpectedResults  expectedResults )
	{
		cout( "**********************************************************************" );
		cout( "***  Starting test # " + testNum );
		cout( "**********************************************************************" );

		preTestSetup.SetTestNum( testNum );
		if ( preTestSetup.Run() == false )
		{
			cout( "ERROR Pre-Test Setup failed for test #" + testNum );
			return false;
		}

		testSteps.SetTestNum( testNum );
		if ( testSteps.Run() == false )
		{
			cout( "ERROR Test Steps failed for test #" + testNum );
			return false;
		}

		if ( expectedResults.Run() == false )
		{
			cout( "ERROR Expected Results failed for test #" + testNum );
			cout( "Expected: '" + expectedResults.ExpectedResultsString() + "'" );
			cout( "Actual Results: '" + expectedResults.ActualResultsString() + "'" );
			return false;
		}

		cout( "Test #" + testNum + " passed." );
		return true;
	}
In C++ I could put the header/footer code in another class with the header printed in the constructor and footer printed in the destructor, but I don't think the Java destructors get called as soon as the object goes out of scope. A friend suggested putting the whole thing in a try/finally block with the footer printed in the finally section.

I was wondering if there are any other ways to guarantee that the footer is always printed as soon as the function returns?
 
I don't line any of those. I'd create and end method and would call it, including the log and the return statement.

Or write it in the call to the Runtest function.

Cheers,
Dian
 
have you thought about using junit to do unit testing? it's the most popular testing tool in the java community.

if you decide not to use junit (though if you haven't invested too much time so far i would strongly recommend it), you've got a few options. First off, as far as destructors are concerned: you've probably heard that java doesn't have them. not exactly true. you can implement "finalize()", but you are completely correct in your thought that they're not called when they go out of scope- rather, the method is called when an object is garbage collected, and who knows when that'll be. so that's not a good option. (i haven't met a developer yet who has had a reason to implement it, and many java developers with a master's degree don't even know about "finalize()".)

second possibility- have a single point of return, but set a boolean indicating the return value.
Code:
    public static boolean RunTest(                final String  testNum,
                                  pkgPreTestSetup.PreTestSetup  preTestSetup,
                                        pkgTestSteps.TestSteps  testSteps,
                            pkgExpectedResults.ExpectedResults  expectedResults )
    {
        // success state:
        String message = "Test #" + testNum + " passed.";
        boolean returnVal = true;

        cout( "**********************************************************************" );
        cout( "***  Starting test # " + testNum );
        cout( "**********************************************************************" );

        preTestSetup.SetTestNum( testNum );
        if ( preTestSetup.Run() == false )
        {
            message = "ERROR Pre-Test Setup failed for test #" + testNum ;
            returnVal = false;
        }

        ...

        cout( message );
        return retrnVal;
    }

another option is to break out the header and footer into methods of their own, and call these before and after calling RunTest (or inside of RunTest, and .

but again, I'd like to suggest something like junit. not only does it handle much of your test code for you, it also helps keep test logic and the display of test results separate, which alleviates your problem.

liam morley
 
Thanks. My friend also suggested JUnit. When I get some time, I'll check it out.
 
JUnit will save you time in the long run. Not only will it handle all the 'plumbing', but allow you to organise and categorise your tests too. It plugs in to most IDEs as well, and you can even incorporate it into your Ant build. Suggest you check it out now rather than later...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top