栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何对javanica @HystrixCommand带注释的方法进行单元测试?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何对javanica @HystrixCommand带注释的方法进行单元测试?

尽管您不一定需要UNIT测试hystrix命令。进行某种春季混合测试仍然有用,我认为在添加注释时点空白接受功能是不正确的。我创建的测试可确保断路器在出现异常时断开。

@RunWith(SpringRunner.class)@SpringBootTestpublic class HystrixProxyServiceTests {    @MockBean    private MyRepo myRepo;    @Autowired    private MyService myService;    private static final String ID = “1”;    @Before    public void setup() {        resetHystrix();        openCircuitBreakerAfteroneFailingRequest();    }    @Test    public void circuitBreakerClosedonSuccess() throws IOException, InterruptedException {        when(myRepo.findoneById(USER_ID1))        .thenReturn(Optional.of(document.builder().build()));        myService.findoneById(USER_ID1);        HystrixCircuitBreaker circuitBreaker = getCircuitBreaker();        Assert.assertTrue(circuitBreaker.allowRequest());        verify(myRepo, times(1)).findoneById( any(String.class));    }    @Test    public void circuitBreakerOpenonException() throws IOException, InterruptedException {        when(myRepo.findoneById(ID)) .thenThrow(new RuntimeException());        try { myService.findoneById(ID);        } catch (RuntimeException exception) { waitUntilCircuitBreakerOpens(); HystrixCircuitBreaker circuitBreaker = getCircuitBreaker(); Assert.assertFalse(circuitBreaker.allowRequest());        }        verify(myRepo, times(1)).findoneById( any(String.class));    }    private void waitUntilCircuitBreakerOpens() throws InterruptedException {        Thread.sleep(1000);    }    private void resetHystrix() {        Hystrix.reset();    }    private void warmUpCircuitBreaker() {        myService.findoneById(USER_ID1);    }    public static HystrixCircuitBreaker getCircuitBreaker() {        return HystrixCircuitBreaker.Factory.getInstance(getCommandKey());    }    private static HystrixCommandKey getCommandKey() {        return HystrixCommandKey.Factory.asKey("findOneById");    }    private void openCircuitBreakerAfteroneFailingRequest() {        ConfigurationManager.getConfigInstance(). setProperty("hystrix.command.findOneById.circuitBreaker.requestVolumeThreshold", 1);    }}

让我绊了一会儿的另一件事是,我输入的默认注释没有特定的命令键,但是创建命令键时,它们是根据我上面指定的方法名称创建的。对于完整的示例,我还添加了注释以显示我未指定commandKey。

@HystrixCommandpublic Optional<document> findoneById(final String id) {    return this.myRepo.findoneById(id);}

希望这对某人有帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号