标签:getbean junit pos framework version lazy repos hot 步骤
1、每个测试都要重新启动spring
2、测试代码在管理spring容器,应该是spring容器在管理测试代码
创建一个子工程。(spring-02-test)
1、添加maven依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring</artifactId>
<groupId>com.luke</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-02-test</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、编写测试类
package com.luke.test;
import com.luke.pojo.Hello;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//表示先启动Spring容器,把junit运行在Spring容器中
@RunWith(SpringJUnit4ClassRunner.class)
//表示从哪里加载资源文件
@ContextConfiguration("classpath:applicationContext.xml")
public class TestHello {
//自动加载
@Autowired
BeanFactory beanFactory ;
@Test
public void test(){
Hello hello = beanFactory.getBean("hello", Hello.class);
hello.sayHello();
}
}
标签:getbean junit pos framework version lazy repos hot 步骤
原文地址:https://www.cnblogs.com/prefordan/p/14631296.html