From d2eb17077fb30cd5123cd243e95b73e5b5d06b88 Mon Sep 17 00:00:00 2001 From: Alex Golub Date: Sun, 15 Jan 2023 22:13:35 +0200 Subject: [PATCH] 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 +}