我想为我的保存和加载方法编写一个单元测试,但这些方法使用文件选择器,我不知道如何模拟单元测试中使用的那些。
我的加载和保存方法如下
显然我需要添加更多细节,但我已经简明扼要地总结了我的情况。
public static String load() throws IOException {
FileChooser fc = new FileChooser();
// Define extension filters
FileChooser.ExtensionFilter txtFilter = new FileChooser.ExtensionFilter("Text Files (*.txt)", "*.txt");
FileChooser.ExtensionFilter odtFilter = new FileChooser.ExtensionFilter("ODT Files (*.odt)", "*.odt");
fc.getExtensionFilters().addAll(txtFilter, odtFilter);
File file = fc.showOpenDialog(null);
// Chose an appropriate method to load the file contents
if (file != null) {
FileChooser.ExtensionFilter selectedFilter = fc.getSelectedExtensionFilter();
if (selectedFilter == txtFilter) {
return readTxt(file);
} else if (selectedFilter == odtFilter) {
return readOdt(file);
} else {
return …Run Code Online (Sandbox Code Playgroud) 我已经在我的程序开始时声明了一本词典
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Dictionary<string, int> dictionary = new Dictionary<string, int>();
}
Run Code Online (Sandbox Code Playgroud)
我有一个函数,使用它发送的字符串填充字典
public IDictionary<string, int> SortTextIntoDictionary(string text)
{
text = text.Replace(",", ""); //Just cleaning up a bit
text = text.Replace(".", ""); //Just cleaning up a bit
text = text.Replace(Environment.NewLine, " ");
string[] arr = text.Split(' '); //Create an array of words
foreach (string word in arr) //let's loop over the words
{
if (dictionary.ContainsKey(word)) //if it's in the dictionary
dictionary[word] = …Run Code Online (Sandbox Code Playgroud)