发表于: 2017-09-21 20:21:31
1 712
今天完成的事情:
一、学习spring 配置Junit4测试单元
(1)pom.xml中加入Junit的jar依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
(2)本文以之前写的spring框架下jdbcTemplate访问数据库的项目为例来写一个Junit4单元测试,在使用jdbcTemplate接口访问数据库的时候,最关键的核心就是加载spring的bean文件来创建context环境来实现jdbcTemplate实例化。这里主要测试是否能够根据spring中bean文件的设置来成功获取这个jdbcTemplate实例。下面是spring 配置文件beans.xml中关于jdbcTemplate的描述信息。
<!-- Definition for userJDBCTemplate bean -->
<bean id="userJdbcTemplate" class="jdbcTemplate.UserJdbcTemplate">
<property name="dateSource" ref="dataSource" />
</bean>
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/task" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean
(3)本文采用的是设置注入的方式,为了能够直观的获取测试结果,需要把测试方法做一下改动红色部分让set方法输出一个实例化jdbcTemplate的哈希值。
//连接数据库
public void setDateSource(DataSource dataSource){
this.dateSource=dataSource;
this.jdbcTemplateuser=new JdbcTemplate(dataSource);
System.out.println("jdbcTemplateuser: "+jdbcTemplateuser.hashCode());
}
(4)测试用例设计
public class UserJdbcTemplateTest {
@Test
public void testcontext() throws Exception {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
}
}
这里采用@Test注解方式来测试用例,运行结果值如下:
jdbcTemplateuser: 2095490653
----------------------------------------------------------------------------------------------------------------------------
这里可看出能够正常执行setDateSource方法,从而也就说明了beans.xml文件中的定义的id已经被加载了也间接测试了spring框架已经可以正常使用了,一维beans.xml文件被正常加载了。
二、简单学习Java web的相关知识,并在Tomcat中实现小案例。
(1)在eclipse里新建动态Java web项目
(2)在webcontent文件下新建jsp文件,命名index.jsp
(3)配置Tomcat服务器加入到项目中,参考网上的教程不在赘述http://jingyan.baidu.com/article/ca2d939dd90183eb6d31ce79.html
(4)在浏览器里输入http://localhost:8088/Myjavaweb/index.jsp
显示效果,有点简陋,,,,
明天的计划:
(1)计划学习日志查看debug
问题:
暂无
收获:
1.Java里实现一个小功能背后都有一个很复杂的过程,要学的东西还有很多。
评论