如何用mockito来写单元测试

一、mockito单元测试中常见的几个注解
1. @RunWith(MockitoJUnitRunner.class) 该注解的意思以什么容器来运行单元测试,容器的意思可以理解为运行环境可以解析你的mockito中的注解和语法的意思,这个注解一般加载在你写的单元测试的类上如下

     @RunWith(MockitoJUnitRunner.class)
      public class SendMobileValicodeControllerTest 

2.@InjectMocks 该注解的意思是创建一个实例对象,类似spring中通过 注解注入外部对象一样

   @RunWith(MockitoJUnitRunner.class)
   public class SendMobileValicodeControllerTest {
   @InjectMocks
   SendMobileValicodeController sendMobileValicodeController;

3.@Mock 这个注解的意思就是模拟,模拟一个对象,用他来构造参数,该注解注释的对象将会被注入到@InjectMocks注解注入的实例中,

    @RunWith(MockitoJUnitRunner.class)
    public class SendMobileValicodeControllerTest {
   @InjectMocks
   SendMobileValicodeController sendMobileValicodeController;
	@Mock
   private SendMobileMsgService sendMobileMsgService;
	@Mock
    private SwitchCache switchCache;

意思就是 sendMobileValicodeController和switchCache 将会以对象属性的身份注入到sendMobileValicodeController这个对象中,类似spring的注解注入,
4.@Test 注解就是测试的方法,该注解的方法一定要要是public方法,代码如下

   public void testSendMobileValicode() throws Exception {
	String data="{\r\n" + 
			" \"mobileNo\":\"18710841160\",\r\n" + 
			" \"openId\":\"1qaz2sxx3edc\",\r\n" + 
			" \"batchId\":\"plmijb789456\",\r\n" + 
			" \"userIp\":\"127.0.0.1\"\r\n" + 
			"}";	
	ResultObject resultObject=new ResultObject(SmsReturnCodeEnum.SUCCESS.getCode(), SmsReturnCodeEnum.SUCCESS.getErrMsg());
	String expectStr=JSONObject.toJSONString(resultObject);
	
	SMSReturnMsgHead smsReturnMsgHead=new SMSReturnMsgHead();
	smsReturnMsgHead.setRespCode("1");
	smsReturnMsgHead.setRespMsg("短信发送成功");
	
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setContent(data.getBytes());
	MockHttpServletResponse response = new MockHttpServletResponse();

	when(switchCache.getSMSTYPE()).thenReturn(SwitchCache.SMSTYPE_SQLSERVER);
	when(sendMobileMsgService.sendMobileValicodeNew("18710841160","1qaz2sxx3edc","plmijb789456","127.0.0.1")).thenReturn(Boolean.TRUE);
	String retStr=sendMobileValicodeController.sendMobileValicode(request, response);	       
	System.out.println("str>>"+retStr);
	Assert.assertTrue(retStr.equals(expectStr));
	
	when(switchCache.getSMSTYPE()).thenReturn(SwitchCache.SMSTYPE_INTERFACE);
	when(sendMobileMsgService.sendMobileValicode("18710841160","1qaz2sxx3edc","plmijb789456","127.0.0.1")).thenReturn(smsReturnMsgHead);
	retStr=sendMobileValicodeController.sendMobileValicode(request, response);	       
	System.out.println("str>>"+retStr);
	Assert.assertTrue(retStr.equals(expectStr));
	
	resultObject=new ResultObject(SmsReturnCodeEnum.FAILED.getCode(),null);
	expectStr=JSONObject.toJSONString(resultObject);
		
	when(sendMobileMsgService.sendMobileValicode("18710841160","1qaz2sxx3edc","plmijb789456","127.0.0.1")).thenReturn(null);
	retStr=sendMobileValicodeController.sendMobileValicode(request, response);	       
	System.out.println("str>>"+retStr);
	Assert.assertTrue(retStr.equals(expectStr));
}

5.引入mockito注解需要的jar包就可以了在可以这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mockito 是一个 Java 单元测试框架,它可以模拟对象并进行单元测试。切面编程是一种编程范式,它可以在程序运行时动态地将代码织入到现有的代码中。在单元测试中,我们可以使用 Mockito 和切面编程来模拟对象并测试代码的行为。 在使用 Mockito 进行切面单元测试时,我们可以使用 Mockito 的 `@Mock` 注解来创建模拟对象,并使用 `@InjectMocks` 注解将模拟对象注入到被测试对象中。然后,我们可以使用 Mockito 的 `when()` 方法来设置模拟对象的行为,并使用 `verify()` 方法来验证被测试对象的行为。 下面是一个使用 Mockito 进行切面单元测试的示例代码: ```java import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class MyAspectTest { @Mock private MyService myService; @InjectMocks private MyAspect myAspect; @Test public void testMyAspect() { when(myService.doSomething()).thenReturn("mocked result"); myAspect.doSomethingWithAspect(); verify(myService).doSomething(); } } ``` 在这个示例中,我们创建了一个 `MyService` 的模拟对象,并将其注入到 `MyAspect` 中。然后,我们使用 `when()` 方法设置模拟对象的行为,并调用 `MyAspect` 的 `doSomethingWithAspect()` 方法。最后,我们使用 `verify()` 方法验证 `MyService` 的 `doSomething()` 方法是否被调用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值