spring mock

spring mock

引入jar

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>

使用方式1:

@InjectMocks
@Autowired
private OrderProduceWorker OrderProduceWorker;

//被mock类必须是 OrderProduceWorker 属性,否则不起作用
@Mock
private OrderProduce OrderProduce;

@Before
public void setup() {
        MockitoAnnotations.initMocks(this);
}

@Test
public void testDoProcess() throws Exception {
        Mockito.when(orderProduce.orderProduce(Mockito.anyLong())).thenReturn(SUCCESS);
        .....................................................
        .....................................................
}        
        

另类玩法:

@Autowired
private OrderProduceWorker OrderProduceWorker;

//被mock类必须 **不是**  OrderProduceWorker 属性,要想启作用 见 @Before
@Mock
private OrderProduce OrderProduce;

@Before
 public void setup() {
  	//init
      MockitoAnnotations.initMocks(this);
      //手工注入替换
      OrderProduceWorker worker = AopTestUtils.getTargetObject(this.orderProduceWorker);
      // You could have the same result by orderProduceWorker add @InjectMocks annotation
      ReflectionTestUtils.setField(worker, "orderProduce", this.orderProduce);
  }

@Test
public void testDoProcess() throws Exception {
        Mockito.when(orderProduce.orderProduce(Mockito.anyLong())).thenReturn(SUCCESS);
        .....................................................
        .....................................................
}        

Mockito.when的一个问题是你传递给它的参数是你试图存根的表达式.因此,当您使用Mockito.when两次进行相同的方法调用时,第二次使用它时,您实际上将获得第一次存根的行为.
我实际上建议不要使用Mockito.when.使用它时可能会遇到许多陷阱 – 很多情况下,当您需要其他语法时. “更安全”的替代语法是Mockito方法的“do”系列.

doReturn(value).when(mock).method(arguments ...);
doThrow(exception).when(mock).method(arguments ...);
doAnswer(answer).when(mock).method(arguments ...);

所以在你的情况下,你想要

doThrow(new BadSqlGrammarException(??, ??, ??)).when(accountMapper).createBadGrammarException();

如果你开始使用Mockito,那么我建议你学会使用“do”系列.它们是模拟void方法的唯一方法,而Mockito文档特别提到了这一点.但是只要Mockito.when可以使用它们就可以使用它们.因此,如果您使用“do”系列,您的测试结果会更加一致,而学习曲线也会减少.

摘自:http://www.voidcn.com/article/p-sqbaaxbk-bth.html

spring boot mock 参见:https://www.jianshu.com/p/ecbd7b5a2021

Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi.SimpleNamingContextBuilder.class org.springframework.mock.web.DelegatingServletInputStream.class org.springframework.mock.web.DelegatingServletOutputStream.class org.springframework.mock.web.HeaderValueHolder.class org.springframework.mock.web.MockExpressionEvaluator.class org.springframework.mock.web.MockFilterChain.class org.springframework.mock.web.MockFilterConfig.class org.springframework.mock.web.MockHttpServletRequest.class org.springframework.mock.web.MockHttpServletResponse.class org.springframework.mock.web.MockHttpSession.class org.springframework.mock.web.MockMultipartFile.class org.springframework.mock.web.MockMultipartHttpServletRequest.class org.springframework.mock.web.MockPageContext.class org.springframework.mock.web.MockRequestDispatcher.class org.springframework.mock.web.MockServletConfig.class org.springframework.mock.web.MockServletContext.class org.springframework.mock.web.PassThroughFilterChain.class org.springframework.mock.web.portlet.MockActionRequest.class org.springframework.mock.web.portlet.MockActionResponse.class org.springframework.mock.web.portlet.MockMultipartActionRequest.class org.springframework.mock.web.portlet.MockPortalContext.class org.springframework.mock.web.portlet.MockPortletConfig.class org.springframework.mock.web.portlet.MockPortletContext.class org.springframework.mock.web.portlet.MockPortletPreferences.class org.springframework.mock.web.portlet.MockPortletRequest.class org.springframework.mock.web.portlet.MockPortletRequestDispatcher.class org.springframework.mock.web.portlet.MockPortletResponse.class org.springframework.mock.web.portlet.MockPortletSession.class org.springframework.mock.web.portlet.MockPortletURL.class org.springframework.mock.web.portlet.MockRenderRequest.class org.springframework.mock.web.portlet.MockRenderResponse.class org.springframework.test.AbstractDependencyInjectionSpringContextTests.class org.springframework.test.AbstractSingleSpringContextTests.class org.springframework.test.AbstractSpringContextTests.class org.springframework.test.AbstractTransactionalDataSourceSpringContextTests.class org.springframework.test.AbstractTransactionalSpringContextTests.class org.springframework.test.AssertThrows.class org.springframework.test.ConditionalTestCase.class org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.class org.springframework.test.annotation.DirtiesContext.class org.springframework.test.annotation.ExpectedException.class org.springframework.test.annotation.IfProfileValue.class org.springframework.test.annotation.NotTransactional.class org.springframework.test.annotation.ProfileValueSource.class org.springframework.test.annotation.Repeat.class org.springframework.test.annotation.SystemProfileValueSource.class org.springframework.test.annotation.Timed.class org.springframework.test.jpa.AbstractAspectjJpaTests.class org.springframework.test.jpa.AbstractJpaTests.class org.springframework.test.jpa.OrmXmlOverridingShadowingClassLoader.class org.springframework.test.web.AbstractModelAndViewTests.class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值