home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (AssertTest.groovy)

This example Groovy source code file (AssertTest.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

assert, assert, assertionerror, assertionerror, asserttest, groovytestcase, groovytestcase, should, should

The Groovy AssertTest.groovy source code

/*
 * Copyright 2003-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy

class AssertTest extends GroovyTestCase {

    void testAssert() {
        def x = null

        assert x == null
        assert x != "abc"
        assert x != "foo"

        x = "abc"

        assert x != "foo"
        assert x != null
        assert x != "def"
        assert x == "abc"

        assert x.equals("abc")

        assert !x.equals("def")
        assert !false
        assert !(1 == 2)
        assert !(1 > 3)
        assert !(1 != 1)
    }

    void testAssertFail() {
        def x = 1234

        def runCode = false
        try {
            runCode = true
            assert x == 5

            fail("Should have thrown an exception")
        }
        catch (AssertionError e) {
            //msg = "Expression: (x == 5). Values: x = 1234"
            //assert e.getMessage() == msg
            //assert e.message == msg
        }
        assert runCode, "has not ran the try / catch block code"
    }

    void testAssertMessageAndExpressionReversed() {
        try {
            // the order is wrong for this one ""=="" becomes
            // the assertion message string. Since it is an
            // expression it must made sure the expression is
            // transformed into an object
            assert "": "" == ""
        } catch (AssertionError ae) {
            assert ae.message.contains("true");
        }
    }

    // GROOVY-1769
    void testAssertWithNewlineAfterColonOrComma() {
        assert true:
            "Assert on newline after colon"

        assert true,
            "Assert on newline after comma"
    }
}

Other Groovy examples (source code examples)

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

new blog posts

 

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