英文:
Java - Mockito - how to inject depency into abstract in test
问题
Sure, here's the translated code for your request:
public abstract class SomeService {
private final SomeClient someClient;
@Inject
public SomeService(SomeClient someClient) {
this.someClient = someClient;
}
public User myMethod(int uid) {
User user = someClient.getUserByUid(uid);
// ...
return user;
}
}
public class SomeServiceTest {
@Mock
private SomeService someService;
@Mock
private SomeClient someClient;
@Before
public void Setup() throws Exception {
MockitoAnnotations.initMocks(this);
// Inject the mocked someClient into the mocked someService
when(someService.getSomeClient()).thenReturn(someClient);
when(someClient.getUserByUid(anyInt())).thenReturn(mockedUserObj);
}
@Test
public void myMethodTest() {
User result = someService.myMethod(1);
// Here you can perform assertions on the result
}
}
The important part is to mock the behavior of the someClient
when it's called within the myMethod
of someService
in your test case. You do this by using when(someClient.getUserByUid(anyInt())).thenReturn(mockedUserObj)
. To inject the mocked someClient
into the mocked someService
, you use when(someService.getSomeClient()).thenReturn(someClient)
, assuming you have a getter method for the someClient
instance in your SomeService
class.
英文:
I have an abstract class where in the constructor there is an inject service, how could I mockup both abostract class and inject service in unit test, here is the code example
public abstract class SomeService {
private final SomeClient someClient;
@Inject
public SomeService(SomeClient someClient) {
this.someClient = someClient;
}
public User myMethod(int uid) {
User user = someClient.getUserByUid(uid);
...
return user;
}
Here is what I want to mockup and test my method
public class SomeServiceTest {
@Mock
private SomeService someService;
@Mock
private SomeClient someClient;
@Before
public void Setup() throws Exception {
MockitoAnnotations.initMocks(this);
// here how could I inject my someClient into my someService
when(someClient.getUserByUid(anyInt).Return(mockedUserObj);
}
@Test
public void myMethodTest(int uid) {
User result = someService.myMethod(1);
//here I want to mock up a response from my someClient that would be equal to mockedUserObj, but the someClient here doesn't seem to be the instance of someService
}
}
How could I inject my someClient
into my someService
of both mocked up class? so that I can mock up the inner method call response using the same mocked up client instance here?
答案1
得分: 1
由于抽象类无法实例化,为了测试目的,创建一个具体的子类是有道理的。
public class MockSomeService extends SomeService {
}
public class SomeServiceTest {
@Mock
private SomeClient someClient;
private MockSomeService mockSomeService;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockSomeService = new MockSomeService(someClient);
when(someClient.getUserByUid(Matchers.anyInt()).thenReturn(mockedUserObj);
}
@Test
public void myMethodTest(int uid) {
// Do stuff
}
}
英文:
Seeing as abstract classes cannot be instantiated it would make sense to create a concrete subclass just for testing purposes.
public class MockSomeService extends SomeService {
}
public class SomeServiceTest {
@Mock
private SomeClient someClient;
private MockSomeService mockSomeService;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockSomeService = new MockSomeService(someClient);
when(someClient.getUserByUid(Matchers.anyInt()).thenReturn(mockedUserObj);
}
@Test
public void myMethodTest(int uid) {
// Do stuff
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论