小编mhe*_*eck的帖子

模拟 AWS 服务和 Lambda 最佳实践

我正在开发一个简单的 AWS lambda 函数,该函数由 DynamoDB Streams 事件触发,并且应该将除REMOVE事件之外的所有记录转发到 SQS 队列。该功能按预期工作,没有什么意外。

我想编写一个单元测试来测试在事件发生时不向 SQS 提交任何内容的行为DELETE。我首先使用aws-sdk-mock尝试过此操作。正如您在函数代码中看到的,我尝试通过在处理程序代码之外初始化 SQS 客户端来遵守 lambda 最佳实践。显然,这会阻止aws-sdk-mock模拟 SQS 服务(GitHub 上有一个与此相关的问题: https: //github.com/dwyl/aws-sdk-mock/issues/206)。

然后,我尝试使用jest来模拟 SQS ,这需要更多代码才能正确完成,但我最终遇到了同样的问题,需要将 SQS 的初始化放在处理程序函数中,这违反了 lambda 最佳实践。

如何为此函数编写单元测试,同时让 SQS client() 的初始化const sqs: SQS = new SQS()在处理程序之外进行?我是否以错误的方式嘲笑服务,或者是否需要更改处理程序的结构以使其更易于测试?

我知道这个 lambda 函数非常简单,单元测试可能是不必要的,但我必须用更复杂的逻辑编写更多的 lambda,我认为这个非常适合演示这个问题。

索引.ts

import {DynamoDBStreamEvent, DynamoDBStreamHandler} from "aws-lambda";
import SQS = require("aws-sdk/clients/sqs");
import DynamoDB = require("aws-sdk/clients/dynamodb");

const sqs: SQS = new SQS()

export const handleDynamoDbEvent: DynamoDBStreamHandler …
Run Code Online (Sandbox Code Playgroud)

amazon-web-services typescript jestjs aws-lambda ts-jest

5
推荐指数
1
解决办法
2986
查看次数

使用jpa和hibernate在spring中延迟加载实体

我正在使用Spring和JPA以及Hibernate.我有一些使用@Repositoy和一些Controller-Classes注释的DAO类.当我在我的控制器中调用其中一个dao的方法来加载一些实体时,我会返回实体,之后,我想得到一些存储在第一个加载实体的字段中的其他实体.但那时春天已经关闭了会话,懒惰的加载已不再可能.

我的db.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd" default-autowire="byName">

    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <bean id="placeholderConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="jpaVendorAdapter" />
    </bean> -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="true" />
                <property name="databasePlatform" value="${db.dialect}" />
            </bean>
        </property>     
    </bean> …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate lazy-loading spring-transactions

0
推荐指数
1
解决办法
2737
查看次数