Skip to content

Commit a4ce55d

Browse files
committed
add simplified version of Marshallable trait
1 parent a97719b commit a4ce55d

File tree

2 files changed

+16
-0
lines changed
  • core-scala/src

2 files changed

+16
-0
lines changed

core-scala/src/main/scala/com/baeldung/scala/classcompositionwithtraits/CarTraits.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ object CarTraits {
2222
/**
2323
* Another trait to be mixed into the class
2424
*/
25+
trait SimpleMarshaller extends Car {
26+
def toJson: String = s"{${"\"model\""}:$model," +
27+
s"\n${"\"horsePower\""}:$horsePower}"
28+
}
29+
30+
/**
31+
* Another trait with self-type to be mixed into the class
32+
*/
2533
trait Marshaller {
2634
self: Car =>
2735
def toJson = s"{${"\"model\""}:$model," +

core-scala/src/test/scala/com/baeldung/scala/classcompositionwithtraits/TraitsTest.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ class TraitsTest extends WordSpec with Matchers {
2828
}
2929
}
3030

31+
"Classes that extends Car with SimpleMarshaller" should {
32+
"inherit abstract class fields and methods" +
33+
"and be marshallable" in {
34+
val bmw0 = new BMW0("F15", 309) with SimpleMarshaller
35+
bmw0.toJson shouldBe "{\"model\":F15,\n\"horsePower\":309}"
36+
}
37+
}
38+
3139
"Classes that extends Car with Marshaller" should {
3240
"inherit abstract class fields and methods" +
3341
"and be marshallable" in {

0 commit comments

Comments
 (0)