注意: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() {
}
}