Java Config下的Spring Test几种方式实例详解
JavaConfig下的SpringTest方式
用了三种方式:
1.纯手动取bean:
packagecom.wang.test; importcom.marsmother.commission.core.config.MapperConfig; importcom.marsmother.commission.core.config.PropertyConfig; importcom.marsmother.commission.core.config.ServiceConfig; importcom.marsmother.commission.core.dto.GeneralResponseData; importcom.marsmother.commission.core.service.UserService; importcom.marsmother.commission.site.config.SecurityConfig; importorg.junit.Before; importorg.junit.Test; importorg.springframework.context.annotation.AnnotationConfigApplicationContext; /** *CreatedbyWangleion15/10/29. */ publicclassCustomeTest{ privatestaticAnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(); @Before publicvoidtearUp(){ context.register(PropertyConfig.class); context.register(ServiceConfig.class); context.register(SecurityConfig.class); context.register(MapperConfig.class); context.refresh(); } @Test publicvoidtestUser(){ UserServiceuserService=context.getBean(UserService.class); LonguserId=3L; GeneralResponseDatadata=userService.addUserRelation(userId); System.out.println(data.getMsg()); } }
2.采用spring-test
packagecom.wang.test; importcom.marsmother.commission.core.config.MapperConfig; importcom.marsmother.commission.core.config.PropertyConfig; importcom.marsmother.commission.core.config.ServiceConfig; importcom.marsmother.commission.core.dto.GeneralResponseData; importcom.marsmother.commission.core.service.UserService; importcom.marsmother.commission.site.config.SecurityConfig; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.test.context.ContextConfiguration; importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** *CreatedbyWangleion15/10/29. */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={PropertyConfig.class,ServiceConfig.class,SecurityConfig.class,MapperConfig.class}) publicclassSpringTest{ @Autowired privateUserServiceuserService; @Test publicvoidtestUser(){ GeneralResponseDatadata=userService.addUserRelation(3L); System.out.println(data.getMsg()); } }
3.采用Mockito
需要引入相应包:
org.mockito mockito-all 1.9.5 test
packagecom.wang.test; importcom.marsmother.commission.core.dto.GeneralResponseData; importcom.marsmother.commission.core.presistence.FollowNumberMapper; importcom.marsmother.commission.core.presistence.UserMapper; importcom.marsmother.commission.core.presistence.UserRelationMapper; importcom.marsmother.commission.core.service.UserService; importorg.junit.Before; importorg.junit.Test; importorg.mockito.InjectMocks; importorg.mockito.Mock; importorg.mockito.MockitoAnnotations; /** *CreatedbyWangleion15/10/29. */ publicclassTestUserService{ @InjectMocks privateUserServiceuserService; @Mock privateFollowNumberMapperfollowNumberMapper; @Mock privateUserMapperuserMapper; @Mock privateUserRelationMapperuserRelationMapper; @Before publicvoidinit(){ MockitoAnnotations.initMocks(this); } @Test publicvoidtestUser(){ LonguserId=3L; GeneralResponseDataresult=userService.addUserRelation(userId); System.out.println(result.getMsg()); } }
这里@Mock的话,并不会真正的去执行数据库的操作。
还有一种用法是@Spy,暂时不了解具体使用方式,待研究。
相比之下,还是spring-test标准一些。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!