Spring-cloud-一步步实现广告系统-20-系统运行测试

6次阅读

共计 5358 个字符,预计需要花费 14 分钟才能阅读完成。

系统运行

经过长时间的编码实现,我们的主体模块已经大致完成,因为之前我们都是零散的对各个微服务自行测试,接下来,我们需要将所有的服务模块进行联调测试,Let’s do it.

清除测试数据 & 测试文件

我们在实现各个服务的过程中,添加了不少的测试文件和测试数据,为了不影响我们最终的展示效果,我们先将之前的历史数据清理掉。

drop database advertisement;

依然使用 flyway 添加我们的测试数据:

INSERT INTO `ad_user` VALUES (10,'Isaac','B2E56F2420D73FEC125D2D51641C5713',1,'2019-08-14 20:29:01','2019-08-14 20:29:01');

INSERT INTO `ad_creative` VALUES (10,'第一个创意',1,1,720,1080,1024,0,1,10,'https://www.life-runner.com','2019-08-14 21:31:31','2019-08-14 21:31:31');

INSERT INTO `ad_plan` VALUES (10,10,'推广计划名称',1,'2019-11-28 00:00:00','2019-11-20 00:00:00','2019-11-19 20:42:27','2019-08-14 20:57:12');

INSERT INTO `ad_unit` VALUES (10,10,'第一个推广单元',1,1,10000000,'2019-11-20 11:43:26','2019-11-20 11:43:26'),(12,10,'第二个推广单元',1,1,15000000,'2019-01-01 00:00:00','2019-01-01 00:00:00');

INSERT INTO `ad_unit_district` VALUES (10,10,'陕西省','西安市'),(11,10,'陕西省','西安市'),(12,10,'陕西省','西安市'),(14,10,'山西省','阳泉市');

INSERT INTO `ad_unit_hobby` VALUES (10,10,'爬山'),(11,10,'读书'),(12,10,'写代码');

INSERT INTO `ad_unit_keyword` VALUES (10,10,'汽车'),(11,10,'火车'),(12,10,'飞机');

INSERT INTO `relationship_creative_unit` VALUES (10,10,10);

导出测试索引文件

可参考 全量索引传送门,或者下载源码 github 传送门 / gitee 传送门 , 运行 mscx-ad-db 项目,然后执行 http://localhost:7002/ad-db/export/plan。

开发自测 Unit Test

一个合格的开发人员是绝对不能容忍自己的代码存在傻~ bug 存在的,但是个人总会有犯错的时候,那么我们要怎么避免此类非业务发展导致的基础问题呢,这时候,开发的 UT 就显得非常 Important 了。

广告投放系统测试

我们来编写投放系统的单元测试,如下图:

单元测试模块的目录结构与我们的正式项目结构保持一致,如果你需要给单元测试编写特例化配置,把我们的 application.yml 配置文件 copy 到 UT 中就可以了,这里就不做赘述。

  • 用户服务单元测试

      /**
  *
  * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初 </a>
  */
  @RunWith(SpringRunner.class)
  @SpringBootTest(classes = {SponsorApplication.class},
    webEnvironment = SpringBootTest.WebEnvironment.NONE
    )
  public class UserServiceTest {

      @Autowired
      private IUserService userService;

      @Test
  //    @Transactional
      public void testCreateUser() throws AdException {UserRequestVO userRequestVO = new UserRequestVO("Isaac Zhang");

          UserResponseVO responseVO = userService.createUser(userRequestVO);

          assert responseVO.getUserName() == "Isaac Zhang";

          System.out.printf("创建用户: %s", JSON.toJSONString(responseVO));
      }
  }
