springboot单元测试

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

你可能感兴趣的:(Spring相关)