1
+ package com .baeldung .scala
2
+
3
+
4
+ import org .junit .Assert .assertEquals
5
+ import org .junit .Test
6
+
7
+ case class CovidCountryStats (countryCode : String , deaths : Int , confirmedCases : Int )
8
+
9
+ class CaseClassesUnitTest {
10
+ @ Test
11
+ def givenCaseClass_whenPatternMatched_thenReturnsProperValue () = {
12
+ val covidPL = CovidCountryStats (" PL" , 776 , 15366 )
13
+
14
+ val text = covidPL matches {
15
+ case CovidCountryStats (" PL" , x, y) => " Death rate for Poland is " + x.toFloat / y.toFloat
16
+ case _ => " Unknown country"
17
+ }
18
+
19
+ assertEquals(" Death rate for Poland is 0.05050111" , text)
20
+ }
21
+
22
+ @ Test
23
+ def givenTwoEqualsCaseClasses_whenCheckingEquality_thenReturnsTrue (): Unit = {
24
+ assert(CovidCountryStats (" PL" , 776 , 15366 ) == CovidCountryStats (" PL" , 776 , 15366 ))
25
+ }
26
+
27
+ @ Test
28
+ def givenCaseClass_whenCallingCopy_thenParametersAreCopied (): Unit = {
29
+ val covidPL = CovidCountryStats (" PL" , 776 , 15366 )
30
+ val covidUA = covidPL.copy(countryCode = " UA" )
31
+
32
+ assertEquals(" UA" , covidUA.countryCodeo)
33
+ assertEquals(766 , covidUA.deaths)
34
+ assertEquals(15366 , covidUA.confirmedCases)
35
+ }
36
+
37
+ @ Test
38
+ def givenTuple_whenCallingApply_thenCreatesNewInstance () = {
39
+ val tuple = (" PL" , 776 , 15366 )
40
+ val covidPL = (CovidCountryStats .apply _).tupled(tuple)
41
+
42
+ assertEquals(CovidCountryStats (" PL" , 776 , 15366 ), covidPL)
43
+ }
44
+ }
0 commit comments