前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于maven项目Spring+Junit

基于maven项目Spring+Junit

作者头像
muntainyang
发布2020-10-23 11:42:29
3480
发布2020-10-23 11:42:29
举报
文章被收录于专栏:IT测试前沿IT测试前沿

一、开发环境

eclipse版本:4.4.2

maven版本:3.5.0

junit版本:4.12

spring版本:5.0.9.RELEASE

JDK版本:1.8.0_102

二、项目结构

三、项目实现

1、新建项目

Maven包配置依赖

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>

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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 扫描所有类型的bean --> <context:component-scan base-package="com.spring.lsh.*"/> </beans>

2、创建测试类

@RunWith注解,使用junit整合spring

@ContextConfiguration注解,指定spring容器的位置

@RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration(locations={"classpath*:applicationContext.xml"}) @ContextConfiguration(locations={"file:D:* /webapp/WEB-INF/applicationContext.xml"})

publicclass UserServiceImpl_spring_test { @Autowired private UserService userService; @Test publicvoid testFindByID() { User user=userService.findByID(1); System.out.print(user.getName()); }

3、被测试的类

@Service("userService") publicclass UserServiceImpl implements UserService{ public User findByID(Integer id){ if(id==1){ User user=new User(); user.setAge("7"); user.setName("放假"); returnuser; } if(id==2){ User user=new User(); user.setAge("70"); user.setName("国庆"); returnuser; } returnnull; }

4、Use.java& UserService.java类

publicclass User { private Integer id; private String name; private String age; public Integer getId() { returnid; } publicvoid setId(Integer id) { this.id = id; } public String getName() { returnname; } publicvoid setName(String name) { this.name = name; } public String getAge() { returnage; } publicvoid setAge(String age) { this.age = age; }

@Component publicinterface UserService { public User findByID(Integer id); }

5、验证spring 依赖注入是否成功的测试类

publicstaticvoid main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"file:D:/software/dubbo/Spring_maven_demo1/src/main/webapp/WEB-INF/applicationContext.xml"}); UserService us = (UserService)context.getBean("userService"); User user=us.findByID(1); System.out.print(user.getName()); }

6、运行

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 IT测试前沿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 3、被测试的类
  • 5、验证spring 依赖注入是否成功的测试类
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档