EasyMock使用注解的方式,进行springmvc的代码测试,测试代码如下:
pom文件引用
<!-- for junit start --> <dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.4.8</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-easymock</artifactId> <version>1.4.8</version> <scope>test </scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring_version}</version> <scope>test</scope> </dependency> <!-- for junit end -->
@RunWith(EasyMockRunner.class) public class LocMatchServiceTest { private static final Logger logger = Logger.getLogger(MatchServiceTest.class); @TestSubject private LocMatchService locMatchService = new LocMatchServiceImpl(); @Mock private MatchService matchService; @Mock private RedisMatch redisMatch; @Test public void testGetAppLocMatchInfo() throws Exception{ LocMatchInfo locMatchInfo = new LocMatchInfo(); locMatchInfo.setProductId(1000L); locMatchInfo.setCoordinate("test"); locMatchInfo.setActivitiesIllustration("test"); locMatchInfo.setActivityBegin(new Date()); locMatchInfo.setDetailAddress("test"); locMatchInfo.setActivityEnd(new Date()); locMatchInfo.setAddressId(100); EasyMock.expect(matchService.getLocMatchInfo(1000L)).andReturn(locMatchInfo); EasyMock.replay(matchService); EasyMock.expect(redisMatch.getMatchInfo("lms1000" )).andReturn(locMatchInfo); EasyMock.replay(redisMatch); EasyMock.expect(matchService.setLocMatchInfo(locMatchInfo)).andThrow(new MatchRpcException()); EasyMock.replay(redisMatch); ReflectionTestUtils.setField(locMatchService, "matchService", matchService); ReflectionTestUtils.setField(locMatchService, "redisMatch", redisMatch); LocMatchInfo record = locMatchService.getAppLocMatchInfo(1000L); assertEquals(locMatchInfo,record ); } }