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!
Connection
? It's generally nasty to mock multiple levels of dependencies this way -- you'll end up maintaining lots of fragile mock code which itself doesn't add value to your project. – earldouglas