I’m trying to mock the RabbitMQ ConnectionFactory object to return a mocked connection, using scalatest and mockito. Below is an example test that I am using:
class RabbitMQMockTest extends FunSuite with MockitoSugar {
test("RabbitMQ ConnectionFactory is correctly mocked") {
def connectionFactory = mock[ConnectionFactory]
def connection = mock[Connection]
when(connectionFactory.newConnection()).thenReturn(connection)
println(connectionFactory.newConnection())
assert(connectionFactory.newConnection() != null)
}
}
This always fails and the println statement always prints “null”. I am very new to using these technologies together and was wondering if anyone had any advice or could let me know if I’m doing anything wrong. Thanks in advance!
Don’t accidentally define variables using ‘def’! I defined the mocks using ‘def’ instead of ‘val’ or ‘var’, so I created a method that returns a new mock every time, instead of a variable.