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

@ -5,5 +5,4 @@ class Dog implements WalkingTrait, SpeakingTrait {
String speakAndWalk() {
WalkingTrait.super.speakAndWalk()
}
}

View File

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

View File

@ -9,5 +9,4 @@ trait SpeakingTrait {
String speakAndWalk() {
return "Speak and walk!!"
}
}

View File

@ -2,12 +2,15 @@ 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()}!"
}
@ -29,8 +32,4 @@ trait UserTrait implements Human {
String showLastName() {
return "Hello, ${lastName()}!"
}
String email
String address
}

View File

@ -3,5 +3,4 @@ package com.baeldung
trait WheelTrait {
int noOfWheels
}

View File

@ -15,100 +15,103 @@ class TraitsUnitTest extends Specification {
def 'Should return msg string when using Employee.sayHello method provided by UserTrait'() {
when:
def msg = employee.sayHello()
then:
msg
msg instanceof String
assert msg == "Hello!"
msg == "Hello!"
}
def 'Should return displayMsg string when using Employee.showName method'() {
when:
def displayMsg = employee.showName()
then:
displayMsg
displayMsg instanceof String
assert displayMsg == "Hello, Bob!"
displayMsg == "Hello, Bob!"
}
def 'Should return greetMsg string when using Employee.greet method'() {
when:
def greetMsg = employee.greet()
then:
greetMsg
greetMsg instanceof String
assert greetMsg == "Hello, from a private method!"
greetMsg == "Hello, from a private method!"
}
def 'Should return MissingMethodException when using Employee.greetingMessage method'() {
when:
def exception
try {
employee.greetingMessage()
}catch(Exception e) {
exception = e
}
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'() {
when:
def emp = employee.self()
then:
emp
emp instanceof Employee
assert emp.is(employee)
emp.is(employee)
}
def 'Should display lastName when using Employee.showLastName method'() {
when:
def lastNameMsg = employee.showLastName()
then:
lastNameMsg
lastNameMsg instanceof String
assert lastNameMsg == "Hello, Marley!"
lastNameMsg == "Hello, Marley!"
}
def 'Should be able to define properties of UserTrait in Employee instance'() {
when:
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.email == "a@e.com"
employee.address == "baeldung.com"
}
def 'Should execute basicAbility method from SpeakingTrait and return msg string'() {
when:
def speakMsg = dog.basicAbility()
then:
speakMsg
speakMsg instanceof String
assert speakMsg == "Speaking!!"
speakMsg == "Speaking!!"
}
def 'Should verify multiple inheritance with traits and execute overridden traits method'() {
when:
def walkSpeakMsg = dog.speakAndWalk()
println walkSpeakMsg
then:
walkSpeakMsg
walkSpeakMsg instanceof String
assert walkSpeakMsg == "Walk and speak!!"
walkSpeakMsg == "Walk and speak!!"
}
def 'Should implement AnimalTrait at runtime and access basicBehavior method'() {
when:
def dogInstance = new Dog() as AnimalTrait
def basicBehaviorMsg = dogInstance.basicBehavior()
then:
basicBehaviorMsg
basicBehaviorMsg instanceof String
assert basicBehaviorMsg == "Animalistic!!"
basicBehaviorMsg == "Animalistic!!"
}
}