本次CKA培训课程,通过线下授课、考题解读、模拟演练等方式,帮助学员快速掌握Kubernetes的理论知识和专业技能,并针对考试做特别强化训练,让学员能从容面对CKA认证考试,使学员既能掌握Kubernetes相关知识,又能通过CKA认证考试,点击下方图片了解详情。
public class JunitSpringTest { //单元测试类
private ApplicationContext context; //定义上下文变量
@Before //测试环境初始化注解
public void initSpring() { //环境初始化方法
context = new ClassPathXmlApplicationContext("applicationContext.xml");//初始上下文
}
@Test //注解测试方法
public void testMethod1() { //测试方法
HelloBean bean1 = (HelloBean)context.getBean("bean1"); //从容器获取bean实例
}
}
public class JUnitSpring1Test { //测试类
private static ApplicationContext context; //静态应用上下文
@BeforeClass //类层级环境初始化注解
public static void initSpring() { //类层级环境初始化
context = new ClassPathXmlApplicationContext("applicationContext.xml");//上下文
}
@Test //注解测试方法
public void testMethod() { //测试方法
System.out.println("JUnitSpring1Test, applicationContext=" + context.toString());
}
}
<dependency>
<groupId>org.springframework</groupId> <!—组名
<artifactId>spring-test</artifactId> <!—组件名
<version>5.0.8.RELEASE</version> <!—版本
</dependency>
//代码方式的bean配置和注册
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); //bean工厂
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();//Bean定义
beanDefinition.setBeanClass(User.class); //设置bean定义类
beanDefinition.getPropertyValues().add("name", "${name}") //设置bean属性;使用占位符
bf.registerBeanDefinition("user", beanDefinition); //注册bean
//模拟环境并设置属性后,对bean中的占位符进行替换
PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
pc.setEnvironment(new MockEnvironment().withProperty("name", "Oscar"));//设置环境属性
pc.postProcessBeanFactory(bf); //替换占位符
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"> <!—JNDI属性定义
<value>java:comp/env/jdbc/mydatasource</value>
</property>
</bean>
public void initForTest() throws IllegalStateException, NamingException { //模拟JNDI方法
DriverManagerDataSource ds = new DriverManagerDataSource(); //创建数据源
ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); //驱动类设置
ds.setUrl("jdbc:mysql://localhost:3306/ssmi?serverTimezone=UTC");//数据源url
ds.setUsername("root"); //用户名
ds.setPassword("123456"); //密码
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("java:comp/env/jdbc/mydatasource", ds); //绑定数据源
builder.activate();//激活
}
public class Foo { //组件类
@Autowired //自动装载注解
private String name; //字符串变量
@PostConstruct //组件初始化注解
private void onInit(){ //组件初始化回调方法
System.out.println("onInit... " + name);
}
@PreDestroy //组件销毁注解
private void onDestroy(){ //组件销毁方法回调
System.out.println("onDestroy... " + name);
}
}
@Test //测试方法注解
public void test () { //测试方法
Foo foo = new Foo(); //组件创建
ReflectionTestUtils.setField(foo, "name", "Oscar"); //设置私有变量的值
ReflectionTestUtils.invokeMethod(foo, "onInit"); //调用组件初始化方法
ReflectionTestUtils.invokeMethod(foo, "onDestroy"); //调用组件销毁方法
}
-
在测试类上使用@RunWith注解测试运行器。
-
使用@ContextConfiguration注解指定Spring的配置(可以是XML配置文件,也可以是配置类)。
-
装载需要的Bean并完成JUnit标签@Test注解的测试方法。
@RunWith(SpringJUnit4ClassRunner.class) //运行器注解
@ContextConfiguration(locations="classpath:cn/osxm/ssmi/chp6/applicationContext.xml")//配置
public class SpringTest { //测试类
@Autowired //自动装载注解
private HelloService helloService;
@Test //测试方法注解
public void hello() { //测试方法
helloService.sayHello();
}
}
-
TestContextManager,测试上下文管理器类,在每次测试时都会创建。提供对测试上下文(TestContext) 实例的管理,还负责测试过程中更新TestContext的状态并代理到TestExecutionListener,用来监测测试的执行,在测试执行点向每个注册的TestExecutionListener发送信号事件。
-
TestContext,测试上下文类,封装测试执行的上下文。提供访问容器和应用上下文的能力,并对applicationContext进行缓存。
-
TestExecutionListener,测试执行监听器,与TestContextManager发布的测试事件进行交互,这个监听器就是注册到TestContextManager上的。提供依赖注入、事务管理等能力。
-
ContextLoader:负责根据配置加载 Spring 的 Bean 定义,以构建 applicationContext 实例对象。ContextLoader是Spring 2.5中引入的一个策略接口,用于为Spring TestContext Framework管理的集成测试加载ApplicationContext。
-
SmartContextLoader:Spring 3.1中引入的ContextLoader接口的扩展。可以选择处理资源位置,带注释的类或上下文初始值设定项。支持按照profile加载。
-
TransactionalTestExecutionListener:事务测试执行监听器,用于对事务进行管理,负责解析@Transaction、@NotTransactional 以及 @Rollback 等事务注解的注解。@Transaction 注解让测试方法工作于事务环境中。可以使用 @Rollback(false) 让测试方法返回前提交事务。而@NotTransactional 注解则可以让测试方法不工作于事务环境中。此外,还可以使用类或方法级别的 @TransactionConfiguration 注解改变事务管理策略。
-
DependencyInjectionTestExecutionListener:依赖注入测试执行监听器,该监听器提供了自动注入的功能,它负责解析测试用例类中的@Autowried注解并完成依赖对象自动注入。
-
DirtiesContextTestExecutionListener:脏上下文测试执行监听器,一般情况下测试方法并不会对Spring容器上下文造成破坏(比如改变Bean的配置信息等),如果某个测试方法确实会破坏Spring容器上下文,可以显式地为该测试方法添加 @DirtiesContext注解,以便Spring TestContext在测试该方法后刷新Spring容器的上下文,DirtiesContextTestExecutionListener监听器的工作就是解析 @DirtiesContext注解。
@Retention(RetentionPolicy.RUNTIME) //运行时注解
@Target(ElementType.METHOD) //注解使用在方法上
@Inherited //允许继承
@Documented //注解包含在Java Doc中
public @interface MyLogTestAnno { //注解定义
public String logFileName();
}
public class MyLogTestExecutionListener implements TestExecutionListener {
public void prepareTestInstance(TestContext testContext) throws Exception {//准备测试实例
}
public void beforeTestClass(TestContext testContext) throws Exception {//测试类之前执行
System.out.println("测试类之前执行");
}
public void afterTestClass(TestContext testContext) throws Exception { //测试类之后执行
System.out.println("测试类之后执行");
}
public void beforeTestMethod(TestContext testContext) throws Exception {//测试方法之前执行
System.out.println("测试方法之前执行");
MyLogTestAnno myLogTest = testContext.getTestMethod().getAnnotation(MyLogTestAnno.class); //获取注解
if (myLogTest == null) {
return;
}
String logfile = myLogTest.logFileName();
System.out.println("注解参入的参数"+logfile);
}
public void afterTestMethod(TestContext testContext) throws Exception {//测试方法之后执行
System.out.println("测试方法之后执行");
}
}
@RunWith(SpringJUnit4ClassRunner.class) //测试运行器注解
@ContextConfiguration(locations = { "classpath:cn/osxm/ssmi/chp6/applicationContext.xml" })
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class, MyLogTestExecutionListener.class,}) //测试执行监听器注解
public class MyExecutionListenerTest{ //测试类
@Autowired //自动装载注解
private UserController userController;
@Test //测试方法注解
@MyLogTestAnno(logFileName = "testLog.txt") //自定义注解使用,logFileName赋值
public void test() {
}
}
-
在测试类中使用依赖注入。
-
测试类的自动化事务管理。
-
使用各种注解,提高开发效率和代码简洁性。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论