zoukankan      html  css  js  c++  java
  • Spring IOC 方式结合TESTGN测试用例,测试简单java的命令模式

    java命令模式: 可以命令("请求")封装成一个对象,一个命令对象通过在特定的接收着上绑定一组动作来封装一个请求.命令对象直接把执行动作和接收者包进对象中,只对外暴露出执行方法的的接口,从而简单的达到动作的请求者和动作的执行者解耦的目的.

    下面用一个简单的实例测试简单的命令模式,同时采用spring ioc的方式+testNG的测试方式,实现遥控器的遥控开灯的命令模式实现和测试.

    1.建立命令接口,包含执行方法,命令对象必须要实现的,执行方法就是暴露的接口方法,提供于接收者调用.

    public interface Command {
        void execute();
    }

    2.建立命令对象,实现命令接口,同时要包含动作执行者的引用.而且要用注解@Compent 方便spring的扫描加载程bean @Resource注入灯的实例

    复制代码
    @Component
    public class LightOnCommand implements Command {
    
        @Resource
        private Light light;
    
        @Override
        public void execute() {
            light.lightOn();
        }
    
    }
    复制代码

    3.建立灯的实现类,接受者,实际的打开灯对象

    复制代码
    @Component
    public class Light {
    public void lightOn() { System.out.println("开灯!!"); } }
    复制代码

    4.建立遥控器的类,发出打开灯的命令,同时引入命令对象,buttonPress方法,按下按钮,打开灯.

    复制代码
    @Component
    public class RemoteControl {
    
        @Resource
        private Command lightOnCommand;
    
        public void buttonPress() {
            lightOnCommand.execute();
        }
    
    }
    复制代码

    5.ok 现在一个简单的命令模式就建立完毕,下面使用TESTNG的基于IOC的方式 测试上面的遥控器的命令模式代码的正确:首先,建立spring-test.xml的资源文件,

    方便定义需要扫描的路径,加载我们注解生成的bean.定义如下:

    复制代码
    <?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  
        http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.suning.soa.lilin.test"></context:component-scan>
    </beans>
    复制代码

    6.建立BaseTest的测试类:主要是为了继承AbstractTestNGSpringContextTests,来启动SpringContext,方便其他的测试类集成BaseTest使用

    @ContextConfiguration(locations = { "classpath:/conf/spring/spring-test.xml" }) 主要是指定Spring的ContextLoader需要加载context文件.

    @ContextConfiguration(locations = { "classpath:/conf/spring/spring-test.xml" })
    public abstract class BaseTest extends AbstractTestNGSpringContextTests {
    
    }

    7.建立主要的遥控器测试类,RemoteControlTest:

    @Test 是TESTNG的开启测试的方法.注入需要测试的遥控器的对象,测试简单的命令模式.
    复制代码
    public class RemoteControlTest extends BaseTest {
    
        @Resource
        RemoteControl remote;
    
        @Test
        public void test() {
            remote.buttonPress();
        }
    
    }
    复制代码

    最后,运行TESTGN的测试方法,可以得到正确的开灯的结果展示.so 一个简单的遥控器开灯的命令模式的测试实例总结完毕.

  • 相关阅读:
    16. 3Sum Closest
    17. Letter Combinations of a Phone Number
    20. Valid Parentheses
    77. Combinations
    80. Remove Duplicates from Sorted Array II
    82. Remove Duplicates from Sorted List II
    88. Merge Sorted Array
    257. Binary Tree Paths
    225. Implement Stack using Queues
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/wsy0202/p/11236798.html
Copyright © 2011-2022 走看看