title: Scala and Mockito with traits
body: <p>I had a simple class that naturally divided into two parts, so I refactored as</p>
  
  <pre><code>class Refactored extends PartOne with PartTwo
  </code></pre>
  
  <p>Then the unit tests started failing.</p>
  
  <p>Below is an attempt to recreate the problem. The functionality of all three examples is the same, but the third test fails with a NullPointerException as indicated.  What it is about the use of traits that is causing the problem with mockito?</p>
  
  <p><strong>Edit:</strong> Is Mockito the best choice for Scala?  Am I using the wrong tools?</p>
  
  <pre><code>import org.scalatest.junit.JUnitSuite
  import org.scalatest.mock.MockitoSugar
  import org.mockito.Mockito.when
  import org.junit.Test
  import org.junit.Before
  
  class A(val b:B)
  class B(val c:Int)
  
  class First(){
    def getSomething(a:A) = a.b.c
  }
  
  class Second_A extends Second_B
  class Second_B{
    def getSomething(a:A) = a.b.c
  }
  
  class Third_A extends Third_B
  trait Third_B{
    // Will get a NullPointerException here 
    // since a.b will be null
    def getSomething(a:A) = a.b.c
  }
  
  class Mocking extends JUnitSuite with MockitoSugar{
      var mockA:A = _
      @Before def setup { mockA = mock[A] }
  
      @Test def first_PASSES {
        val mockFirst = mock[First]
        when(mockFirst.getSomething(mockA)).thenReturn(3)
  
        assert(3 === mockFirst.getSomething(mockA))
      }
  
      @Test def second_PASSES {
        val mockSecond = mock[Second_A]
        when(mockSecond.getSomething(mockA)).thenReturn(3)
  
        assert(3 === mockSecond.getSomething(mockA))
      }
  
      @Test def third_FAILS {
        val mockThird = mock[Third_A]
  
        //NullPointerException inside here (see above in Third_B)
        when(mockThird.getSomething(mockA)).thenReturn(3) 
  
        assert(3 === mockThird.getSomething(mockA))
      }
  }
  </code></pre>