JUnit与Spring-Test结合对Spring框架进行单元测试
程序员文章站
2022-04-26 10:29:35
...
1、添加所需jar包的依赖:spring-test及junit
2、将测试类的头部加注解:
@RunWith(SpringJUnit4ClassRunner.class) //spring-test的测试工具
@ContextConfiguration({"classpath*:spring-*.xml"}) //在类加载前用于构建spring上下文的配置文件
3、在测试方法上加@Test注解
import hj.nerp.service.UserService;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:spring/spring-context-*.xml"})
public class TestDecorator {
@Autowired
private UserService userService;
@Test
public void testAspect() {
System.out.println("ssssss");
userService.getUserName();
}
}
下一篇: 爬虫利器:Requests库使用