spring-mock 例子

今天用了一下spring-mock来测试系统中的dao.感觉真的不错。这个很简单,记下来得原因是怕自己会忘。
你的测试用例必须从AbstractDependencyInjectionSpringContextTests继承。他会帮你创建beanfactory以及beans.但是你必须告诉他到那去找配置文件。这个工作就是通过getConfigLocations方法来完成。一般情况下,这个方法都很简单。
看看我的就知道他要干些什么了。

@Override
    
protected  String[] getConfigLocations() {
        
//  TODO Auto-generated method stub
         return   new  String[]{  " /springContext-hibernate.xml "  };
    }

好了,这样就配置完成了。下面的工作就是获取你要测试的对象,并对他测试了。

public  ShipMasterDao getShipMasterDao() {
        
if (shipMasterDao  ==   null  ){
            shipMasterDao 
=  (ShipMasterDao) this .applicationContext.getBean( " shipMasterDao " );
        }
        
return  shipMasterDao;
    }
    
    
public   void  testGetUser(){
        ShipMaster shipMaster 
=   this .getShipMasterDao().getShipMaster( 1 );
        
this .assertEquals(shipMaster.getImono(),  " imo01 " );
    }
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
Spring Boot进行mock单元测试的步骤如下: 1. 添加Mockito和JUnit的依赖: ```xml <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.6.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.6.2</version> <scope>test</scope> </dependency> ``` 2. 创建测试类并添加注解: ```java import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) public class MyServiceTest { @Mock private MyRepository myRepository; @Test public void testMyService() { //写测试代码 } } ``` 3. 创建Mock对象: ```java @Mock private MyRepository myRepository; ``` 4. 编写测试代码: ```java import static org.mockito.Mockito.when; public class MyServiceTest { @Mock private MyRepository myRepository; @Test public void testMyService() { when(myRepository.findByName("John")).thenReturn(new MyEntity("John", 30)); MyService myService = new MyService(myRepository); MyEntity result = myService.getByName("John"); assertEquals("John", result.getName()); assertEquals(30, result.getAge()); } } ``` 在这个例子,我们使用Mockito创建了一个MyRepository的Mock对象,并且在测试代码定义了当调用findByName("John")方法时返回一个MyEntity对象。然后我们使用这个Mock对象创建了一个MyService对象并调用getByName("John")方法,并验证返回值与预期值相等。 这就是使用Mockito进行Spring Boot mock单元测试的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值