Skip to content

Instantly share code, notes, and snippets.

@khajavi
Created April 16, 2016 08:21
Show Gist options
  • Save khajavi/9bb9bcf08884f5de3506809945894f22 to your computer and use it in GitHub Desktop.
Save khajavi/9bb9bcf08884f5de3506809945894f22 to your computer and use it in GitHub Desktop.
Mock dependency in CDI context with mockito
import javax.inject.Inject
import org.mockito.{MockitoAnnotations, InjectMocks, Mock, Mockito}
import org.scalatest.FunSuite
class MockWithInjectionTest extends FunSuite {
@Mock var dataRepoMock: DataRepo = _
@InjectMocks var calc: Calculate = _
test("Mockito Injection") {
calc = new Calculate
MockitoAnnotations.initMocks(this)
Mockito.when(dataRepoMock.getData).thenReturn(6)
val data = calc.doubleIt
assert(data == 12)
}
}
class Calculate {
@Inject private var foo: DataRepo = _
def doubleIt = foo.getData * 2
}
class DataRepo {
def getData = 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment