Unit test for a method, which calls other methods

93 Views Asked by At

I have the class A with private methods a and b and public method c.

Methods a and b are covered by tests.

The public method c is used with a parameter, so it can call a or b with if stmt

I want test method c with behavior-based test (test a called for some parameter and b called for other). I can't figure out how achieve this with ScalaTest and ScalaMock

Part of the class code

  protected final def generate(leftBranch: List[L], leftPoint: L, rightBranch: List[L], rightPoint: L): C = {
    val lEmpty = leftBranch.isEmpty
    val rEmpty = rightBranch.isEmpty
    if (lEmpty && rEmpty) {
      val cc = leafOrdering.compare(leftPoint, rightPoint)
      if (cc < 0) {
        nextOrExpand(leftPoint){ p =>
          if (leafOrdering.compare(p, rightPoint) < 0) {
            usePoint(p)

          } else
            expand(leftPoint)

        }
      }
      else if (cc == 0)
        throw SamePointException()

      else
        throw LowerRestrictionException()

    }
    else if (lEmpty) {
      val cc = leafOrdering.compare(leftPoint, rightBranch.head)
      if (cc < 0)
        nextOrExpand(leftPoint){ p =>
          if (leafOrdering.compare(p, rightBranch.head) <= 0) {
            usePoint(p)

          } else {
            expand(leftPoint)
          }

        }

      else if (cc == 0)
        minimalRestricted(List(leftPoint), startBranchWith, rightBranch.tail, rightPoint)

      else
        throw LowerRestrictionException()

    }
    else if (rEmpty)
      if (leafOrdering.compare(leftBranch.head, rightPoint) < 0)
        next(leftBranch, leftPoint)

      else
        throw LowerRestrictionException()

    else {
      if (leafOrdering.compare(leftBranch.head, rightBranch.head) < 0)
        next(leftBranch, leftPoint)

      else {        
        throw LowerRestrictionException()

      }

    }
  }

Methods nextOrExpand, usePoint, expand, minimalRestricted, next are private and tested. The generate method is a public method that needs to be tested.

1

There are 1 best solutions below

2
Gastón Schabas On

If I'm not wrong, with behavior-based test you want to write your test like the example showed here

class SetSpec extends AnyWordSpec {

  // Describe a scope for a subject, in this case: "A Set"
  "A Set" can { // All tests within these curly braces are about "A Set"

    // Can describe nested scopes that "narrow" its outer scopes
    "empty" should { // All tests within these curly braces are about "A Set (when empty)"

      "have size 0" in {    // Here, 'it' refers to "A Set (when empty)". The full name
        assert(Set.empty.size == 0) // of this test is: "A Set (when empty) should have size 0"
      }
      "produce NoSuchElementException when head is invoked" in { // Define another test
        intercept[NoSuchElementException] {
          Set.empty.head
        }
      }
      "should be empty" ignore { // To ignore a test, change 'it' to 'ignore'...
        assert(Set.empty.isEmpty)
      }
    }

    // Describe a second nested scope that narrows "A Set" in a different way
    "non-empty" should { // All tests within these curly braces are about "A Set (when non-empty)"

      "have the correct size" in { // Here, 'it' refers to "A Set (when non-empty)". This test's full
        assert(Set(1, 2, 3).size == 3)     // name is: "A Set (when non-empty) should have the correct size"
      }
      // Define a pending test by using (pending) for the body
      "return a contained value when head is invoked" is (pending)
      import tagobjects.Slow
      "be non-empty" taggedAs (Slow) in { // Tag a test by placing a tag object after the test name
        assert(Set(1, 2, 3).nonEmpty)
      }
    }
  }
}

If that's what you want, you would be able to see that you can mix different traits to write your tests using different styles. Also based on which one you pick, the output of the test will be different.

scalatest also provide a rich DSL that will let you write your test close to a natural language. You can use matchers that offer different methods such as have, be, contains, etc