Minor fixes for Trait subject

This commit is contained in:
Alex Golub 2023-01-15 22:13:35 +02:00
parent 3d36abe4f8
commit d2eb17077f
10 changed files with 109 additions and 108 deletions

View File

@ -1,8 +1,8 @@
package com.baeldung.traits
trait AnimalTrait {
String basicBehavior() {
return "Animalistic!!"
}
}
}

View File

@ -1,9 +1,8 @@
package com.baeldung.traits
class Dog implements WalkingTrait, SpeakingTrait {
String speakAndWalk() {
WalkingTrait.super.speakAndWalk()
}
}
}

View File

@ -1,12 +1,14 @@
package com.baeldung.traits
class Employee implements UserTrait {
@Override
String name() {
return 'Bob'
}
@Override
String lastName() {
return "Marley"
}
}
}

View File

@ -1,6 +1,6 @@
package com.baeldung.traits
interface Human {
String lastName()
}
}

View File

@ -1,13 +1,12 @@
package com.baeldung.traits
trait SpeakingTrait {
String basicAbility() {
return "Speaking!!"
}
String speakAndWalk() {
return "Speak and walk!!"
}
}
}

View File

@ -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
}

View File

@ -1,9 +1,9 @@
package com.baeldung
trait VehicleTrait extends WheelTrait {
String showWheels() {
return "Num of Wheels $noOfWheels"
return "Num of Wheels $noOfWheels"
}
}
}

View File

@ -1,13 +1,13 @@
package com.baeldung.traits
trait WalkingTrait {
String basicAbility() {
return "Walking!!"
}
String speakAndWalk() {
return "Walk and speak!!"
}
}
}

View File

@ -1,7 +1,6 @@
package com.baeldung
trait WheelTrait {
int noOfWheels
}
}

View File

@ -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!!"
}
}
}