File tree Expand file tree Collapse file tree 2 files changed +18
-5
lines changed
main/scala/com/baeldung/scala/classcompositionwithtraits
test/scala/com/baeldung/scala/classcompositionwithtraits Expand file tree Collapse file tree 2 files changed +18
-5
lines changed Original file line number Diff line number Diff line change 1
1
package com .baeldung .scala .classcompositionwithtraits
2
2
3
- import com .baeldung .scala .classcompositionwithtraits .CarTraits .{Car , Printable }
4
-
5
3
object CarTraits {
6
4
7
5
/**
@@ -22,6 +20,14 @@ object CarTraits {
22
20
/**
23
21
* Another trait to be mixed into the class
24
22
*/
23
+ trait SimpleMarshaller extends Car {
24
+ def toJson : String = s " { ${" \" model\" " }: $model, " +
25
+ s " \n ${" \" horsePower\" " }: $horsePower} "
26
+ }
27
+
28
+ /**
29
+ * Another trait with self-type to be mixed into the class
30
+ */
25
31
trait Marshaller {
26
32
self : Car =>
27
33
def toJson = s " { ${" \" model\" " }: $model, " +
Original file line number Diff line number Diff line change 1
1
package com .baeldung .scala .classcompositionwithtraits
2
2
3
+ import com .baeldung .scala .classcompositionwithtraits .CarTraits ._
3
4
import org .scalatest .{Matchers , WordSpec }
4
5
5
6
/**
6
7
* @author Sergey Ionin
7
8
*/
8
9
9
- class TraitsTest extends WordSpec with Matchers {
10
-
11
- import com .baeldung .scala .classcompositionwithtraits .CarTraits ._
10
+ class CarTraitsTest extends WordSpec with Matchers {
12
11
13
12
" Class that extends Car" should {
14
13
" inherit abstract class fields" in {
@@ -28,6 +27,14 @@ class TraitsTest extends WordSpec with Matchers {
28
27
}
29
28
}
30
29
30
+ " Classes that extends Car with SimpleMarshaller" should {
31
+ " inherit abstract class fields and methods" +
32
+ " and be marshallable" in {
33
+ val bmw0 = new BMW0 (" F15" , 309 ) with SimpleMarshaller
34
+ bmw0.toJson shouldBe " {\" model\" :F15,\n\" horsePower\" :309}"
35
+ }
36
+ }
37
+
31
38
" Classes that extends Car with Marshaller" should {
32
39
" inherit abstract class fields and methods" +
33
40
" and be marshallable" in {
You can’t perform that action at this time.
0 commit comments