随笔:Spring Test -- Mockito -- 浅谈个人理解@Mock, @MockBean, @InjectMocks

@Mock is used to create a mock object of a class or interface. Mock objects are used in testing to simulate the behavior of a real object or component that the test depends on. When a method of the mock object is called during the test, it will return a pre-configured response, allowing the test to simulate the behavior of the real object without having to actually interact with it.

@MockBean, on the other hand, is used to create a mock object of a Spring bean. Spring beans are Java objects managed by the Spring container and used throughout an application. The @MockBean annotation can be used to replace a real bean with a mock object in the Spring application context during a test. This allows the test to control the behavior of the bean and simulate different scenarios, without affecting the behavior of the real bean.

Both @Mock and @MockBean are useful for testing Spring applications and can help isolate the code being tested by removing dependencies on other components. This makes tests more reliable and easier to maintain, as changes to other components in the application will not affect the behavior of the test.

One key difference between @Mock and @MockBean is that @MockBean creates a mock object and adds it to the Spring application context, while @Mock creates a standalone mock object. This means that @MockBean can be used to replace a real bean with a mock object in the Spring context, while @Mock is typically used to create standalone mock objects for non-Spring classes.

@InjectMocks is used to inject those mock objects into the object being tested. It is typically used to inject mock objects into a class under test, in order to replace any real dependencies with mock objects. @InjectMocks can be used in combination with @Mock and @MockBean to create a test object with all the necessary dependencies injected.

Example:

@Service
public class MyService {
    @Autowired
    private MyRepository repository;

    public void doSomething() {
        // Do something using the repository
        repository.save(new MyEntity());
    }
}

@Repository
public class MyRepository {
    public void save(MyEntity entity) {
        // Save the entity to the database
    }
}

In this case, we have a MyService class that depends on a MyRepository Spring bean.

Then let's write a test using @Mock and @InjectMocks:

public class MyServiceTest {
    @Mock
    private MyRepository repository;

    @InjectMocks
    private MyService service;

    @Test
    public void testDoSomething() {
        // Configure the mock object
        doNothing().when(repository).save(any(MyEntity.class));

        // Call the method being tested
        service.doSomething();

        // Verify that the repository.save method was called
        verify(repository, times(1)).save(any(MyEntity.class));
    }
}

we create a mock object of MyRepository using the @Mock annotation.

And we also use @InjectMocks annotation, it will do two important thing: 

1. creates an instance of the class MyService

2. injects the mocks that are created with the @Mock (or @Spy) annotations into this instance.

We can also write a test using just @MockBean:

@SpringBootTest
public class MyServiceTest {
    @MockBean
    private MyRepository repository;

    @Autowired
    private MyService service;

    @Test
    public void testDoSomething() {
        // Configure the mock object
        doNothing().when(repository).save(any(MyEntity.class));

        // Call the method being tested
        service.doSomething();

        // Verify that the repository.save method was called
        verify(repository, times(1)).save(any(MyEntity.class));
    }
}

In this case, we use @MockBean to create a mock MyRepository bean, which is added to the Spring application context.

When using @MockBean, Spring replaces the actual bean in the application context with the mock bean during the test. The MyService bean is then injected with this mock bean instead of the real MyRepository bean.

The above example are the two ways to write the same test.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值