From f3a20f71294591544a1283b3da66e67cade6e482 Mon Sep 17 00:00:00 2001 From: Alex Golub Date: Sat, 14 Jan 2023 21:31:29 +0200 Subject: [PATCH 1/2] Align tests with `Spock` testing framework --- .../com/baeldung/strings/Concatenate.groovy | 24 ++- .../baeldung/strings/ConcatenateTest.groovy | 149 ++++++++---------- .../strings/StringMatchingSpec.groovy | 2 +- .../stringtoint/ConvertStringToInt.groovy | 131 ++++++++------- .../stringtypes/CharacterInGroovy.groovy | 17 +- .../stringtypes/DoubleQuotedString.groovy | 63 ++++---- .../stringtypes/SingleQuotedString.groovy | 15 +- .../com/baeldung/stringtypes/Strings.groovy | 21 +-- 8 files changed, 218 insertions(+), 204 deletions(-) diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/strings/Concatenate.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/strings/Concatenate.groovy index b3a0852a0b..4a7c3f8529 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/strings/Concatenate.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/strings/Concatenate.groovy @@ -1,9 +1,10 @@ package com.baeldung.strings; class Concatenate { + String first = 'Hello' String last = 'Groovy' - + String doSimpleConcat() { return 'My name is ' + first + ' ' + last } @@ -23,21 +24,28 @@ class Concatenate { String doConcatUsingLeftShiftOperator() { return 'My name is ' << first << ' ' << last } - + String doConcatUsingArrayJoinMethod() { return ['My name is', first, last].join(' ') } String doConcatUsingArrayInjectMethod() { - return [first,' ', last] - .inject(new StringBuffer('My name is '), { initial, name -> initial.append(name); return initial }).toString() + return [first, ' ', last] + .inject(new StringBuffer('My name is '), { initial, name -> initial.append(name) }) + .toString() } - + String doConcatUsingStringBuilder() { - return new StringBuilder().append('My name is ').append(first).append(' ').append(last) + return new StringBuilder().append('My name is ') + .append(first) + .append(' ') + .append(last) } String doConcatUsingStringBuffer() { - return new StringBuffer().append('My name is ').append(first).append(' ').append(last) + return new StringBuffer().append('My name is ') + .append(first) + .append(' ') + .append(last) } -} \ No newline at end of file +} diff --git a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/strings/ConcatenateTest.groovy b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/strings/ConcatenateTest.groovy index 3ef4a5d460..e4a178a14b 100644 --- a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/strings/ConcatenateTest.groovy +++ b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/strings/ConcatenateTest.groovy @@ -1,101 +1,88 @@ -import com.baeldung.strings.Concatenate; +import com.baeldung.strings.Concatenate +import spock.lang.Specification -class ConcatenateTest extends GroovyTestCase { - - void testSimpleConcat() { - def name = new Concatenate() - name.first = 'Joe'; - name.last = 'Smith'; - def expected = 'My name is Joe Smith' - assertToString(name.doSimpleConcat(), expected) - } - - void testConcatUsingGString() { - def name = new Concatenate() - name.first = "Joe"; - name.last = "Smith"; - def expected = "My name is Joe Smith" - assertToString(name.doConcatUsingGString(), expected) - } - - void testConcatUsingGStringClosures() { - def name = new Concatenate() - name.first = "Joe"; - name.last = "Smith"; - def expected = "My name is Joe Smith" - assertToString(name.doConcatUsingGStringClosures(), expected) - } - - void testConcatUsingStringConcatMethod() { - def name = new Concatenate() - name.first = "Joe"; - name.last = "Smith"; - def expected = "My name is Joe Smith" - assertToString(name.doConcatUsingStringConcatMethod(), expected) +class ConcatenateTest extends Specification { + + final Concatenate NAME = new Concatenate(first: 'Joe', last: 'Smith') + final String EXPECTED = "My name is Joe Smith"; + + def "SimpleConcat"() { + expect: + NAME.doSimpleConcat() == EXPECTED } - void testConcatUsingLeftShiftOperator() { - def name = new Concatenate() - name.first = "Joe"; - name.last = "Smith"; - def expected = "My name is Joe Smith" - assertToString(name.doConcatUsingLeftShiftOperator(), expected) + def "ConcatUsingGString"() { + expect: + NAME.doConcatUsingGString() == EXPECTED } - void testConcatUsingArrayJoinMethod() { - def name = new Concatenate() - name.first = "Joe"; - name.last = "Smith"; - def expected = "My name is Joe Smith" - assertToString(name.doConcatUsingArrayJoinMethod(), expected) + def "ConcatUsingGStringClosures"() { + expect: + NAME.doConcatUsingGStringClosures() == EXPECTED } - void testConcatUsingArrayInjectMethod() { - def name = new Concatenate() - name.first = "Joe"; - name.last = "Smith"; - def expected = "My name is Joe Smith" - assertToString(name.doConcatUsingArrayInjectMethod(), expected) + def "ConcatUsingStringConcatMethod"() { + expect: + NAME.doConcatUsingStringConcatMethod() == EXPECTED } - void testConcatUsingStringBuilder() { - def name = new Concatenate() - name.first = "Joe"; - name.last = "Smith"; - def expected = "My name is Joe Smith" - assertToString(name.doConcatUsingStringBuilder(), expected) + def "ConcatUsingLeftShiftOperator"() { + expect: + NAME.doConcatUsingLeftShiftOperator() == EXPECTED } - void testConcatUsingStringBuffer() { - def name = new Concatenate() - name.first = "Joe"; - name.last = "Smith"; - def expected = "My name is Joe Smith" - assertToString(name.doConcatUsingStringBuffer(), expected) + def "ConcatUsingArrayJoinMethod"() { + expect: + NAME.doConcatUsingArrayJoinMethod() == EXPECTED } - void testConcatMultilineUsingStringConcatMethod() { - def name = new Concatenate() - name.first = '''Joe + def "ConcatUsingArrayInjectMethod"() { + expect: + NAME.doConcatUsingArrayInjectMethod() == EXPECTED + } + + def "ConcatUsingStringBuilder"() { + expect: + NAME.doConcatUsingStringBuilder() == EXPECTED + } + + def "ConcatUsingStringBuffer"() { + expect: + NAME.doConcatUsingStringBuffer() == EXPECTED + } + + def "ConcatMultilineUsingStringConcatMethod"() { + when: + NAME.first = '''Joe Smith - '''; - name.last = 'Junior'; + ''' + NAME.last = 'Junior' + + then: def expected = '''My name is Joe Smith - Junior'''; - assertToString(name.doConcatUsingStringConcatMethod(), expected) + Junior''' + NAME.doConcatUsingStringConcatMethod() == expected } - void testGStringvsClosure(){ - def first = "Joe"; - def last = "Smith"; - def eagerGString = "My name is $first $last" - def lazyGString = "My name is ${-> first} ${-> last}" + def "GStringvsClosure"() { + given: + def eagerGString = "My name is $NAME.first $NAME.last" + def lazyGString = "My name is ${-> NAME.first} ${-> NAME.last}" - assert eagerGString == "My name is Joe Smith" - assert lazyGString == "My name is Joe Smith" - first = "David"; - assert eagerGString == "My name is Joe Smith" - assert lazyGString == "My name is David Smith" - } + expect: + eagerGString == "My name is Joe Smith" + lazyGString == "My name is Joe Smith" + } + + def "LazyVsEager"() { + given: + def eagerGString = "My name is $NAME.first $NAME.last" + def lazyGString = "My name is ${-> NAME.first} ${-> NAME.last}" + NAME.first = "David" + + expect: + eagerGString == "My name is Joe Smith" + lazyGString == "My name is David Smith" + } } diff --git a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/strings/StringMatchingSpec.groovy b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/strings/StringMatchingSpec.groovy index 3865bc73fa..89202d9500 100644 --- a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/strings/StringMatchingSpec.groovy +++ b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/strings/StringMatchingSpec.groovy @@ -13,7 +13,7 @@ class StringMatchingSpec extends Specification { expect: p instanceof Pattern - and: "you can use slash strings to avoid escaping of blackslash" + and: "you can use slash strings to avoid escaping of backslash" def digitPattern = ~/\d*/ digitPattern.matcher('4711').matches() } diff --git a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtoint/ConvertStringToInt.groovy b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtoint/ConvertStringToInt.groovy index 48cf48fa8a..cce6ede989 100644 --- a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtoint/ConvertStringToInt.groovy +++ b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtoint/ConvertStringToInt.groovy @@ -1,110 +1,119 @@ package com.baeldung.stringtoint -import org.junit.Test +import spock.lang.Specification import java.text.DecimalFormat -import static org.junit.Assert.assertEquals -import static org.junit.Assert.assertNull +class ConvertStringToInt extends Specification { -class ConvertStringToInt { + final String STRING_NUM = "123" + final int EXPECTED_INT = 123 - @Test - void givenString_whenUsingAsInteger_thenConvertToInteger() { - def stringNum = "123" + def "givenString_whenUsingAsInteger_thenConvertToInteger"() { + given: def invalidString = "123a" - Integer expectedInteger = 123 - Integer integerNum = stringNum as Integer + Integer integerNum = STRING_NUM as Integer + + when: def intNum = invalidString?.isInteger() ? invalidString as Integer : null - assertNull(null, intNum) - assertEquals(integerNum, expectedInteger) + then: + intNum == null + integerNum == EXPECTED_INT } - @Test - void givenString_whenUsingAsInt_thenConvertToInt() { - def stringNum = "123" - int expectedInt = 123 - int intNum = stringNum as int + def "givenString_whenUsingAsInt_thenConvertToInt"() { + given: + int intNum = STRING_NUM as int - assertEquals(intNum, expectedInt) + expect: + intNum == EXPECTED_INT } - @Test - void givenString_whenUsingToInteger_thenConvertToInteger() { - def stringNum = "123" - int expectedInt = 123 - int intNum = stringNum.toInteger() + def "givenString_whenUsingToInteger_thenConvertToInteger"() { + given: + int intNum = STRING_NUM.toInteger() - assertEquals(intNum, expectedInt) + expect: + intNum == EXPECTED_INT } - @Test - void givenString_whenUsingParseInt_thenConvertToInteger() { - def stringNum = "123" - int expectedInt = 123 - int intNum = Integer.parseInt(stringNum) + def "givenString_whenUsingParseInt_thenConvertToInteger"() { + given: + int intNum = Integer.parseInt(STRING_NUM) - assertEquals(intNum, expectedInt) + expect: + intNum == EXPECTED_INT } - @Test - void givenString_whenUsingValueOf_thenConvertToInteger() { - def stringNum = "123" - int expectedInt = 123 - int intNum = Integer.valueOf(stringNum) + def "givenString_whenUsingValueOf_thenConvertToInteger"() { + given: + int intNum = Integer.valueOf(STRING_NUM) - assertEquals(intNum, expectedInt) + expect: + intNum == EXPECTED_INT } - @Test - void givenString_whenUsingIntValue_thenConvertToInteger() { - def stringNum = "123" - int expectedInt = 123 - int intNum = new Integer(stringNum).intValue() + def "givenString_whenUsingIntValue_thenConvertToInteger"() { + given: + int intNum = Integer.valueOf(STRING_NUM).intValue() - assertEquals(intNum, expectedInt) + expect: + intNum == EXPECTED_INT } - @Test - void givenString_whenUsingNewInteger_thenConvertToInteger() { - def stringNum = "123" - int expectedInt = 123 - int intNum = new Integer(stringNum) + def "givenString_whenUsingNewInteger_thenConvertToInteger"() { + given: + Integer intNum = Integer.valueOf(STRING_NUM) - assertEquals(intNum, expectedInt) + expect: + intNum == EXPECTED_INT } - @Test - void givenString_whenUsingDecimalFormat_thenConvertToInteger() { - def stringNum = "123" - int expectedInt = 123 + def "givenString_whenUsingDecimalFormat_thenConvertToInteger"() { + given: DecimalFormat decimalFormat = new DecimalFormat("#") - int intNum = decimalFormat.parse(stringNum).intValue() - assertEquals(intNum, expectedInt) + when: + int intNum = decimalFormat.parse(STRING_NUM).intValue() + + then: + intNum == EXPECTED_INT } - @Test(expected = NumberFormatException.class) - void givenInvalidString_whenUsingAs_thenThrowNumberFormatException() { + def "givenInvalidString_whenUsingAs_thenThrowNumberFormatException"() { + given: def invalidString = "123a" + + when: invalidString as Integer + + then: + thrown(NumberFormatException) } - @Test(expected = NullPointerException.class) - void givenNullString_whenUsingToInteger_thenThrowNullPointerException() { + def "givenNullString_whenUsingToInteger_thenThrowNullPointerException"() { + given: def invalidString = null + + when: invalidString.toInteger() + + then: + thrown(NullPointerException) } - @Test - void givenString_whenUsingIsInteger_thenCheckIfCorrectValue() { + def "givenString_whenUsingIsInteger_thenCheckIfCorrectValue"() { + given: def invalidString = "123a" def validString = "123" + + when: def invalidNum = invalidString?.isInteger() ? invalidString as Integer : false def correctNum = validString?.isInteger() ? validString as Integer : false - assertEquals(false, invalidNum) - assertEquals(123, correctNum) + then: + !invalidNum + correctNum == 123 } } diff --git a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/CharacterInGroovy.groovy b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/CharacterInGroovy.groovy index c043723d95..30365ec9d7 100644 --- a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/CharacterInGroovy.groovy +++ b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/CharacterInGroovy.groovy @@ -1,19 +1,18 @@ package groovy.com.baeldung.stringtypes -import org.junit.Assert -import org.junit.Test +import spock.lang.Specification -class CharacterInGroovy { +class CharacterInGroovy extends Specification { - @Test - void 'character'() { + def 'character'() { + given: char a = 'A' as char char b = 'B' as char char c = (char) 'C' - Assert.assertTrue(a instanceof Character) - Assert.assertTrue(b instanceof Character) - Assert.assertTrue(c instanceof Character) + expect: + a instanceof Character + b instanceof Character + c instanceof Character } - } diff --git a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/DoubleQuotedString.groovy b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/DoubleQuotedString.groovy index a730244d0a..e1074145f4 100644 --- a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/DoubleQuotedString.groovy +++ b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/DoubleQuotedString.groovy @@ -1,67 +1,76 @@ package groovy.com.baeldung.stringtypes -import org.junit.Assert -import org.junit.Test +import spock.lang.Specification -class DoubleQuotedString { +class DoubleQuotedString extends Specification { - @Test - void 'escape double quoted string'() { + def 'escape double quoted string'() { + given: def example = "Hello \"world\"!" - println(example) + expect: + example == 'Hello "world"!' } - @Test - void 'String ang GString'() { + def 'String ang GString'() { + given: def string = "example" def stringWithExpression = "example${2}" - Assert.assertTrue(string instanceof String) - Assert.assertTrue(stringWithExpression instanceof GString) - Assert.assertTrue(stringWithExpression.toString() instanceof String) + expect: + string instanceof String + stringWithExpression instanceof GString + stringWithExpression.toString() instanceof String } - @Test - void 'placeholder with variable'() { + def 'placeholder with variable'() { + given: def name = "John" + + when: def helloName = "Hello $name!".toString() - Assert.assertEquals("Hello John!", helloName) + then: + helloName == "Hello John!" } - @Test - void 'placeholder with expression'() { + def 'placeholder with expression'() { + given: def result = "result is ${2 * 2}".toString() - Assert.assertEquals("result is 4", result) + expect: + result == "result is 4" } - @Test - void 'placeholder with dotted access'() { + def 'placeholder with dotted access'() { + given: def person = [name: 'John'] + when: def myNameIs = "I'm $person.name, and you?".toString() - Assert.assertEquals("I'm John, and you?", myNameIs) + then: + myNameIs == "I'm John, and you?" } - @Test - void 'placeholder with method call'() { + def 'placeholder with method call'() { + given: def name = 'John' + when: def result = "Uppercase name: ${name.toUpperCase()}".toString() - Assert.assertEquals("Uppercase name: JOHN", result) + then: + result == "Uppercase name: JOHN" } - @Test - void 'GString and String hashcode'() { + def 'GString and String hashcode'() { + given: def string = "2+2 is 4" def gstring = "2+2 is ${4}" - Assert.assertTrue(string.hashCode() != gstring.hashCode()) + expect: + string.hashCode() != gstring.hashCode() } - } diff --git a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/SingleQuotedString.groovy b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/SingleQuotedString.groovy index 569991b788..924515570c 100644 --- a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/SingleQuotedString.groovy +++ b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/SingleQuotedString.groovy @@ -1,15 +1,14 @@ package groovy.com.baeldung.stringtypes -import org.junit.Assert -import org.junit.Test +import spock.lang.Specification -class SingleQuotedString { +class SingleQuotedString extends Specification { - @Test - void 'single quoted string'() { - def example = 'Hello world' + def 'single quoted string'() { + given: + def example = 'Hello world!' - Assert.assertEquals('Hello world!', 'Hello' + ' world!') + expect: + example == 'Hello' + ' world!' } - } diff --git a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/Strings.groovy b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/Strings.groovy index e45f352285..9d29210d81 100644 --- a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/Strings.groovy +++ b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/stringtypes/Strings.groovy @@ -1,26 +1,29 @@ package groovy.com.baeldung.stringtypes -import org.junit.Assert -import org.junit.Test +import spock.lang.Specification -class Strings { +class Strings extends Specification { - @Test - void 'string interpolation '() { + def 'string interpolation '() { + given: def name = "Kacper" + when: def result = "Hello ${name}!" - Assert.assertEquals("Hello Kacper!", result.toString()) + then: + result.toString() == "Hello Kacper!" } - @Test - void 'string concatenation'() { + def 'string concatenation'() { + given: def first = "first" def second = "second" + when: def concatenation = first + second - Assert.assertEquals("firstsecond", concatenation) + then: + concatenation == "firstsecond" } } From f2c9fbde810b3708557553c83bc5dda902a87059 Mon Sep 17 00:00:00 2001 From: Alex Golub Date: Sun, 15 Jan 2023 22:13:35 +0200 Subject: [PATCH 2/2] Minor fixes for Trait subject --- .../com/baeldung/traits/AnimalTrait.groovy | 4 +- .../groovy/com/baeldung/traits/Dog.groovy | 5 +- .../com/baeldung/traits/Employee.groovy | 6 +- .../groovy/com/baeldung/traits/Human.groovy | 4 +- .../com/baeldung/traits/SpeakingTrait.groovy | 7 +- .../com/baeldung/traits/UserTrait.groovy | 25 ++- .../com/baeldung/traits/VehicleTrait.groovy | 8 +- .../com/baeldung/traits/WalkingTrait.groovy | 8 +- .../com/baeldung/traits/WheelTrait.groovy | 5 +- .../com/baeldung/traits/TraitsUnitTest.groovy | 145 +++++++++--------- 10 files changed, 109 insertions(+), 108 deletions(-) diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/AnimalTrait.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/AnimalTrait.groovy index 6ec5cda571..a3fed81c8b 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/AnimalTrait.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/AnimalTrait.groovy @@ -1,8 +1,8 @@ package com.baeldung.traits trait AnimalTrait { - + String basicBehavior() { return "Animalistic!!" } -} \ No newline at end of file +} diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Dog.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Dog.groovy index 3e0677ba18..fc53b1bef9 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Dog.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Dog.groovy @@ -1,9 +1,8 @@ package com.baeldung.traits class Dog implements WalkingTrait, SpeakingTrait { - + String speakAndWalk() { WalkingTrait.super.speakAndWalk() } - -} \ No newline at end of file +} diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Employee.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Employee.groovy index b3e4285476..16f1fab984 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Employee.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Employee.groovy @@ -1,12 +1,14 @@ package com.baeldung.traits class Employee implements UserTrait { - + + @Override String name() { return 'Bob' } + @Override String lastName() { return "Marley" } -} \ No newline at end of file +} diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Human.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Human.groovy index e78d59bbfd..5417334269 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Human.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/Human.groovy @@ -1,6 +1,6 @@ package com.baeldung.traits interface Human { - + String lastName() -} \ No newline at end of file +} diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/SpeakingTrait.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/SpeakingTrait.groovy index f437a94bd9..969982e8e0 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/SpeakingTrait.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/SpeakingTrait.groovy @@ -1,13 +1,12 @@ package com.baeldung.traits trait SpeakingTrait { - + String basicAbility() { return "Speaking!!" } - + String speakAndWalk() { return "Speak and walk!!" } - -} \ No newline at end of file +} diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/UserTrait.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/UserTrait.groovy index 0d395bffcd..1d1d188460 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/UserTrait.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/UserTrait.groovy @@ -1,36 +1,35 @@ package com.baeldung.traits trait UserTrait implements Human { - + + String email + String address + + abstract String name() + String sayHello() { return "Hello!" } - - abstract String name() - + String showName() { - return "Hello, ${name()}!" + return "Hello, ${name()}!" } private String greetingMessage() { return 'Hello, from a private method!' } - + String greet() { def msg = greetingMessage() println msg msg } - + def self() { - return this + return this } - + String showLastName() { return "Hello, ${lastName()}!" } - - String email - String address } - \ No newline at end of file diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/VehicleTrait.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/VehicleTrait.groovy index f5ae8fab30..e29561157c 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/VehicleTrait.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/VehicleTrait.groovy @@ -1,9 +1,9 @@ package com.baeldung trait VehicleTrait extends WheelTrait { - + String showWheels() { - return "Num of Wheels $noOfWheels" + return "Num of Wheels $noOfWheels" } - -} \ No newline at end of file + +} diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/WalkingTrait.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/WalkingTrait.groovy index 66cff8809f..84be49ec25 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/WalkingTrait.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/WalkingTrait.groovy @@ -1,13 +1,13 @@ package com.baeldung.traits trait WalkingTrait { - + String basicAbility() { return "Walking!!" } - + String speakAndWalk() { return "Walk and speak!!" } - -} \ No newline at end of file + +} diff --git a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/WheelTrait.groovy b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/WheelTrait.groovy index 364d5b883e..7f2448e185 100644 --- a/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/WheelTrait.groovy +++ b/core-groovy-modules/core-groovy/src/main/groovy/com/baeldung/traits/WheelTrait.groovy @@ -1,7 +1,6 @@ package com.baeldung trait WheelTrait { - + int noOfWheels - -} \ No newline at end of file +} diff --git a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/traits/TraitsUnitTest.groovy b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/traits/TraitsUnitTest.groovy index 85130e8f07..b47cba6437 100644 --- a/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/traits/TraitsUnitTest.groovy +++ b/core-groovy-modules/core-groovy/src/test/groovy/com/baeldung/traits/TraitsUnitTest.groovy @@ -7,108 +7,111 @@ class TraitsUnitTest extends Specification { Employee employee Dog dog - void setup () { + void setup() { employee = new Employee() dog = new Dog() } - def 'Should return msg string when using Employee.sayHello method provided by UserTrait' () { + def 'Should return msg string when using Employee.sayHello method provided by UserTrait'() { when: - def msg = employee.sayHello() + def msg = employee.sayHello() + then: - msg - msg instanceof String - assert msg == "Hello!" + msg + msg instanceof String + msg == "Hello!" } - - def 'Should return displayMsg string when using Employee.showName method' () { + + def 'Should return displayMsg string when using Employee.showName method'() { when: - def displayMsg = employee.showName() + def displayMsg = employee.showName() + then: - displayMsg - displayMsg instanceof String - assert displayMsg == "Hello, Bob!" + displayMsg + displayMsg instanceof String + displayMsg == "Hello, Bob!" } - - def 'Should return greetMsg string when using Employee.greet method' () { + + def 'Should return greetMsg string when using Employee.greet method'() { when: - def greetMsg = employee.greet() + def greetMsg = employee.greet() + then: - greetMsg - greetMsg instanceof String - assert greetMsg == "Hello, from a private method!" + greetMsg + greetMsg instanceof String + greetMsg == "Hello, from a private method!" } - - def 'Should return MissingMethodException when using Employee.greetingMessage method' () { + + def 'Should return MissingMethodException when using Employee.greetingMessage method'() { when: - def exception - try { - employee.greetingMessage() - }catch(Exception e) { - exception = e - } - + employee.greetingMessage() + then: - exception - exception instanceof groovy.lang.MissingMethodException - assert exception.message == "No signature of method: com.baeldung.traits.Employee.greetingMessage()"+ - " is applicable for argument types: () values: []" + thrown(MissingMethodException) + specificationContext.thrownException.message == + "No signature of method: com.baeldung.traits.Employee.greetingMessage() is applicable for argument types: () values: []" } - - def 'Should return employee instance when using Employee.whoAmI method' () { + + def 'Should return employee instance when using Employee.whoAmI method'() { when: - def emp = employee.self() + def emp = employee.self() + then: - emp - emp instanceof Employee - assert emp.is(employee) + emp + emp instanceof Employee + emp.is(employee) } - - def 'Should display lastName when using Employee.showLastName method' () { + + def 'Should display lastName when using Employee.showLastName method'() { when: - def lastNameMsg = employee.showLastName() + def lastNameMsg = employee.showLastName() + then: - lastNameMsg - lastNameMsg instanceof String - assert lastNameMsg == "Hello, Marley!" + lastNameMsg + lastNameMsg instanceof String + lastNameMsg == "Hello, Marley!" } - - def 'Should be able to define properties of UserTrait in Employee instance' () { + + def 'Should be able to define properties of UserTrait in Employee instance'() { when: - employee = new Employee(email: "a@e.com", address: "baeldung.com") + employee = new Employee(email: "a@e.com", address: "baeldung.com") + then: - employee - employee instanceof Employee - assert employee.email == "a@e.com" - assert employee.address == "baeldung.com" + employee + employee instanceof Employee + employee.email == "a@e.com" + employee.address == "baeldung.com" } - - def 'Should execute basicAbility method from SpeakingTrait and return msg string' () { + + def 'Should execute basicAbility method from SpeakingTrait and return msg string'() { when: - def speakMsg = dog.basicAbility() + def speakMsg = dog.basicAbility() + then: - speakMsg - speakMsg instanceof String - assert speakMsg == "Speaking!!" + speakMsg + speakMsg instanceof String + speakMsg == "Speaking!!" } - - def 'Should verify multiple inheritance with traits and execute overridden traits method' () { + + def 'Should verify multiple inheritance with traits and execute overridden traits method'() { when: - def walkSpeakMsg = dog.speakAndWalk() - println walkSpeakMsg + def walkSpeakMsg = dog.speakAndWalk() + println walkSpeakMsg + then: - walkSpeakMsg - walkSpeakMsg instanceof String - assert walkSpeakMsg == "Walk and speak!!" + walkSpeakMsg + walkSpeakMsg instanceof String + walkSpeakMsg == "Walk and speak!!" } - - def 'Should implement AnimalTrait at runtime and access basicBehavior method' () { + + def 'Should implement AnimalTrait at runtime and access basicBehavior method'() { when: - def dogInstance = new Dog() as AnimalTrait - def basicBehaviorMsg = dogInstance.basicBehavior() + def dogInstance = new Dog() as AnimalTrait + def basicBehaviorMsg = dogInstance.basicBehavior() + then: - basicBehaviorMsg - basicBehaviorMsg instanceof String - assert basicBehaviorMsg == "Animalistic!!" + basicBehaviorMsg + basicBehaviorMsg instanceof String + basicBehaviorMsg == "Animalistic!!" } -} \ No newline at end of file +}