scalamockitospecs

Scala, Specs2, Mockito and null return values


I'm trying to test-drive some Scala code using Specs2 and Mockito. I'm relatively new to all three, and having difficulty with the mocked methods returning null.

In the following (transcribed with some name changes)

  "My Component's process(File)" should  {

    "pass file to Parser" in new modules {
      val file = mock[File]
      myComponent.process(file)

      there was one(mockParser).parse(file)
    }

    "pass parse result to Translator" in new modules {
      val file = mock[File]
      val myType1 = mock[MyType1]

      mockParser.parse(file) returns (Some(myType1))
      myComponent.process(file)

      there was one(mockTranslator).translate(myType1)
    }

  }

The "pass file to Parser" works until I add the translator call in the SUT, and then dies because the mockParser.parse method has returned a null, which the translator code can't take.

Similarly, the "pass parse result to Translator" passes until I try to use the translation result in the SUT.

The real code for both of these methods can never return null, but I don't know how to tell Mockito to make the expectations return usable results.

I can of course work around this by putting null checks in the SUT, but I'd rather not, as I'm making sure to never return nulls and instead using Option, None and Some.

Pointers to a good Scala/Specs2/Mockito tutorial would be wonderful, as would a simple example of how to change a line like

there was one(mockParser).parse(file)

to make it return something that allows continued execution in the SUT when it doesn't deal with nulls.

Flailing about trying to figure this out, I have tried changing that line to

there was one(mockParser).parse(file) returns myResult

with a value for myResult that is of the type I want returned. That gave me a compile error as it expects to find a MatchResult there rather than my return type.

If it matters, I'm using Scala 2.9.0.


Solution

  • If you don't have seen it, you can look the mock expectation page of the specs2 documentation.

    In your code, the stub should be mockParser.parse(file) returns myResult

    Edited after Don's edit:

    There was a misunderstanding. The way you do it in your second example is the good one and you should do exactly the same in the first test:

    val file = mock[File]
    val myType1 = mock[MyType1]
    
    mockParser.parse(file) returns (Some(myType1))
    myComponent.process(file)
    there was one(mockParser).parse(file)
    

    The idea of unit testing with mock is always the same: explain how your mocks work (stubbing), execute, verify.

    That should answer the question, now a personal advice:

    Most of the time, except if you want to verify some algorithmic behavior (stop on first success, process a list in reverse order) you should not test expectation in your unit tests.

    In your example, the process method should "translate things", thus your unit tests should focus on it: mock your parsers and translators, stub them and only check the result of the whole process. It's less fine grain but the goal of a unit test is not to check every step of a method. If you want to change the implementation, you should not have to modify a bunch of unit tests that verify each line of the method.