```

大家可以看到,在上述代码中,我们测试了创建用户的 service 方法,特别注意 2 个点:

  • @Transactional注解.

因为我们使用的是和正式服务相同的数据库,我们在测试的时候就会真实的插入一个用户到 ad_user 表中,如果我们不想这个用户存入表中,就需要加上 @Transactional 注解,我们的创建就不会 commit,也就不会被插入到真实数据库中。

  • @SpringBootTest 注解
    classes表明测试启动类是哪个,webEnvironment = SpringBootTest.WebEnvironment.NONE表明我们当前的测试并非一个 web 环境。

这里就不针对每一个 service 进行单元测试的编写,但是大家一定要记住,在真实的企业开发环境中,大的开发团队一定会对单元测试的代码覆盖率有一个要求,一般都不会低于 60%, 我个人对自己的 行代码覆盖率 是 > 80%. 这样才能真实的保证我们的每一个方法都尽量都执行和验证到。

大家尝试依次实现其余的单元测试吧。

广告检索系统测试

我们的检索服务对外只提供一个服务,因此我们只需要创建一个 Test 类就可以了,let’s code.

package com.sxzhongf.ad.search;

import com.sxzhongf.ad.AdSearchApplication;
import com.sxzhongf.ad.search.vo.SearchRequest;
import com.sxzhongf.ad.search.vo.SearchResponse;
import com.sxzhongf.ad.search.vo.feature.DistrictFeature;
import com.sxzhongf.ad.search.vo.feature.FeatureRelation;
import com.sxzhongf.ad.search.vo.feature.HobbyFeatrue;
import com.sxzhongf.ad.search.vo.feature.KeywordFeature;
import com.sxzhongf.ad.search.vo.media.AdSlot;
import com.sxzhongf.ad.search.vo.media.App;
import com.sxzhongf.ad.search.vo.media.Device;
import com.sxzhongf.ad.search.vo.media.Geo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * SearchTest for 搜索服务测试用例
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初 </a>
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdSearchApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class SearchTest {

    @Autowired
    private ISearch search;

    @Test
    public void testFetchAds() {SearchRequest request = new SearchRequest().builder()
                                                   .mediaId("isaac-search-mediaId")
                                                   .requestInfo(new SearchRequest.RequestInfo(
                                                           "request id",
                                                           Arrays.asList(new AdSlot().builder()
                                                                               .adSlotCode("slot code")
                                                                               .height(800)
                                                                               .minCpm(1024)
                                                                               .positionType(1)
                                                                               .type(Arrays.asList(1))
                                                                               .build()),
                                                           buildSimpleApp(),
                                                           buildSimpleGeo(),
                                                           buildSimpleDevice()))
                                                   .featureInfo(
                                                           buildSimpleFeatureInfo(Arrays.asList("汽车", "火车", "飞机"),
                                                                   Collections.singletonList(
                                                                           new DistrictFeature.ProvinceAndCity("陕西省", "西安市")
                                                                   ),
                                                                   Arrays.asList("爬山", "写代码", "飞机"),
                                                                   FeatureRelation.OR
                                                           )
                                                   )
                                                   .build();
        SearchResponse response = search.fetchAds(request);
//        assert response.adSlotRelationAds.get(0).contains("key");

        System.out.println("开始查询广告拉:" + response);
    }

    /**
     * 创建 demo {@link App}
     */
    private App buildSimpleApp() {return new App().builder()
                        .activityName("simple App activityName")
                        .appCode("simple App appCode")
                        .appName("simple app name")
                        .packageName("simple app package name")
                        .build();}

    /**
     * 创建 demo {@link Geo}
     */
    private Geo buildSimpleGeo() {return new Geo().builder()
                        .longitude(Float.valueOf("100.2222222"))
                        .latitude(Float.valueOf("38.8888888"))
                        .city("xiaan")
                        .province("shaanxi")
                        .build();}

    /**
     * 创建 demo {@link Device}
     */
    private Device buildSimpleDevice() {return new Device().builder()
                           .deviceCode("simple device code")
                           .deviceMacAddr("simple mac addr")
                           .displaySize("simple display size")
                           .ip("127.0.0.1")
                           .model("simple model")
                           .screenSize("simple screen size")
                           .serialName("simple serial name")
                           .build();}

    private SearchRequest.FeatureInfo buildSimpleFeatureInfo(
            List<String> keywords,
            List<DistrictFeature.ProvinceAndCity> provinceAndCities,
            List<String> hobbys,
            FeatureRelation featureRelation
    ) {
        return new SearchRequest.FeatureInfo(new KeywordFeature(keywords),
                new DistrictFeature(provinceAndCities),
                new HobbyFeatrue(hobbys),
                featureRelation
        );
    }
}

在这个测试用例中,我们主要的复杂性是在组件各种查询条件,这个就需要各位伙伴在理解业务的时候需要万分上心。

正文完
 0