springboot单元测试-junit4和junit5

注意:test的包名要和main的包名对应才行

下面是Junit4的写法

1.在想测试的方法里使用idea的代码自动生成功能,在该类下右键,点击generate,选择test,因为这里没有我们 想要的测试框架,所以我们随便选择一个测试框架,目的就是为了生成包和代码,然后找到生成的代码,在类上面添加
@RunWith(SpringRunner.class)
@SpringBootTest
这两个注解。在测试方法上加@Test注解就可以了

import org.junit.Test;

@RunWith(SpringRunner.class)
@SpringBootTest
public class GetAnswerTest {

    @Autowired
    private QuesRepository quesRepository;

    private static ThreadFactory ansMinThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("ansMin-pool-%d").build();
    private static ExecutorService ansMinPool = new ThreadPoolExecutor(1, 1,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(512), ansMinThreadFactory, new ThreadPoolExecutor.AbortPolicy());

    @Test
    public void run() {
        Long id = Long.parseLong("636183148362727424");
        Ques ques = quesRepository.getOne(id);
        GetAnswer getAnswer = new GetAnswer(ques);
        getAnswer.run();
    }
}

下面是Junit5的写法

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest
class DemoApplicationTests {
 
    @Test
    void contextLoads() {
    }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值