正确地模拟一个返回javax.mail.Message的服务方法。

huangapple 未分类评论44阅读模式
英文:

Correctly mock a service method which returns a javax.mail.Message

问题

我有一个名为EmailScanner.java的类,其中有以下代码:

@Autowired
@Qualifier("customClient")
private EmailClient client; //EmailClient是一个接口

我正试图为上述类编写一个测试,并对客户端进行如下模拟。

    @InjectMocks
    private EmailScanner emailScanner;

    @Mock
    @Qualifier("customClient")
    private EmailClient client;

在客户端中,我有一个方法用于读取并返回电子邮件。 client.fetchEmail(folderName)

我已经编写了 Mockito.doReturn(getSampleMessage()).when(client).fetchEmail(Mockito.anyString());
getSampleMessage() 从磁盘中读取 .eml 文件并将其转换为邮件消息,但在运行时我得到了空消息。我是否漏掉了什么,或者在这里做错了什么?

英文:

I have an EmailScanner.java class which has

@Autowired
@Qualifier("customClient")
private EmailClient client; //EmailClient is interface

I am trying to write a test for above class and mocking the client as shown below.

    @InjectMocks
    private EmailScanner emailScanner;

    @Mock
    @Qualifier("customClient")
    private EmailClient client;

in the client, I have a method that reads and returns an Email. client.fetchEmail(folderName).

I have written Mockito.doReturn(getSampleMessage()).when(client).fetchEmail(Mockito.anyString());.
getSampleMessage() reads .eml file from disk and convert it into Mail message, on running i am getting null message. Is there anything i am missed on or doing wrong here?

huangapple
  • 本文由 发表于 2020年7月23日 22:03:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/63056152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定