Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6533759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:10:02+00:00 2026-05-25T10:10:02+00:00

I’m trying to test-drive some Scala code using Specs2 and Mockito. I’m relatively new

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:10:03+00:00Added an answer on May 25, 2026 at 10:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.