SpringBootTest 单元测试遇到的坑

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/libraryhu/article/details/70239110
项目结构是spring boot搭建的提供RPC接口的服务,用到了dubbo;

最近需要用到单元测试,发现一致卡在那里,执行不到测试方法,貌似阻塞了;

单元测试类:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.NONE, classes = Application.class)
public class TagTest {
	@Test
	public void test(){
	System.out.println("***************");
	}
}

找了半天,经过多次实践以及跟踪源码分析,发现执行到 DubboServiceLatchCommandLineRunner 中,在方法run中阻塞了;


package com.wacai.springboot.dubbo.commands;

import com.wacai.goodies.lifecycle.ShutdownLatch;
import org.springframework.boot.CommandLineRunner;


public class DubboServiceLatchCommandLineRunner implements CommandLineRunner {

    private String domain = "com.wacai.lifecycles";

    @Override
    public void run(String... args) throws Exception {
        ShutdownLatch latch = new ShutdownLatch(getDomain());
        latch.await();  //这里会导致阻塞
    }


    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }
}

后来把 maven配置文件中的以下依赖注释掉,再跑单元测试就可以了

<dependency>
    <groupId>com.wacai</groupId>
    <artifactId>spring-boot-starter-dubbo</artifactId>
    <version>1.0.3</version>
</dependency>



展开阅读全文

没有更多推荐了,返回首页