测试AWS SNS Util类

时间:2019-05-13 11:42:32

标签: java unit-testing junit mockito

我正在尝试为此util类编写单元测试。(我在编写单元测试方面经验不是很丰富)我想测试以下内容:

  1. 它会为提供空值而引发NPE。
  2. 一个非空的AmazonSNS因此被初始化。

    @Singleton @ slf4j 公共类SNSClientUtil {

    @Inject
    private MadonnaConfig madonnaConfig;
    
    public AmazonSNS getSNSClient(String snsTopicArn) {
        return AmazonSNSClientBuilder.standard()
                .withCredentials(new OdinAWSCredentialsProvider(madonnaConfig.getSnsOdinMaterialSet()))
                .withRegion(Regions.fromName(getRegionNameFromARN(snsTopicArn)))
                .build();
    }
    
    private String getRegionNameFromARN(String arn) {
        String[] tokens = arn.split(":");
        return tokens[3];
    }
    

    }

我正在使用Mockito。这是我的测试课:

@RunWith(MockitoJUnitRunner.class)
public class SNSClientUtilTest {

    @Mock
    SNSClientUtil snsClientUtil;

    private String topicARN = "arn:aws:sns:ap-south-1:123456789:Notifications-Devo";
    private String regionName = "ap-south-1";

    @Before
    public void setup() {

    }

    @Test(expected = NullPointerException.class)
    public void testNullTopicARN() {
        snsClientUtil.getSNSClient(null);
    }

    @Test
    public void testSNSClientCreation() {
        AmazonSNSClientBuilder builder = Mockito.mock(AmazonSNSClientBuilder.class, Mockito.RETURNS_DEEP_STUBS);
        Mockito.when(builder.build()).thenReturn(Mockito.any(AmazonSNS.class));
        AmazonSNS sns = snsClientUtil.getSNSClient(topicARN);
        Assert.assertNotNull(sns);
    }
}

空指针测试失败。因此,我尝试通过模拟AmazonSNSClientBuilder来测试非null SNS客户端的创建。但这给出了以下例外:

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class com.amazonaws.services.sns.AmazonSNSClientBuilder
Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types

at com.amazon.madonnaservice.test.utils.SNSClientUtilTest.testRegionNameFromARN(SNSClientUtilTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(Unknown Source)
at org.mockito.runners.MockitoJUnitRunner.run(Unknown Source)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

1 个答案:

答案 0 :(得分:1)

Mockito给您错误

  

org.mockito.exceptions.base.MockitoException:无法模拟/间谍类   com.amazonaws.services.sns.AmazonSNSClientBuilder Mockito无法   模拟/间谍以下:     -期末课程     -匿名班     -基本类型

public final class AmazonSNSClientBuilder是最终的 https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/sns/AmazonSNSClientBuilder.html

因此您的模仿设置为无效。也许您可以使用来自亚马逊的另一个可以模拟的api,否则您需要引入一个包装您可以在单元测试中模拟的客户端的层。