一、Max练习
1、在pom.xml中添加依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
2、编写Max.java
public class Max {
private int a;
private int b;
public Max(int a,int b){
this.a=a;
this.b=b;
}
public int getMax(){
return a > b ? a : b;
}
}
3、配置bean
<!--配置Max-->
<bean id="max" class="com.spring.IoC.Max">
<constructor-arg name="a" value="5"/>
<constructor-arg name="b" value="3"/>
</bean>
4、创建Max单元测试
-
在Max类的声明的后面按“Alt+Enter”,选择“Create Test”,或者在类的代码体右键单击,选择Generate->Test,在如图所以界面勾选JUnit4,并勾选待测方法getMax(),点击OK
- 完成后, 在Test包下,会自动创建一个MaxTest类,具体代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
//指定单元测试环境
@RunWith(SpringJUnit4ClassRunner.class)
//配置文件路径
@ContextConfiguration(locations = {"/beans.xml"})
/**
* Created by Administrator on 2019/3/4.
*/
public class MaxTest {
//自动注入
@Autowired
private Max max;
@Test
public void getMax() throws Exception {
assertEquals(5,max.getMax());
}
}
头部两个注解表示指定运行环境和指定配置文件路径,使用了@Autowired自动注入配置文件中的bean,使用assertEquals断言,判断期望值和实际值是否相等,并使用Logger记录日志。
5、运行结果
二、user UserLogin练习
定义User类,封装account和password属性,提供getter/setter,重载构造方法,重写toString()方法
定义UserLogin类,封装User属性,提供getter/setter,并定义public boolean userLogin(User user);方法,判定某个用户能否登陆成功,指定账号为“admin”,密码为“111”
写配置文件,通过构造器注入创建一个User对象并传值,通过setter注入创建UserLogin对象并传值
写单元测试,测试某用户能否登陆成功
- User类
public class User {
private String account;
private String password;
public User(String account, String password) {
this.account = account;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"account='" + account + '\'' +
", password='" + password + '\'' +
'}';
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- UserLogin类
public class UserLogin {
private User user;
public UserLogin(){}
public UserLogin(User user) {
this.user = user;
}
public boolean UserLogin(User user){
if(user.getAccount().equals("admin")&&user.getPassword().equals("111")){
return true;
}else{
return false;
}
}
}
- UserLoginTest类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class) //指定单元测试环境
@ContextConfiguration(locations = {"/beans.xml"}) //配置文件路径
public class UserLoginTest {
@Autowired
private UserLogin userLogin;
@Test
public void userLogin() throws Exception {
assertEquals(true,userLogin.UserLogin(userLogin.getUser()));
}
}
-
运行结果