建议先阅读
:Spring全家桶
步骤1:新建测试类
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfig.class) public class BookServiceTest {
}
|
步骤2:注入Service类
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfig.class) public class BookServiceTest {
@Autowired private BookService bookService;
}
|
步骤3:编写测试方法
我们先来对查询进行单元测试。
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfig.class) public class BookServiceTest {
@Autowired private BookService bookService;
@Test public void testGetById(){ Book book = bookService.getById(1); System.out.println(book); }
@Test public void testGetAll(){ List<Book> all = bookService.getAll(); System.out.println(all); }
}
|
根据ID查询,测试的结果为:
查询所有,测试的结果为: