使用Spring的隐式注解和装配以及使用SpringTest框架

时间:2023-11-15 17:22:38

SpringTestConfiguration

1.加入jar 包spring-test-4.3.9.RELEASE.jar

2.写基本的Component

注意级联状态下  需要给需要调用的属性加入getter方法

 package com.myth.spring.annotion.component;

 import org.springframework.stereotype.Component;

 @Component
public class Person {
private String name = "Sally";
private int age = 26;
private Car car ; public void travel(Car car) {
System.out.println(name + " go to travel by "+car.getCarName() + " when at "+age );
}
}
 package com.myth.spring.annotion.component;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class Car {
private String carName = "Q5"; private String carBrand = "Audi"; private int carPrice = 1000000; public void run() {
System.out.println("The car :"+carName+"'s brand is "+carBrand+" and the price is "+carPrice);
} //若级联的类需要调用类的方法时 需要加入get 方法
@Autowired
public String getCarName() {
return carName;
}
}

3.加入 ComponentScan的配置 basePackages 可以扫描多个包

 package com.myth.spring.annotion.configuration;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//加入Configuration
@Configuration
//加入ComponentScan并配置basepackage
@ComponentScan(basePackages="com.myth.spring.annotion")
public class TravelConfiguration {
}

使用Spring的隐式注解和装配以及使用SpringTest框架

并且可以使用excludeFilters 来排除不需要的组件

4.加入SpringTest的框架

1) 写入@RunWith(SpringJUnit4ClassRunner.class) 由SpringJunit4ClassRunner 来替我们创建容器

首先Junit  会根据我们的SpringJUnit4ClassRunner.class 找到  TestContext   然后创建 TestContextManager  然后将TestExecutionListenner 加入到  容器中

而下面有个AbstractTestExecutionListener 可以创建我们所需要的Listenner 可以在下面的代码中看到创建Listenner

使用Spring的隐式注解和装配以及使用SpringTest框架

  • TestContext:它封装了运行测试用例的上下文;
  • TestContextManager:它是进入 Spring TestContext 框架的程序主入口,它管理着一个 TestContext 实例,并在适合的执行点上向所有注册在 TestContextManager 中的 TestExecutionListener 监听器发布事件:比如测试用例实例的准备,测试方法执行前后方法的调用等。
  • TestExecutionListener:该接口负责响应 TestContextManager 发布的事件。

使用Spring的隐式注解和装配以及使用SpringTest框架

protected void dirtyContext(TestContext testContext, HierarchyMode hierarchyMode) {
testContext.markApplicationContextDirty(hierarchyMode);
testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE);
}

2) 写入@ContextConfiguration(classes=TravelConfiguration.class) 来识别配置的类

3) 用autowired 来表示自动装箱机制。

TestTravel

 package com.myth.spring.annotion.test;

 import static org.junit.Assert.assertNotNull;

 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; import com.myth.spring.annotion.component.Car;
import com.myth.spring.annotion.component.Person;
import com.myth.spring.annotion.configuration.TravelConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TravelConfiguration.class)
public class TestTravel {
@Autowired
private Person person;
@Autowired
private Car car; @Test
public void test() {
person.travel(car);
} }