

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 可测试性和依赖关系注入
<a name="test"></a>

**Topics**
+ [Spring 集成](#test.spring)
+ [JUnit Integration](#test.junit)

框架设计为适当地控制反转 (IoC)。可以使用像 Spring 这样的容器配置和实例化活动和工作流程实施以及框架提供的工作线程和上下文对象。框架提供了与 Spring 框架的现成集成。此外，还为单元测试工作流程和活动实现提供了与 JUnit 的集成。

## Spring 集成
<a name="test.spring"></a>

利用 com.amazonaws.services.simpleworkflow.flow.spring 程序包中包含的类，可以在您的应用程序中轻松地使用 Spring 框架。其中包括自定义范围和 Spring 感知的活动和工作流程工作线程：`WorkflowScope`、`SpringWorkflowWorker` 和 `SpringActivityWorker`。利用这些类，您可以通过 Spring 完整配置工作流程和活动实施以及工作线程。

### WorkflowScope
<a name="test.workflowscope"></a>

`WorkflowScope` 是由框架提供的自定义 Spring 范围实施。此范围允许您在其生命周期被限定于决策任务范围的 Spring 容器中创建对象。每当工作线程收到新的决策任务时，将实例化此范围中的 bean。您应将此范围用于工作流程实施 bean 及其依赖的任何其他 bean。Spring 提供的单例和原型范围不应用于工作流程实施 bean，因为框架需要为每个决策任务创建一个新 bean。该操作失败将会导致意外行为。

以下示例显示 Spring 配置的代码段，它将注册 `WorkflowScope`，然后将其用于配置工作流程实施 bean 和活动客户端 bean。

```
<!-- register AWS Flow Framework for Java WorkflowScope -->
   <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
      <property name="scopes">
       <map>
         <entry key="workflow">
          <bean class="com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope" />
         </entry>
       </map>
      </property>
   </bean>

   <!-- activities client -->
   <bean id="activitiesClient" class="aws.flow.sample.MyActivitiesClientImpl" scope="workflow">
   </bean>

   <!-- workflow implementation -->
   <bean id="workflowImpl" class="aws.flow.sample.MyWorkflowImpl" scope="workflow">
      <property name="client" ref="activitiesClient"/>
      <aop:scoped-proxy proxy-target-class="false" />
   </bean>
```

配置行：需要 `<aop:scoped-proxy proxy-target-class="false" />` (在 `workflowImpl` bean 的配置中使用)，因为 `WorkflowScope` 不支持使用 CGLIB 作为代理。应将此配置用于 `WorkflowScope` 中连接到其他范围中的另一个 bean 的任何 bean。在此情况下，`workflowImpl` bean 需要连接到单例范围中的工作流程工作线程 bean (参见以下完整示例)。

您可以参阅 Spring 框架文档以详细了解如何使用自定义范围。

### Spring 感知的工作线程
<a name="test.springworkers"></a>

在使用 Spring 时，您应使用框架提供的 Spring 感知的工作线程类：`SpringWorkflowWorker` 和 `SpringActivityWorker`。可使用 Spring 将这些工作线程注入应用程序中，如下一个示例所示。Spring 感知的工作线程可实施 Spring 的 `SmartLifecycle` 接口，并且 (默认情况下) 在初始化 Spring 上下文时自动开始轮询任务。您可以通过将工作线程的 `disableAutoStartup` 属性设置为 `true` 来禁用此功能。

以下示例说明如何配置决策程序。此示例使用 `MyActivities` 和 `MyWorkflow` 接口 (此处未显示) 以及相应的实施 (`MyActivitiesImpl` 和 `MyWorkflowImpl`)。生成的客户端接口和实施为 `MyWorkflowClient`/`MyWorkflowClientImpl` 和 `MyActivitiesClient`/`MyActivitiesClientImpl` (此处也未显示)。

使用 Spring 的自动连接功能将活动客户端注入工作流程实施中。

```
public class MyWorkflowImpl implements MyWorkflow {
   @Autowired
   public MyActivitiesClient client;

   @Override
   public void start() {
      client.activity1();
   }
}
```

决策程序的 Spring 配置如下所示：

```
<?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:aop="http://www.springframework.org/schema/aop"
   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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!-- register custom workflow scope -->
   <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
      <property name="scopes">
       <map>
         <entry key="workflow">
          <bean class="com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope" />
         </entry>
       </map>
      </property>
   </bean>
   <context:annotation-config/>

   <bean id="accesskeys" class="com.amazonaws.auth.BasicAWSCredentials">
      <constructor-arg value="{AWS.Access.ID}"/>
      <constructor-arg value="{AWS.Secret.Key}"/>
   </bean>

   <bean id="clientConfiguration" class="com.amazonaws.ClientConfiguration">
      <property name="socketTimeout" value="70000" />
   </bean>

   <!-- Amazon SWF client -->
   <bean id="swfClient"
      class="com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflowClient">
      <constructor-arg ref="accesskeys" />
      <constructor-arg ref="clientConfiguration" />
      <property name="endpoint" value="{service.url}" />
   </bean>

   <!-- activities client -->
   <bean id="activitiesClient" class="aws.flow.sample.MyActivitiesClientImpl" scope="workflow">
   </bean>

   <!-- workflow implementation -->
   <bean id="workflowImpl" class="aws.flow.sample.MyWorkflowImpl" scope="workflow">
      <property name="client" ref="activitiesClient"/>
      <aop:scoped-proxy proxy-target-class="false" />
   </bean>

   <!-- workflow worker -->
   <bean id="workflowWorker"
      class="com.amazonaws.services.simpleworkflow.flow.spring.SpringWorkflowWorker">
      <constructor-arg ref="swfClient" />
      <constructor-arg value="domain1" />
      <constructor-arg value="tasklist1" />
      <property name="registerDomain" value="true" />
      <property name="domainRetentionPeriodInDays" value="1" />
      <property name="workflowImplementations">
         <list>
            <ref bean="workflowImpl" />
         </list>
      </property>
   </bean>
</beans>
```

由于在 `SpringWorkflowWorker` Spring 中已完全配置，并且在初始化 Spring 上下文时会自动开始轮询，因此决策者的宿主进程很简单：

```
public class WorkflowHost {
   public static void main(String[] args){
      ApplicationContext context
          = new FileSystemXmlApplicationContext("resources/spring/WorkflowHostBean.xml");
      System.out.println("Workflow worker started");
   }
}
```

同样，可以配置活动工作线程，如下所示：

```
<?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:aop="http://www.springframework.org/schema/aop"
   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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!-- register custom scope -->
   <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
      <property name="scopes">
         <map>
            <entry key="workflow">
               <bean
                  class="com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope" />
            </entry>
         </map>
      </property>
   </bean>

   <bean id="accesskeys" class="com.amazonaws.auth.BasicAWSCredentials">
      <constructor-arg value="{AWS.Access.ID}"/>
      <constructor-arg value="{AWS.Secret.Key}"/>
   </bean>

   <bean id="clientConfiguration" class="com.amazonaws.ClientConfiguration">
      <property name="socketTimeout" value="70000" />
   </bean>

   <!-- Amazon SWF client -->
   <bean id="swfClient"
      class="com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflowClient">
      <constructor-arg ref="accesskeys" />
      <constructor-arg ref="clientConfiguration" />
      <property name="endpoint" value="{service.url}" />
   </bean>

   <!-- activities impl -->
   <bean name="activitiesImpl" class="asadj.spring.test.MyActivitiesImpl">
   </bean>

   <!-- activity worker -->
   <bean id="activityWorker"
      class="com.amazonaws.services.simpleworkflow.flow.spring.SpringActivityWorker">
      <constructor-arg ref="swfClient" />
      <constructor-arg value="domain1" />
      <constructor-arg value="tasklist1" />
      <property name="registerDomain" value="true" />
      <property name="domainRetentionPeriodInDays" value="1" />
      <property name="activitiesImplementations">
         <list>
            <ref bean="activitiesImpl" />
         </list>
      </property>
   </bean>
</beans>
```

活动工作线程宿主进程与决策程序的类似：

```
public class ActivityHost {
   public static void main(String[] args) {
      ApplicationContext context = new FileSystemXmlApplicationContext(
      "resources/spring/ActivityHostBean.xml");
      System.out.println("Activity worker started");
   }
}
```

### 注入决策上下文
<a name="test.injectdecision"></a>

如果工作流程实施依赖于上下文对象，则也可通过 Spring 轻松注入它们。框架将在 Spring 容器中自动注册与上下文相关的 bean。例如，在以下代码段中，已自动连接各种上下文对象。不需要上下文对象的其他 Spring 配置。

```
public class MyWorkflowImpl implements MyWorkflow {
   @Autowired
   public MyActivitiesClient client;
   @Autowired
   public WorkflowClock clock;
   @Autowired
   public DecisionContext dcContext;
   @Autowired
   public GenericActivityClient activityClient;
   @Autowired
   public GenericWorkflowClient workflowClient;
   @Autowired
   public WorkflowContext wfContext;
   @Override
   public void start() {
      client.activity1();
   }
}
```

若要通过 Spring XML 配置来配置工作流程实施中的上下文对象，可使用 com.amazonaws.services.simpleworkflow.flow.spring 程序包中的 `WorkflowScopeBeanNames` 类中声明的 bean 名称。例如：

```
<!-- workflow implementation -->
<bean id="workflowImpl" class="asadj.spring.test.MyWorkflowImpl" scope="workflow">
   <property name="client" ref="activitiesClient"/>
   <property name="clock" ref="workflowClock"/>
   <property name="activityClient" ref="genericActivityClient"/>
   <property name="dcContext" ref="decisionContext"/>
   <property name="workflowClient" ref="genericWorkflowClient"/>
   <property name="wfContext" ref="workflowContext"/>
   <aop:scoped-proxy proxy-target-class="false" />
</bean>
```

或者，您可以在工作流程实施 bean 中注入 `DecisionContextProvider`，并使用它创建上下文。如果您要提供提供程序和上下文的自定义实施，这会很有用。

### 在活动中注入资源
<a name="test.injectresource"></a>

您可以使用控制反转 (IoC) 容器实例化和配置活动实施，并通过将数据库连接等资源声明为活动实施类的属性来轻松注入这些资源。此类资源通常被限定为单例范围。请注意，活动实施由多个线程上的活动工作线程调用。因此，必须同步对共享资源的访问。

## JUnit Integration
<a name="test.junit"></a>

该框架提供了上下文对象（例如测试时钟）的 JUnit 扩展和测试实现，您可以使用它们来编写和运行单元测试 JUnit。利用这些扩展，您可以本地内联测试工作流程实施。

### 编写简单单元测试
<a name="test.junit.simple"></a>

 要编写工作流程的测试，请使用 com.amazonaws.services.simpleworkflow.flow.junit 程序包中的 `WorkflowTest` 类。该类是特定于框架的 JUnit `MethodRule`实现，可在本地运行您的工作流程代码，以内联方式调用活动，而不是通过 Amazon SWF。这使您能够根据需要灵活地频繁运行测试，而不会产生任何费用。

要使用此类，只需声明类型 `WorkflowTest` 的字段并使用 `@Rule` 注释标记它。在运行您的测试之前，请创建新的 `WorkflowTest` 对象并向其添加活动和工作流程实施。之后，您可以使用生成的工作流程客户端工厂来创建客户端并启动工作流程执行。该框架还提供了一个自定义 JUnit 运行器`FlowBlockJUnit4ClassRunner`，您必须将其用于工作流程测试。例如：

```
@RunWith(FlowBlockJUnit4ClassRunner.class)
public class BookingWorkflowTest {

    @Rule
    public WorkflowTest workflowTest = new WorkflowTest();

    List<String> trace;

    private BookingWorkflowClientFactory workflowFactory
         = new BookingWorkflowClientFactoryImpl();

    @Before
    public void setUp() throws Exception {
        trace = new ArrayList<String>();
        // Register activity implementation to be used during test run
        BookingActivities activities = new BookingActivitiesImpl(trace);
        workflowTest.addActivitiesImplementation(activities);
        workflowTest.addWorkflowImplementationType(BookingWorkflowImpl.class);
    }

    @After
    public void tearDown() throws Exception {
        trace = null;
    }

    @Test
    public void testReserveBoth() {
        BookingWorkflowClient workflow = workflowFactory.getClient();
        Promise<Void> booked = workflow.makeBooking(123, 345, true, true);
        List<String> expected = new ArrayList<String>();
        expected.add("reserveCar-123");
        expected.add("reserveAirline-123");
        expected.add("sendConfirmation-345");
        AsyncAssert.assertEqualsWaitFor("invalid booking", expected, trace, booked);
    }
}
```

您还可以为添加到 `WorkflowTest` 的每个活动实施指定一个单独的任务列表。例如，如果您有一个计划宿主特定的任务列表中的活动的工作流程实施，则可在每个宿主的任务列表中注册活动：

```
for (int i = 0; i < 10; i++) {
    String hostname = "host" + i;
    workflowTest.addActivitiesImplementation(hostname,
                                             new ImageProcessingActivities(hostname));
}
```

请注意，`@Test` 中的代码是异步的。因此，您应使用异步工作流程客户端来启动执行。为了验证您的测试结果，还提供了一个 `AsyncAssert` 帮助类。此类使您能够在验证结果前等待 Promise 就绪。在此示例中，我们等待工作流程执行的结果就绪，然后再验证测试输出。

如果您使用的是 Spring，则可以使用 `SpringWorkflowTest` 类而不是 `WorkflowTest` 类。`SpringWorkflowTest` 提供了配置属性，您可以使用它们通过 Spring 配置轻松地配置活动和工作流程实施。就像 Spring 感知的工作线程一样，您应使用 `WorkflowScope` 配置工作流程实施 bean。这将确保为每个决策任务创建一个新的工作流程实施 bean。请务必将这些 bean 的作用域代理设置 proxy-target-class设置为。`false`有关更多详细信息，请参阅“Spring 集成”部分。可以更改“Spring 集成”部分中显示的 Spring 配置示例来使用 `SpringWorkflowTest` 测试工作流程：

```
<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans ht
tp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframe
work.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <!-- register custom workflow scope -->
  <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
      <map>
        <entry key="workflow">
          <bean
            class="com.amazonaws.services.simpleworkflow.flow.spring.WorkflowScope" />
        </entry>
      </map>
    </property>
  </bean>
  <context:annotation-config />
  <bean id="accesskeys" class="com.amazonaws.auth.BasicAWSCredentials">
    <constructor-arg value="{AWS.Access.ID}" />
    <constructor-arg value="{AWS.Secret.Key}" />
  </bean>
  <bean id="clientConfiguration" class="com.amazonaws.ClientConfiguration">
    <property name="socketTimeout" value="70000" />
  </bean>

  <!-- Amazon SWF client -->
  <bean id="swfClient"
    class="com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflowClient">
    <constructor-arg ref="accesskeys" />
    <constructor-arg ref="clientConfiguration" />
    <property name="endpoint" value="{service.url}" />
  </bean>

  <!-- activities client -->
  <bean id="activitiesClient" class="aws.flow.sample.MyActivitiesClientImpl"
    scope="workflow">
  </bean>

  <!-- workflow implementation -->
  <bean id="workflowImpl" class="aws.flow.sample.MyWorkflowImpl"
    scope="workflow">
    <property name="client" ref="activitiesClient" />
    <aop:scoped-proxy proxy-target-class="false" />
  </bean>

  <!-- WorkflowTest -->
  <bean id="workflowTest"
    class="com.amazonaws.services.simpleworkflow.flow.junit.spring.SpringWorkflowTest">
    <property name="workflowImplementations">
      <list>
        <ref bean="workflowImpl" />
      </list>
    </property>
    <property name="taskListActivitiesImplementationMap">
      <map>
        <entry>
          <key>
            <value>list1</value>
          </key>
          <ref bean="activitiesImplHost1" />
        </entry>
      </map>
    </property>
  </bean>
</beans>
```

#### 模拟活动实施
<a name="test.junit.mockactivity"></a>

您可以在测试期间使用真实活动实施，但如果您要仅对工作流程逻辑进行单元测试，则应模拟活动。可以通过向 `WorkflowTest` 类提供活动接口的模拟实施来做到这一点。例如：

```
@RunWith(FlowBlockJUnit4ClassRunner.class)
public class BookingWorkflowTest {

    @Rule
    public WorkflowTest workflowTest = new WorkflowTest();

    List<String> trace;

    private BookingWorkflowClientFactory workflowFactory
         = new BookingWorkflowClientFactoryImpl();

    @Before
    public void setUp() throws Exception {
        trace = new ArrayList<String>();
        // Create and register mock activity implementation to be used during test run
        BookingActivities activities = new BookingActivities() {

            @Override
            public void sendConfirmationActivity(int customerId) {
                trace.add("sendConfirmation-" + customerId);
            }

            @Override
            public void reserveCar(int requestId) {
                trace.add("reserveCar-" + requestId);
            }

            @Override
            public void reserveAirline(int requestId) {
                trace.add("reserveAirline-" + requestId);
            }
        };
        workflowTest.addActivitiesImplementation(activities);
        workflowTest.addWorkflowImplementationType(BookingWorkflowImpl.class);
    }

    @After
    public void tearDown() throws Exception {
        trace = null;
    }

    @Test
    public void testReserveBoth() {
        BookingWorkflowClient workflow = workflowFactory.getClient();
        Promise<Void> booked = workflow.makeBooking(123, 345, true, true);
        List<String> expected = new ArrayList<String>();
        expected.add("reserveCar-123");
        expected.add("reserveAirline-123");
        expected.add("sendConfirmation-345");
        AsyncAssert.assertEqualsWaitFor("invalid booking", expected, trace, booked);
    }
}
```

或者，您可以提供活动客户端的模拟实施并将其注入您的工作流程实施中。

### 测试上下文对象
<a name="test.junit.objects"></a>

如果您的工作流实现依赖于框架上下文对象（例如 `DecisionContext`），那么您无需执行任何特殊操作即可测试此类工作流。在通过 `WorkflowTest` 运行测试时，它会自动注入测试上下文对象。当您的工作流实现访问上下文对象时（例如，使用 `DecisionContextProviderImpl`），它将获得测试实现。您可以在测试代码 (`@Test` 方法) 中操作这些测试上下文对象，以创建有意义的测试用例。例如，如果您的工作流程创建了一个计时器，则可通过调用 `WorkflowTest` 类的 `clockAdvanceSeconds` 方法来使时钟及时向前移动，从而触发计时器。您也可以加快时钟，以使计时器的触发时间早于通常通过使用 `WorkflowTest` 的 `ClockAccelerationCoefficient` 属性实现的触发时间。例如，如果您的工作流程创建了一个 1 小时的计时器，则可将 `ClockAccelerationCoefficient` 设置为 60 来使计时器在 1 分钟内触发。默认情况下，将 `ClockAccelerationCoefficient` 设置为 1。

有关 com.amazonaws.services.simpleworkflow.flow.test 和 com.amazonaws.services.simpleworkflow.flow.junit 程序包的更多详细信息，请参阅 适用于 Java 的 AWS SDK 文档。