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 package com.baeldung.traits
trait AnimalTrait { trait AnimalTrait {
String basicBehavior() { String basicBehavior() {
return "Animalistic!!" return "Animalistic!!"
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -1,36 +1,35 @@
package com.baeldung.traits package com.baeldung.traits
trait UserTrait implements Human { trait UserTrait implements Human {
String email
String address
abstract String name()
String sayHello() { String sayHello() {
return "Hello!" return "Hello!"
} }
abstract String name()
String showName() { String showName() {
return "Hello, ${name()}!" return "Hello, ${name()}!"
} }
private String greetingMessage() { private String greetingMessage() {
return 'Hello, from a private method!' return 'Hello, from a private method!'
} }
String greet() { String greet() {
def msg = greetingMessage() def msg = greetingMessage()
println msg println msg
msg msg
} }
def self() { def self() {
return this return this
} }
String showLastName() { String showLastName() {
return "Hello, ${lastName()}!" return "Hello, ${lastName()}!"
} }
String email
String address
} }

View File

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

View File

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

View File

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

View File

@ -7,108 +7,111 @@ class TraitsUnitTest extends Specification {
Employee employee Employee employee
Dog dog Dog dog
void setup () { void setup() {
employee = new Employee() employee = new Employee()
dog = new Dog() 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: when:
def msg = employee.sayHello() def msg = employee.sayHello()
then: then:
msg msg
msg instanceof String msg instanceof String
assert msg == "Hello!" msg == "Hello!"
} }
def 'Should return displayMsg string when using Employee.showName method' () { def 'Should return displayMsg string when using Employee.showName method'() {
when: when:
def displayMsg = employee.showName() def displayMsg = employee.showName()
then: then:
displayMsg displayMsg
displayMsg instanceof String displayMsg instanceof String
assert displayMsg == "Hello, Bob!" displayMsg == "Hello, Bob!"
} }
def 'Should return greetMsg string when using Employee.greet method' () { def 'Should return greetMsg string when using Employee.greet method'() {
when: when:
def greetMsg = employee.greet() def greetMsg = employee.greet()
then: then:
greetMsg greetMsg
greetMsg instanceof String 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' () { def 'Should return MissingMethodException when using Employee.greetingMessage method'() {
when: when:
def exception employee.greetingMessage()
try {
employee.greetingMessage()
}catch(Exception e) {
exception = e
}
then: then:
exception thrown(MissingMethodException)
exception instanceof groovy.lang.MissingMethodException specificationContext.thrownException.message ==
assert exception.message == "No signature of method: com.baeldung.traits.Employee.greetingMessage()"+ "No signature of method: com.baeldung.traits.Employee.greetingMessage() is applicable for argument types: () values: []"
" 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: when:
def emp = employee.self() def emp = employee.self()
then: then:
emp emp
emp instanceof Employee emp instanceof Employee
assert emp.is(employee) emp.is(employee)
} }
def 'Should display lastName when using Employee.showLastName method' () { def 'Should display lastName when using Employee.showLastName method'() {
when: when:
def lastNameMsg = employee.showLastName() def lastNameMsg = employee.showLastName()
then: then:
lastNameMsg lastNameMsg
lastNameMsg instanceof String lastNameMsg instanceof String
assert lastNameMsg == "Hello, Marley!" 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: when:
employee = new Employee(email: "a@e.com", address: "baeldung.com") employee = new Employee(email: "a@e.com", address: "baeldung.com")
then: then:
employee employee
employee instanceof Employee employee instanceof Employee
assert employee.email == "a@e.com" employee.email == "a@e.com"
assert employee.address == "baeldung.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: when:
def speakMsg = dog.basicAbility() def speakMsg = dog.basicAbility()
then: then:
speakMsg speakMsg
speakMsg instanceof String speakMsg instanceof String
assert speakMsg == "Speaking!!" 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: when:
def walkSpeakMsg = dog.speakAndWalk() def walkSpeakMsg = dog.speakAndWalk()
println walkSpeakMsg println walkSpeakMsg
then: then:
walkSpeakMsg walkSpeakMsg
walkSpeakMsg instanceof String walkSpeakMsg instanceof String
assert walkSpeakMsg == "Walk and speak!!" 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: when:
def dogInstance = new Dog() as AnimalTrait def dogInstance = new Dog() as AnimalTrait
def basicBehaviorMsg = dogInstance.basicBehavior() def basicBehaviorMsg = dogInstance.basicBehavior()
then: then:
basicBehaviorMsg basicBehaviorMsg
basicBehaviorMsg instanceof String basicBehaviorMsg instanceof String
assert basicBehaviorMsg == "Animalistic!!" basicBehaviorMsg == "Animalistic!!"
} }
} }