Java – Mockito – 如何在测试中将依赖注入抽象类中

huangapple go评论142阅读模式
英文:

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
    }
}

huangapple
  • 本文由 发表于 2020年8月13日 02:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63383041.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定