|
| 1 | +package com.baeldung.scala.exceptionhandling |
| 2 | + |
| 3 | +import org.scalatest._ |
| 4 | +import org.scalatest.Assertions._ |
| 5 | +import scala.util.{Success, Failure} |
| 6 | + |
| 7 | +class ExamplesUnitTest extends FlatSpec with Matchers { |
| 8 | + |
| 9 | + "tryCatch" should "handle NegativeNumberException" in { |
| 10 | + noException should be thrownBy Examples.tryCatch(-1, -2) |
| 11 | + assert(Examples.tryCatch(-20, -30) == -2) |
| 12 | + } |
| 13 | + |
| 14 | + it should "handle IntOverflowException" in { |
| 15 | + noException should be thrownBy Examples.tryCatch(Int.MaxValue, 1) |
| 16 | + assert(Examples.tryCatch(Int.MaxValue, 1) == -1) |
| 17 | + } |
| 18 | + |
| 19 | + it should "return the correct sum" in { |
| 20 | + noException should be thrownBy Examples.tryCatch(2, 1) |
| 21 | + assert(Examples.tryCatch(2, 3) == 5) |
| 22 | + } |
| 23 | + |
| 24 | + "trySuccessFailure" should "handle NegativeNumberException" in { |
| 25 | + import CalculatorExceptions._ |
| 26 | + val result = Examples.trySuccessFailure(-1, -2) |
| 27 | + result match { |
| 28 | + case Failure(e) => assert(e.isInstanceOf[NegativeNumberException]) |
| 29 | + case Success(_) => fail("Should fail!") |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + it should "handle IntOverflowException" in { |
| 34 | + import CalculatorExceptions._ |
| 35 | + val result = Examples.trySuccessFailure(Int.MaxValue, 1) |
| 36 | + result match { |
| 37 | + case Failure(e) => assert(e.isInstanceOf[IntOverflowException]) |
| 38 | + case Success(_) => fail("Should fail!") |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + it should "return the correct sum" in { |
| 43 | + import CalculatorExceptions._ |
| 44 | + val result = Examples.trySuccessFailure(3, 2) |
| 45 | + result match { |
| 46 | + case Failure(e) => fail("Should succed!") |
| 47 | + case Success(result) => assert(result == 5) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + "catchObjects" should "handle NegativeNumberException" in { |
| 52 | + import CalculatorExceptions._ |
| 53 | + val result = Examples.catchObjects(-1, -2) |
| 54 | + result match { |
| 55 | + case Failure(e) => assert(e.isInstanceOf[NegativeNumberException]) |
| 56 | + case Success(_) => fail("Should fail!") |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + it should "handle IntOverflowException" in { |
| 61 | + import CalculatorExceptions._ |
| 62 | + val result = Examples.catchObjects(Int.MaxValue, 1) |
| 63 | + result match { |
| 64 | + case Failure(e) => assert(e.isInstanceOf[IntOverflowException]) |
| 65 | + case Success(_) => fail("Should fail!") |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + it should "return the correct sum" in { |
| 70 | + import CalculatorExceptions._ |
| 71 | + val result = Examples.catchObjects(3, 2) |
| 72 | + result match { |
| 73 | + case Failure(e) => fail("Should succed!") |
| 74 | + case Success(result) => assert(result == 5) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + "customCatchObjects" should "handle NegativeNumberException" in { |
| 79 | + import CalculatorExceptions._ |
| 80 | + val result = Examples.customCatchObjects(-1, -2) |
| 81 | + result match { |
| 82 | + case Failure(e) => assert(e.isInstanceOf[NegativeNumberException]) |
| 83 | + case Success(_) => fail("Should fail!") |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + it should "handle IntOverflowException" in { |
| 88 | + import CalculatorExceptions._ |
| 89 | + assertThrows[IntOverflowException] { |
| 90 | + Examples.customCatchObjects(Int.MaxValue, 1) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + it should "return the correct sum" in { |
| 95 | + import CalculatorExceptions._ |
| 96 | + val result = Examples.customCatchObjects(3, 2) |
| 97 | + result match { |
| 98 | + case Failure(e) => fail("Should succed!") |
| 99 | + case Success(result) => assert(result == 5) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + "customCatchObjects composed with trySuccessFailure" should "return the correct sum" in { |
| 104 | + import CalculatorExceptions._ |
| 105 | + val result = Examples.customCatchObjects(3, 2) flatMap (Examples |
| 106 | + .trySuccessFailure(_, 3)) |
| 107 | + result match { |
| 108 | + case Failure(e) => fail("Should succed!") |
| 109 | + case Success(result) => assert(result == 8) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + it should "print an error" in { |
| 114 | + import CalculatorExceptions._ |
| 115 | + val result = Examples.customCatchObjects(-1, -2) flatMap (Examples |
| 116 | + .trySuccessFailure(_, 3)) |
| 117 | + result match { |
| 118 | + case Failure(e) => assert(e.isInstanceOf[NegativeNumberException]) |
| 119 | + case Success(result) => fail("Should fail!") |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments