0

测试数据库连接 Spring

如何使用自动装配的 spring bean 测试数据库连接。

http://www.tutorialspoint.com/spring/spring_jdbc_example.htm

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/bean.xml"})
public class TestDataBase {

  @Autowired
  private DataSource ds;

  private StudentJDBCTemplate  StudentJDBCTemplateObj;

  @Test
  public void test() {

    StudentJDBCTemplateObj = new StudentJDBCTemplate();
    StudentJDBCTemplateObj.setDataSource(ds);

    Student st = StudentJDBCTemplateObj.getStudent(1);

    Integer expected = 1; 
    Integer actual = st.getId();

    assertEquals(expected, actual); 
  }
}
4

0 回答 0