平平庸庸

(原创方法)接口测试-内部接口外方

上一篇 / 下一篇  2010-12-09 12:33:12 / 个人分类:java

!1 内部接口外放

!2 what
这里,我们所谓的内部接口外放是将一个提供给同一个jvm进程内其他对象调用的“方法”导出给另外一个jvm进程使用。
!2 why
在运行环境下直接对这样的方法测试

!2 how


!3 获得现有运行环境的ioc容器

1、配置 web.xml

<listener>
<listener-class>com.sunmx.IniSpring</listener-class>
</listener>

2、IniSpring源码

package com.sunmx;

/**
*
* @author peter.sun
*/

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class IniSpring implements ServletContextListener {


private static WebApplicationContext springContext;

public IniSpring() {
super();
}

public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
}


public void contextDestroyed(ServletContextEvent event) {
}

public static ApplicationContext getApplicationContext() {
return springContext;
}


}


!3 导出内部方法
方法一 :http-invoker

1 配置 web-xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Service-config.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.service</url-pattern>
</servlet-mapping>


2 配置Service-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/some.service">serviceExporter</prop>
</props>
</property>
</bean>
<!--配置要发布的服务类-->
<bean id="helloService" class="com.sunmx.httpinvoker.HelloServiceImpl"/>

<!--用HttpInvokerServiceExporter 发布服务-->
<bean id="serviceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="helloService"/>
<property name="serviceInterface" value="com.sunmx.httpinvoker.IHelloService"/>
</bean>
</beans>

3 IHelloService源码

package com.sunmx.httpinvoker;

/**
*
* @author peter.sun
*/
public interface IHelloService {

//为了演示方便,只提供一个服务,你可以在添加你想发布的任何服务
public String doHelloService(String name);
}

4 HelloServiceImpl源码

package com.sunmx.httpinvoker;

import com.sunmx.*;


/**
*
* @author peter.sun
*/
public class HelloServiceImpl implements IHelloService {
SomeBeans sb1;
public String doHelloService(String name) {
sb1=(SomeBeans) IniSpring.getApplicationContext().getBean("someBeans");
sb1.printNumber();
return "Hello "+name + ",this is the result for your request";
}
}

注释:SomeBeans 是被测试的内部类。通过以上的配置和测试代码。服务端添加了一个http invoker形式的rpc供远程调用HelloServiceImpl。IHelloService是HelloServiceImpl实现的接口

5 fixutre实现

package com.sunmx.invokerclient;

/**
*
* @author peter.sun
*/
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InvokerClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"invoker-client.xml");
//在此引入了服务接口类,只有在客户端有了这个接口类,才能调用远程的服务。
IHelloService service =(IHelloService) context.getBean("helloServiceProxy");
String result = service.doHelloService("孙明星");
System.out.println(result);
}
}

package com.sunmx.invokerclient;

/**
*
* @author peter.sun
*/
public interface IHelloService {

//为了演示方便,只提供一个服务,你可以在添加你想发布的任何服务
//这个服务负责对前台传入的name说Hello
public String doHelloService(String name);
}


invoker-client.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!--
Document : invoker-client.xml.xml
Created on : 2010年7月8日, 下午1:55
Author : peter.sun
Description:
Purpose of the document follows.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 客户端能够使用下面的代理连接到远程服务 -->
<bean id="helloServiceProxy"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://127.0.0.1:8080/springtest/some.service"/>
<property name="serviceInterface"
value="com.sunmx.invokerclient.IHelloService"/>
</bean>
<!-- 注意serviceUrl的值,这是一个标准的http请求。如果在本机测试可以把IP地址改成localhost -->
</beans>

版权声明:原创作品,转载时请务必以超链接形式标明本文原始出处 、作者信息和本声明,否则将追究法律责任。 本文出自Lennon-孙明星的51Testing软件测试博客:http://www.51testing.com/?14711

TAG:

 

评分:0

我来说两句

Open Toolbar