alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (ReturnTest.groovy)

This example Groovy source code file (ReturnTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

groovytestcase, groovytestcase, returntest, returntest

The Groovy ReturnTest.groovy source code

package groovy

/** 
 * Tests the use of returns in Groovy
 * 
 * @author <a href="mailto:james@coredevelopers.net">James Strachan
 * @version $Revision: 4996 $
 */
class ReturnTest extends GroovyTestCase {
    void testIntegerReturnValues() {
        def value = foo(5)
        assert value == 10
    }

    void testBooleanReturnValues() {
        def value = bar(6)
        assert value
    }

    def foo(x) {
        return ( x * 2 )
    }

    def bar(x) {
        return x > 5
    }
    
    void testVoidReturn() {
        explicitVoidReturn()
        implicitVoidReturn()
        explicitVoidReturnWithoutFinalReturn()
        implicitVoidReturnWithoutFinalReturn()
    }

    void explicitVoidReturn() {
        return
    }

    def implicitVoidReturn() {
        return
    }

    void explicitVoidReturnWithoutFinalReturn() {
        def x = 4;
        if (x == 3) {
            return;
        } else {
            try {
                x = 3;
                return;
            } finally {
                //do nothing
            }
        }
    }

    def implicitVoidReturnWithoutFinalReturn() {
        def x = 4;
        if (x == 3) {
            return;
        } else {
            try {
                x = 3;
                return;
            } finally {
                //do nothing
            }
        }
    } 
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ReturnTest.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.