junit @Mock
时间: 2023-10-02 21:07:10 浏览: 103
junit单元测试及Mock应用,超详细的PPT实战应用
@Mock是JUnit中的注解,它用于创建一个模拟对象(mock object)。@Mock注解的作用是生成一个接口或者类的mock对象,可以用于模拟测试中的依赖对象。但是要注意,使用@Mock注解生效的两种方式。一种是使用@RunWith(MockitoJUnitRunner.class)注解在测试类上,另一种是在@Before方法中使用MockitoAnnotations.openMocks(this)初始化对象。如果没有正确初始化@Mock注解的对象,可能会导致空指针异常的发生。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Solved - JUnit Mockito: 比较 @Inject @InjectMocks @MockBean @Mock](https://blog.csdn.net/creepcheck/article/details/105986289)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [@Mock、Mockito.mock()、@MockBean的区别](https://blog.csdn.net/Numb_ZL/article/details/122217202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文