title: Scala testable code with inheritance and mixins
body: <p>I've developed a lot of code in Java and dabbled in Groovy and Haskell which has now led me to Scala.</p>
  
  <p>I feel relatively comfortable with the functional side of Scala, but I'm finding myself a bit shaky on object oriented design in Scala, because it feels a bit different to Java, in particular due to traits/mix-ins.</p>
  
  <p>I aim to write code that is as testable as possible, which in my Java development has always translated into focus on</p>
  
  <ul>
  <li>Immutability where possible</li>
  <li>Prefer injection of state by constructors</li>
  <li>ALWAYS go for composition instead of inheritance (heavily influenced by, and likely an over-reaction to <a href="https://stackoverflow.com/a/2947823/2020801">this post on SO</a>) </li>
  </ul>
  
  <p>Now I'm trying to land on my feet in this new Scala territory, and I'm having a hard time figuring out what approach I should go for here, <em>in particular whether I should start using inheritance for some purposes</em>.</p>
  
  <p><em>Programming Scala</em> (Wampler and Payne; O'Reilly, 2nd Edition) has a section with considerations ("Good Object-Oriented Design: A Digression"), and I've read a number of posts on SO, but I haven't seen explicit mentions of the design consideration of testability. The book offers this advice on using inheritance:  </p>
  
  <blockquote>
    <ol>
    <li>An abstract base class or trait is subclassed one level by concrete classes, including case classes.</li>
    <li>Concrete classes are never subclassed, except for two cases:
    
    <ul>
    <li>Classes that mix in other behaviors defined in traits (...)</li>
    <li>Test-only versions to promote automated unit teting.</li>
    </ul></li>
    <li>When subclassing seems like the right approach, consider partitioning behaviors into traits and mix in those traits instead.</li>
    <li>Never split logical state across parent-child type boundaries.</li>
    </ol>
  </blockquote>
  
  <p>Some digging on SO also suggests that <a href="https://stackoverflow.com/a/3427095/2020801">sometimes mix-ins are preferable to composition</a>.</p>
  
  <p>So in essence I have two questions: </p>
  
  <ol>
  <li><p>Are there common cases where it would be better, even considering testability, to use inheritance?</p></li>
  <li><p>Do mix-ins offer good ways to enhance the testability of my code?</p></li>
  </ol>