学SSM第二天-SSM框架的测试test

第一步在pom.xml中加入依赖及创建applicationContext.xml

注意:一定要添加Junit4.12或更高版本,否则报错!

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

				<!--Spring 依赖-->
				<!-- spring-context -->
				<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>spring-context</artifactId>
						<version>5.2.9.RELEASE</version>
				</dependency>
				<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>spring-test</artifactId>
						<version>5.2.9.RELEASE</version>
						<scope>test</scope>
				</dependency>
				<!-- spring-web -->
				<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>spring-web</artifactId>
						<version>5.2.9.RELEASE</version>
				</dependency>
				<!--servlet相关的3个jar包-->
				<dependency>
						<groupId>jstl</groupId>
						<artifactId>jstl</artifactId>
						<version>1.2</version>
				</dependency>
				<dependency>
						<groupId>javax.servlet</groupId>
						<artifactId>javax.servlet-api</artifactId>
						<version>3.1.0</version>
						<scope>provided</scope>
				</dependency>
				<dependency>
						<groupId>javax.servlet.jsp</groupId>
						<artifactId>javax.servlet.jsp-api</artifactId>
						<version>2.2.1</version>
						<scope>provided</scope>
				</dependency>

在resource目录底下创建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.xsd
	   http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
		<!--配置扫描路径-->
		<context:component-scan base-package="com.xy"/>
</beans>

第二步配置web.xml

<context-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:applicationContext.xml</param-value>
		</context-param>

		<listener>
				<!--
				  监听ServletContext对象创建,然后将spring容器放到该ServletContext中缓存起来
				-->
				<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
		</listener>

第三步 编写测试基类,以后所有测试类继承此类

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("src/main/resources")
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class BaseTest {

		@Test
		public void test() {
				System.out.println("test");
		}
}

第四步测试

public class AppTest extends  BaseTest{
		//自动注入service
		@Autowired
		private PersonService personService;
		
		@Test
		public void test01(){
				//业务逻辑
				
		}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值