Java SparkPost的单元测试

Java SparkPost的单元测试,java,unit-testing,regression-testing,sparkpost,Java,Unit Testing,Regression Testing,Sparkpost,@服务 公共类SparkPostController{ package johnny.me.controller; import com.sparkpost.Client; import com.sparkpost.model.responses.Response; import com.sparkpost.exception.SparkPostException; import org.springframework.stereotype.Service; /** * Sparkpost

@服务 公共类SparkPostController{

package johnny.me.controller;

import com.sparkpost.Client;
import com.sparkpost.model.responses.Response;
import com.sparkpost.exception.SparkPostException;
import org.springframework.stereotype.Service;

/**
* Sparkpost class sends the email using
* the sendmessage methood
*/

这是我的sparkpost控制器类。正在尝试进行单元测试/模拟以发送消息。也称为回归测试。请非常具体地说,对单元测试来说是相当新的。让我的类在测试文件夹下生成。这就是我所得到的。

我是上面提到的sparkpost java库的作者/维护者

上面显示的用例是SparkPost库的“最简单”用法。对于像您这样的代码,人们通常使用您定义的“sendMessage(…)”函数创建接口。当单元测试使用spring(或测试)时,注入“stub”类的版本,而不是生产版本。但在生产或集成中加载上面显示的版本

如果您真的想加载SparkPost库,有几种方法可以针对第三方库进行测试。其中一种方法是使用JMock针对库进行测试。或者您可以在客户端具体实例上设置存根IRestConnection(这是一种更高级的解决方案)

根据您的需要,我认为上面的第一个建议是您的最佳选择,并且在您使用其他第三方库时也会非常常见

“App”目录中的文件是基本的示例使用/集成示例。请随时在此项目中添加一个问题,以添加演示应用单元测试:

“Lib”中的文件是实际的SparkPost库

您可以在此处找到现有的库单元测试,这可能有助于您了解它是如何在那里完成的。实际上,有一个针对“”的存根实现。这是一个示例,其中创建了存根连接,而不是默认的真正的RestConnection

我希望这有帮助

    String API_KEY = "API KEY";
         Client client = new Client(API_KEY);
         private static String from_email = "me@yahoo.com";
         private static String subject = "Time equation";
         private static String text = "";

    public Response sendMessage(String recipient, String message) throws SparkPostException {
         return client.sendMessage(from_email,recipient,subject,
                 text,message);
    }

}