File tree Expand file tree Collapse file tree 2 files changed +16
-0
lines changed
main/scala/com/baeldung/scala/classcompositionwithtraits
test/scala/com/baeldung/scala/classcompositionwithtraits Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,14 @@ object CarTraits {
22
22
/**
23
23
* Another trait to be mixed into the class
24
24
*/
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
+ */
25
33
trait Marshaller {
26
34
self : Car =>
27
35
def toJson = s " { ${" \" model\" " }: $model, " +
Original file line number Diff line number Diff line change @@ -28,6 +28,14 @@ class TraitsTest extends WordSpec with Matchers {
28
28
}
29
29
}
30
30
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
+
31
39
" Classes that extends Car with Marshaller" should {
32
40
" inherit abstract class fields and methods" +
33
41
" and be marshallable" in {
You can’t perform that action at this time.
0 commit comments