EasyMock uses the annotation method to test the code of springmvc

 

EasyMock uses the annotation method to test the code of springmvc. The test code is as follows:

 

pom file reference

 

<!-- 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 );
    }
}

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040775&siteId=291194637