Spring boot 使用Junt

本文介绍如何使用SpringBoot和JUnit4进行整合测试。通过@RunWith注解指定启动器为SpringJUnit4ClassRunner,并使用@SpringBootTest注解加载启动类,替代了传统的@ContextConfiguration。示例代码展示了一个名为TestLogin的测试类,该类使用了@Autowired注解注入DiscoveryClient,并通过@Test注解定义了一个名为test0的测试方法,用于获取本服务的信息。
//@RunWith:启动器,SpringJUnit4ClassRunner:Spring整合JUnit4
//@SpringBootTest获取启动类,相当于@Contextconfiguartion("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = UserServiceApplication.class)
public class TestLogin {
    @Autowired
    private DiscoveryClient client;

    @Test
    public void test0() {
        //获取本服务的信息
        ServiceInstance instance = client.getLocalServiceInstance();
        Object data = "test0!";
        Map map = new HashMap();
        map.put("service", instance.getServiceId());
        map.put("serviceInfo", instance);
        map.put("data", data);
        System.out.println(map);
    }
}

 

转载于:https://www.cnblogs.com/chenglc/p/10690616.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值