Search code examples
javascalamockitoprivatescalatest

Using ScalaTest to test java that contain @Autowired and private fields


I have begun to use ScalaTest to test my Java code and I like it (I've read the "Testing in Scala" book).

Recently I've been trying to test some java Spring code that contain fields which are marked private and @Autowired. There are no setter methods for these fields.

I realise that I could easily test them using JUnit + Mockito using the annotations @InjectMocks and @Mock, however, I would like to test them using ScalaTest and a mocking framework (such as Mockito or ScalaMock).

My question: Can Scalatest + Mockito be used to mock out private fields or do these frameworks require that my java classes have a public setter method defined for my private + @Autowired fields?


Solution

  • You can do it with InjectMocks. Here's a working example using ScalaTest and Mockito to test a class containing @Autowired members:

    import org.mockito.{MockitoAnnotations, InjectMocks, Mock}
    import org.scalatest.{FunSpec, BeforeAndAfter}
    import org.scalatest.Matchers._
    import org.mockito.Mockito._
    import org.springframework.beans.factory.annotation.Autowired
    
    class InjectMocksSpec extends FunSpec with BeforeAndAfter {
    
      @Mock
      var paramMock: MockedClass = null
    
      @InjectMocks
      var testClass = new TestClass()
    
      describe("InjectMocks") {
        it("should inject mock for Autowired field") {
          MockitoAnnotations.initMocks(this)
    
          when(paramMock.get).thenReturn("bar")
    
          testClass.test should be("bar")
    
          verify(paramMock, times(1)).get
        }
      }
    
    }
    
    class MockedClass {
      def get: String = {
        "foo"
      }
    }
    
    class TestClass {
      @Autowired
      var param: MockedClass = null
    
      def test: String = {
        param.get
      }
    }