当前位置: 首页 > news >正文

外链建设都需要带网站网址网站建设服务器

外链建设都需要带网站网址,网站建设服务器,2345网址电脑版首页,湖南长沙门户网站目录 一、配置文件方式 1.导入依赖 2.编写类和方法 3.配置文件applicationContext-test.xml 4.测试类 5.运行结果 二、全注解方式 1.编写类和方法 2.配置类 3.测试类 4.运行结果 每次进行单元测试的时候,都需要编写创建工厂,加载配置文件等相关…

目录

一、配置文件方式

1.导入依赖

2.编写类和方法

3.配置文件applicationContext-test.xml

4.测试类

5.运行结果

二、全注解方式

1.编写类和方法

2.配置类

3.测试类

4.运行结果


每次进行单元测试的时候,都需要编写创建工厂,加载配置文件等相关操作,比较繁琐,现在Spring提供了整合Junit单元测试的技术,可以简化测试开发,所以必须首先要有Junit单元测试的环境,也就是要导入Junit单元测试的依赖,还有spring-test的依赖

这里用了两种方式去整合,一种是配置文件的方式,一种是全注解的方式

一、配置文件方式

1.导入依赖

需要导入spring的核心依赖、Junit单元测试的依赖和spring-test的依赖

    <dependencies><!--spring的核心依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><!-- https://mvnrepository.com/artifact/log4j/log4j --><!--日志--><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.12</version></dependency>------------------------------------------------------//主要是这个<!--测试--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>-------------------------------------------------------<!--连接池--><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><!--mysql--><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency>------------------------------------------------------------//也得有这个<!--spring-test--><!-- https://mvnrepository.com/artifact/org.springframework/spring-test --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version><scope>test</scope></dependency>----------------------------------------------------------</dependencies>

2.编写类和方法

把该类交给spring去管理

public class User {public void sayHello(){System.out.println("Hello--------------");}
}

3.配置文件applicationContext-test.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--整合单元测试--><bean id="user" class="com.test.pojo.User"></bean>
</beans>

4.测试类

import com.test.pojo.User;
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;/*
* Spring整合Junit单元测试
* */
@RunWith(value = SpringJUnit4ClassRunner.class)  //运行单元测试
@ContextConfiguration(value = "classpath:applicationContext-test.xml")//加载类路径下的配置文件
public class Demo {//按类型自动注入@Autowiredprivate User user;@Testpublic void run1(){user.sayHello();}
}

5.运行结果

二、全注解方式

依赖没有变,只是没有配置文件了,换成了配置类;

并且重新换了一个类Customer,没有用配置文件方式里的User类

1.编写类和方法

import org.springframework.stereotype.Component;@Component
public class Customer {public void save(){System.out.println("保存客户");}
}

2.配置类

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration//声明当前是配置类
@ComponentScan(value = "com.test.pojo") //扫描包
public class SpringConfig {}

3.测试类

import com.test.config.SpringConfig;
import com.test.pojo.Customer;
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(value = SpringJUnit4ClassRunner.class)  //运行单元测试
@ContextConfiguration(classes = SpringConfig.class)//加载配置类
public class Demo1 {@Autowired //按类型注入private Customer customer;@Testpublic void run1(){customer.save();}}

4.运行结果

http://www.yayakq.cn/news/538773/

相关文章:

  • 佛山网站优化体验做网站的又营业执照的吗
  • 西宁网站系统建设泸州中泸集团建设有限公司网站
  • 网站开发与设计这么样长沙网站推广系统
  • 河北集团网站建设做收费网站
  • wordpress 修改建站时间网站模版 蓝色
  • 做网站不切片可以吗火脉推广平台
  • 江苏建设造价信息网站app研发流程
  • 企业建站用什么系统开发一款app需要投入多少钱
  • 建筑专业网站建设做一个一般的网站需要多少钱
  • 阿里巴巴1688怎么做网站做个app软件多少钱
  • 深圳网站建设 沙漠风wordpress 管理员权限
  • 庐江网站建设网站建设云解析dns有什么用
  • 宝安沙井网站建设检索类的网站建设
  • 网站建设需要ui吗10个暴利小生意创业
  • 网站做微信支付接口WordPress文章设置密码
  • 惠州做棋牌网站建设apt 安装wordpress
  • 做网站赚钱还是企业宣传片案例平台 网站
  • 东莞网站搭建哪家强门户网站建设的报价
  • 本地佛山企业网站建设爱论坛
  • 如何建设网站步骤好用的h5网站模板下载
  • 淘宝客做销量的网站有哪些用tomcat做网站目录
  • 美工网站深圳免费网站优化网络推广
  • 提供提供手机网站建设郑州新感觉会所网站哪里做的
  • 企业在网站建设后期需要做什么建筑网建筑规范大全
  • 做网站号码网站建设要花钱吗
  • 把网站放在虚拟主机上 怎么进入网站后台增城新塘镇 企业网站建设
  • 和各大网站做视频的工作南宁百度seo网站优化
  • 网站上的公告怎么做参考文献顶呱呱网站做的怎么样
  • 深圳做棋牌网站建设哪家技术好焦作做网站的公司
  • 快递网站域名更换兰州做网站怎么样