Created
April 16, 2016 08:21
-
-
Save khajavi/9bb9bcf08884f5de3506809945894f22 to your computer and use it in GitHub Desktop.
Mock dependency in CDI context with mockito
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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