@Mock 和 @InjectMocks 的多个级别

发布于 2024-11-15 01:04:54 字数 636 浏览 2 评论 0原文

所以我知道在 Mockito 中 @InjectMocks 会通过 @Mock 的注释注入任何它能注入的东西,但是如何处理这种情况呢?

    @Mock
    private MockObject1 mockObject1;
    
    @Mock
    private MockObject2 mockObject2;
    
    @InjectMocks
    private SystemUnderTest systemUnderTest = new SystemUnderTest();

想象一下,MockObject2 有一个 MockObject1 类型的属性,而 SystemUnderTest 有一个 MockObject2 类型的属性。我希望将 mockObject1 注入到 mockObject2 中,并将 mockObject2 注入到 systemUnderTest 中。

这可以通过注释实现吗?

So I understand that in Mockito @InjectMocks will inject anything that it can with the annotation of @Mock, but how to handle this scenario?

    @Mock
    private MockObject1 mockObject1;
    
    @Mock
    private MockObject2 mockObject2;
    
    @InjectMocks
    private SystemUnderTest systemUnderTest = new SystemUnderTest();

Imagine that MockObject2 has an attribute that is of type MockObject1, and SystemUnderTest has an attribute of type MockObject2. I would like to have mockObject1 injected into mockObject2, and mockObject2 injected into systemUnderTest.

Is this possible with annotations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲歌长辞 2024-11-22 01:04:54

由于我在这里没有得到任何回复,所以我在 Mockito 论坛上提问。以下是讨论的链接:https://groups.google.com/d /topic/mockito/hWwcI5UHFi0/discussion

总结一下答案,从技术上讲,这会破坏模拟的目的。您实际上应该只模拟 SystemUnderTest 类所需的对象。在本身就是模拟的对象中模拟事物是毫无意义的。

如果您真的想这样做,@Spy 可以提供帮助。

Since I didn't get any response here I asked on the Mockito forums. Here is a link to the discussion: https://groups.google.com/d/topic/mockito/hWwcI5UHFi0/discussion

To summarize the answers, technically this would kind of defeat the purpose of mocking. You should really only mock the objects needed by the SystemUnderTest class. Mocking things within objects that are themselves mocks is kind of pointless.

If you really wanted to do it, @Spy can help.

苍白女子 2024-11-22 01:04:54

通过将 @Spy 与 @InjectMocks 结合起来是可能的。对于您的示例,它将是:

@Spy
private MockObject1 mockObject1 = new MockObject1 ();

@Spy @InjectMocks //if MockObject2 has a MockObject1, then it will be injected here.
private MockObject2 mockObject2 = new MockObject2 ();

@InjectMocks
private SystemUnderTest systemUnderTest;

It is possible by combining @Spy with @InjectMocks. For your example, it would be:

@Spy
private MockObject1 mockObject1 = new MockObject1 ();

@Spy @InjectMocks //if MockObject2 has a MockObject1, then it will be injected here.
private MockObject2 mockObject2 = new MockObject2 ();

@InjectMocks
private SystemUnderTest systemUnderTest;
煞人兵器 2024-11-22 01:04:54

我发现的其他解决方案是使用 java sintax 而不是注释来注入 @Spy 对象。

@Spy
private MockObject1 mockObject1 = new MockObject1 ();

@InjectMocks //if MockObject2 has a MockObject1, then it will be injected here.
private MockObject2 mockObject2 = spy(MockObject2.class);

@InjectMocks
private SystemUnderTest systemUnderTest;

Other solution I found is using java sintax instead annotation to make the @Spy object injected.

@Spy
private MockObject1 mockObject1 = new MockObject1 ();

@InjectMocks //if MockObject2 has a MockObject1, then it will be injected here.
private MockObject2 mockObject2 = spy(MockObject2.class);

@InjectMocks
private SystemUnderTest systemUnderTest;
隱形的亼 2024-11-22 01:04:54

这对我有用:

private MockObject1 mockObject1 = mock(MockObject1.class);

@Spy
private RealObject2 realObject = new RealObject2(mockObject1);

@InjectMocks
private SystemUnderTest systemUnderTest = new SystemUnderTest();

This works for me:

private MockObject1 mockObject1 = mock(MockObject1.class);

@Spy
private RealObject2 realObject = new RealObject2(mockObject1);

@InjectMocks
private SystemUnderTest systemUnderTest = new SystemUnderTest();
浮世清欢 2024-11-22 01:04:54

这是我的解决方案:

@ExtendWith(SpringExtension.class)
class DocumentServiceTestMock {

//2nd Level Dependency
CustomerFacade customerFacade = Mockito.mock(CustomerFacade.class);

//Direct Dependency
@Spy
PostCreateHelper postCreateHelper = new PostCreateHelper(customerFacade);

//SUT
@InjectMocks
DocumentService documentService;

@BeforeEach
void setUp() {
        given(customerFacade.getString()).willReturn("customerFacade_MOCKED");
}

@Test
void test() {
     String documentAsResource = documentService.getDocumentAsResource(ExportType.WORD);
}
//----------
@Service
public class DocumentService {
   final PostCreateHelper postCreateHelper;

@Component
public class PostCreateHelper {
   final CustomerFacade customerFacade;

@Service
public class CustomerFacade {

Here is my solution:

@ExtendWith(SpringExtension.class)
class DocumentServiceTestMock {

//2nd Level Dependency
CustomerFacade customerFacade = Mockito.mock(CustomerFacade.class);

//Direct Dependency
@Spy
PostCreateHelper postCreateHelper = new PostCreateHelper(customerFacade);

//SUT
@InjectMocks
DocumentService documentService;

@BeforeEach
void setUp() {
        given(customerFacade.getString()).willReturn("customerFacade_MOCKED");
}

@Test
void test() {
     String documentAsResource = documentService.getDocumentAsResource(ExportType.WORD);
}
//----------
@Service
public class DocumentService {
   final PostCreateHelper postCreateHelper;

@Component
public class PostCreateHelper {
   final CustomerFacade customerFacade;

@Service
public class CustomerFacade {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文