发表于: 2018-01-12 23:37:55

1 639


完成

1.使用junit 对spring进行单元测试(参考https://my.oschina.net/dlpinghailinfeng/blog/336694

实现代码如下

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.ptteng</groupId>
   <artifactId>SpringJunitDemo</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

   <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.11</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.mockito</groupId>
           <artifactId>mockito-core</artifactId>
           <version>1.9.5</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-test</artifactId>
           <version>3.2.3.RELEASE</version>
           <scope>test</scope>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>3.2.3.RELEASE</version>
       </dependency>
   </dependencies>

   <build>
       <plugins>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>2.5</version>
               <configuration>
                   <skipTests>true</skipTests>
               </configuration>
           </plugin>
       </plugins>
   </build>

</project>

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
   <!-- 声明employee对象,交给spring创建
   constructor-arg:通过构造函数注入。
  property:通过setter对应的方法注入。 -->
   <bean id="employee" name="employee" class="Employee" autowire="default">
       <constructor-arg name="age" value="20"></constructor-arg>
       <constructor-arg name="name" value="zhangsan"></constructor-arg>
   </bean>
</beans>

Employee.java

public class Employee {
private Integer age;
   private String name;

   public Integer getAge() {
return age;
   }

public void setAge(Integer age) {
this.age = age;
   }

public String getName() {
return name;
   }

public void setName(String name) {
this.name = name;
   }


public Employee(Integer age, String name) {
this.age = age;
       this.name = name;
   }

@Override
   public String toString() {
return "Employee name is " + name + ",age is" + age;
   }

}

测试类

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)//使用junit4进行测试
@ContextConfiguration(locations = {"classpath:ApplicationContext.xml"})////加载配置文件

public class EmployeeTest {
@Autowired
   ApplicationContext ctx;

   @Test
   public void testEmployee(){
Employee employee = (Employee) ctx.getBean("employee");
       //assertEquals("zhangsan",employee.getName());
       System.out.println(employee);
   }
}

结果

其中

Junit常用的声明方法API为Assert 类

方法 void assertEquals(boolean expected, boolean actual) 是检查两个变量或者等式是否平衡

其返回值为空,不注释掉的话也能测试通过,为了有个可视的结果,加了一句


2.学习java语法(以下内容摘自http://blog.csdn.net/w83325887/article/category/6344852

为什么封装?

婴儿太多,所以我们会考虑将每个出生的婴儿文档我们都会放于一个盒子里,这样方便以后我们查询他的身份证信息。于是在这个盒子上,我们打了一个孔,叫setXXX()方法,如果是身份证,我们就取名setCode()方法,这样就将一个人的身份证信息建立set了。但放进去后,我们有时候还需要检验这个人的身份证信息,所以我们又打了一个孔,叫getXXX()方法,对应于setXXX()方法。

 

对了,每个人的姓名和身份证号都是独一无二的,为了防止别人随意的修改这些信息,我们将这些信息标记为私人的,也就是私有的,英文表示就是private。 所以我们会表示为private String code;而仅仅是String code;

 

这样我们就把这些信息装入了一个盒子里,并保证了他的安全性。

封装原则
    * 将不需要对外提供的内容都隐藏起来。private
    * 把属性隐藏,提供公共方法对其访问。setXXX(), getXXX();

 为什么会有this呢?

为什么会有继承?

 

继承,顾名思义就是孩子继承父母的东西。

比如说老爸Dad()会sleep()。他有两个孩子Boy(),Girl()。

孩子也会睡觉,那我得在两个孩子里分别单独写sleep()方法,这也挺麻烦的。

还好只是两个孩子,要是有多个孩子,7个或8个怎么办,难道每个都要说会睡觉么,这样是不是显得废话特别多。

所以我们使用继承关系,只要老爸写一个,大家都可以用。


同时,我们人类都有一个祖先。

所以类也有一个祖先,叫Object类,Object可译为“客观的”

 

继承,用英语表示extends,也有扩展的意思。

所以说,继承的好处就是为了减少代码的重复率,也可以归纳为程序员的懒~~~

有好处当然也坏处,正如妈妈说的:不能随随便便拿不相关人的东西。

只有确认需要继承父类的东西,才能继承,可不能随便认爹。


为什么会有super?

因为super可以调用父类的成员变量。

当然this也可以调用父类的成员变量,还可以访问自己的成员变量。

然而,super还有一个隐藏功能,就是:如果没有默认的构造函数,就必须用super显式地编写调用构造函数的语句。这是this所无法完成的功能。


问题

没什么新问题


收获

老大今天谈话,说来修真院要注重团队协作,自学能力,解决问题的能力的训练。

认真对待小课堂,写写博客,有时间看看洗骨换髓基础知识,在任务过程中把Java语法补回来。


计划

1.学习SpringAOP

2.学习spring+mybatis操作数据库





返回列表 返回列表
评论

    分享到