Testing an AWS SNS Util Class



我正在尝试为此util类编写单元测试。

  1. 它在提供无效的Arn时抛出NPE。
  2. 因此,初始化了一个非零零amazonsn。

    @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)

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

因此,您的Mockito设置为无效。也许您可以使用可以模拟的Amazon的另一个API,否则您需要介绍一个可以在单元测试中模拟的客户端的图层。

相关内容

  • 没有找到相关文章

最新更新