Demo entry 6633645

测试代码

   

Submitted by anonymous on Aug 04, 2017 at 07:14
Language: Java. Code size: 5.3 kB.

package com.hivescm.integ;

import com.hivescm.common.domain.DataResult;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * Created by hub on 2017/7/31.
 */
public class FeignTest {
    private RestTemplate restTemplate;
    @Before
    public void init(){
        restTemplate = new RestTemplate();
        restTemplate.delete("http://localhost:8887/demo-client/removeArtByFeign/33", DataResult.class);
        restTemplate.delete("http://localhost:8887/demo-client/removeUserByFeign/88", DataResult.class);
    }



    private MultiValueMap genUserInput(){
        MultiValueMap map = new LinkedMultiValueMap();
        map.set("id", 88L);
        map.set("name", "李四");
        map.set("artCount", 7L);
        return map;
    }

    private MultiValueMap genArtInput(){
        MultiValueMap map = new LinkedMultiValueMap();
        map.set("id", 33L);
        map.set("uid", 88L);
        map.set("content", "我们的家乡");
        return map;
    }

    @Test
    public void addUser(){
        MultiValueMap map = genUserInput();
        DataResult dataResult= restTemplate.postForObject("http://localhost:8887/demo-client/addUserByFeign", map, DataResult.class);
        Object o = dataResult.getResult();
        Assert.assertEquals("插入结果未符合预期", 1, o);
    }

    @Test
    public void getUser(){
        MultiValueMap map = genUserInput();
        restTemplate.postForObject("http://localhost:8887/demo-client/addUserByFeign", map, DataResult.class);

        DataResult dataResult = restTemplate.getForObject("http://localhost:8887/demo-client/getUserByFeign/88", DataResult.class);
        Map map1 = (Map) dataResult.getResult();
        //此处涉及序列化问题,当数值在int范围内返回int类型,在long范围内返回long类型,故直接用断言会出错,但是对实际无显著影响。
        Assert.assertEquals("取出之id不对", 88L + "", map1.get("id") + "");
    }

    @Test
    public void removeUser(){
        MultiValueMap map = genUserInput();
        restTemplate.postForObject("http://localhost:8887/demo-client/addUserByFeign", map, DataResult.class);
        restTemplate.delete("http://localhost:8887/demo-client/removeUserByFeign/88", DataResult.class);
    }
    @Test
    public void addArt() throws  Exception{
        MultiValueMap userMap = genUserInput();
        restTemplate.postForObject("http://localhost:8887/demo-client/addUserByFeign", userMap, DataResult.class);
        MultiValueMap artMap = genArtInput();
        //连续插入两条文章,看日志。
        DataResult dataResult = restTemplate.postForObject("http://localhost:8887/demo-client/addArtByFeign", artMap, DataResult.class);
        Thread.sleep(500);
        DataResult dataResult2 = restTemplate.postForObject("http://localhost:8887/demo-client/addArtByFeign", artMap, DataResult.class);
        Object o = dataResult.getResult();
        Assert.assertEquals("插入结果未符合预期", 1, o);
    }

    @Test
    public void getArt() throws  Exception{
        MultiValueMap userMap = genUserInput();
        restTemplate.postForObject("http://localhost:8887/demo-client/addUserByFeign", userMap, DataResult.class);
        MultiValueMap artMap = genArtInput();
        restTemplate.postForObject("http://localhost:8887/demo-client/addArtByFeign", artMap, DataResult.class);
        //稍后,保证消息到达并消费。因为消息异步发送立即返回,可能出现get操作先于add消息到达的情形,所以找不到数据,并不是get本身有问题。
        Thread.sleep(1000);
        DataResult dataResult = restTemplate.getForObject("http://localhost:8887/demo-client/getArtByFeign/33", DataResult.class);
        Map map1 = (Map) dataResult.getResult();
        Assert.assertEquals("取出之id不对", 33L + "", map1.get("artId") + "");
    }

    @Test
    public void removeArt(){
        MultiValueMap userMap = genUserInput();
        restTemplate.postForObject("http://localhost:8887/demo-client/addUserByFeign", userMap, DataResult.class);
        MultiValueMap artMap = genArtInput();
        restTemplate.postForObject("http://localhost:8887/demo-client/addArtByFeign", artMap, DataResult.class);
        restTemplate.delete("http://localhost:8887/demo-client/removeArtByFeign/33");
    }

    @Test
    public void transaction() throws Exception{
        MultiValueMap userMap = genUserInput();
        userMap.set("artCount", 10L);
        MultiValueMap artMap = genArtInput();
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.postForObject("http://localhost:8887/demo-client/addUserByFeign", userMap, DataResult.class);
        restTemplate.postForObject("http://localhost:8887/demo-client/addArtByFeign", artMap, DataResult.class);
        Thread.sleep(500);
        DataResult artDataResult = restTemplate.getForObject("http://localhost:8887/demo-client/getArtByFeign/33", DataResult.class);
        Assert.assertNull("不该插入的文章却插入了", artDataResult.getResult());
        DataResult userDataResult = restTemplate.getForObject("http://localhost:8887/demo-client/getUserByFeign/88", DataResult.class);
        Map returnUserMap = (Map) userDataResult.getResult();
        Assert.assertEquals("取出之文章数目不对", 10L + "", returnUserMap.get("artCount") + "");
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).