I'm writing simple app which retrieves contacts from one text file, messages from another text file and then send message via gateway using retrieved data.
(我正在编写一个简单的应用程序,该应用程序从一个文本文件中检索联系人,从另一个文本文件中检索消息,然后使用检索到的数据通过网关发送消息。)
I want to carry out tests for this app using Mockito. (我想使用Mockito对此应用程序进行测试。)
I created class ContactDetailRetriever
which contains two methods: retrieveContactDetails()
and mapOfContact()
. (我创建了类ContactDetailRetriever
,该类包含两个方法: retrieveContactDetails()
和mapOfContact()
。)
public class ContactDetailRetriever implements AutoCloseable {
private LinkedHashMap<String, String> contacts = new LinkedHashMap<>();
private ArrayList<String> arrOfContacts = new ArrayList<>();
private final String FILE_NAME;
public ContactDetailRetriever() {
this.FILE_NAME = "contacts.txt";
}
public ContactDetailRetriever(String fileName) {
this.FILE_NAME = fileName;
}
public ArrayList<String> retrieveContactDetails() {
try(BufferedReader reader = new BufferedReader(new FileReader(new File(FILE_NAME)))) {
String line;
while((line = reader.readLine()) != null) {
arrOfContacts.add(line);
}
}
catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return arrOfContacts;
}
public LinkedHashMap<String, String> mapOfContact() {
//Here I want to use method
//retrieveContactDetails();
String key = "";
String value = "";
int lineNo;
for (lineNo = 0; lineNo < arrOfContacts.size(); lineNo++) {
if(lineNo % 2 == 0) {
key = arrOfContacts.get(lineNo);
}
if(lineNo % 2 == 1) {
value = arrOfContacts.get(lineNo);
}
contacts.put(key, value);
}
return contacts;
}
What I wanted to do is to use retrieveContactDetails()
inside mapOfContact()
which works just as I wanted but I have a problem with testing usage of retrieveContactDetails()
.
(我想要做的是在mapOfContact()
使用retrieveContactDetails()
,它的工作方式与我想要的一样,但是我在测试retrieveContactDetails()
使用方面存在问题。)
What I ended up with is refactoring the code in class MessageSender
which uses indirectly mapOfContact()
so retrieveContactDetails()
is not used inside mapOfContact()
anymore. (我结束了在重构中类代码MessageSender
间接使用mapOfContact()
所以retrieveContactDetails()
是不是内部使用mapOfContact()
了。)
public class MessageSender implements SendingTool {
private LinkedHashMap<String, String> contactsMap;
private FileInjector fileInjector;
public MessageSender() {
this.fileInjector = new FileInjector();
}
public MessageSender(FileInjector fileInjector) {
this.fileInjector = fileInjector;
}
public LinkedHashMap<String, String> retrieveContactsMap(String fileName) {
try(ContactDetailRetriever contactDetailRetriever = fileInjector.buildContactDetailRetriever(fileName)) {
//Two assignments instead of one
contactArr = contactDetailRetriever.retrieveContactDetails();
contactsMap = contactDetailRetriever.mapOfContact();
}
return contactsMap;
}
It still works OK but in my opinion it's not elegant way.
(它仍然可以正常工作,但我认为这不是一种优雅的方式。)
Also it means that I edit my code for tests which as far as I'm concerned should not be the case. (这也意味着我编辑我的测试代码,就我而言,情况并非如此。)
What is the best way to test if retrieveContactDetails()
is used inside of mapOfContact()
? (测试是否在mapOfContact()
使用了retrieveContactDetails()
的最佳方法是什么?)
One more thing which may be relevant is that ContactDetailRetriever
dependencies are injected to MessageSender
by another class:
(另一件事可能是相关的,另一个类将ContactDetailRetriever
依赖项注入到MessageSender
:)
public class FileInjector {
public ContactDetailRetriever buildContactDetailRetriever(String fileName) {
return new ContactDetailRetriever(fileName);
}
public ContactDetailRetriever buildContactDetailRetriever() {
return new ContactDetailRetriever();
}
}
(})
ask by novice translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…