1、在pom文件中添加依赖
<!--spring-test依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
<scope>test</scope>
</dependency>
<!--junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2、编写待测程序Max.java
package com.spring;
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、配置Max的bean
<bean id="max" class="com.spring.Max">
<constructor-arg name="a" value="5"/>
<constructor-arg name="b" value="3"/>
</bean>
4、创建单元测试代码
-
在Max类的声明的后面按“Alt+Enter”,选择“Create Test”,然后选择JUnit4,并勾选待测方法getMax(),点击OK
5MaxTest类
在Test包下,会自动创建一个MaxTest类,具体代码如下:
package com.spring;
import org.apache.log4j.Logger;
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.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring.xml"})
public class MaxTest {
private static Logger log = Logger.getLogger(MaxTest.class.getClass());
@Autowired
private Max max;
@Test
public void getMax() {
log.debug("test by mqxu");
assertEquals(5, max.getMax());
}
}
- 头部两个注解表示指定运行环境和指定配置文件路径,使用了@Autowired自动注入配置文件中的bean,使用assertEquals断言,判断期望值和实际值是否相等,并使用Logger记录日志。
5、运行结果
6、综合练习
- 定义User类,封装account和password属性,提供getter/setter,重载构造方法,重写toString()方法
- 定义UserLogin类,封装User属性,提供getter/setter,并定义public boolean userLogin(User user);方法,判定某个用户能否登陆成功,指定账号为“admin”,密码为“111”
- 写配置文件,通过构造器注入创建一个User对象并传值,通过setter注入创建UserLogin对象并传值
- 写单元测试,测试某用户能否登陆成功
- User类
package com.spring.ioc;
public class User {
private String account;
private String password;
public User() {
}
public User(String account, String password) {
this.account = account;
this.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;
}
@Override
public String toString() {
return "User{" + "account='" + account + '\'' + ", password='" + password + '\'' + '}';
}
}
- UserLogin类
package com.spring.ioc;
public class UserLogin {
private User user;
public UserLogin() {
}
public UserLogin(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean userLogin(User user) {
if ("admin".equals(user.getAccount()) && "123".equals(user.getPassword())) {
return true;
} else {
return false;
}
}
}
- 配置文件
<!--配置一个User的bean-->
<bean id="user" class="com.spring.ioc.User">
<constructor-arg name="account" value="admin"/>
<constructor-arg name="password" value="123"/>
</bean>
<!--配置一个UserLogin的bean-->
<bean id="userLogin" class="com.spring.ioc.UserLogin">
<property name="user" ref="user"/>
</bean>
- 单元测试类
package com.spring.ioc;
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 = {"/applicationContext.xml"})
public class UserLoginTest {
@Autowired
private User user;
@Autowired
private UserLogin userLogin;
@Test
public void userLogin() {
assertEquals(true, userLogin.userLogin(user));
}
}
-
运行结果