如何模拟 com.google.inject.Binder 和 org.yaml.snakeyaml.Yaml?
How to mock com.google.inject.Binder and org.yaml.snakeyaml.Yaml?
根据我的要求,我必须将我的代码覆盖率提高到 80% 我有些达到了 71% 但 30% 的代码是在 main class 和 YAML 配置加载器 class.谁能告诉我如何为以下方法编写测试用例:-
private String configFilePath;
private Optional<ItemMasterFileProcessorConfiguration> config;
public ItemMasterFileProcessorTaskModule(String configFilePath)
{
this.configFilePath = configFilePath;
config = Optional.empty();
}
@Override
public void configure(Binder binder)
{
binder.bind(<Interface>.class).to(<ImplClass>.class);
binder.bind(<Interface>.class).to(<ImplClass>.class);
}
@Provides
public ItemMasterFileProcessorConfiguration getConfig() throws ItemMasterFileProcessorException
{
Yaml yaml = new Yaml();
try
{
if (!config.isPresent())
{
ItemMasterFileProcessorConfiguration writerConfig = yaml.loadAs(ItemMasterFileProcessorConfiguration.class.getClassLoader().getResourceAsStream(configFilePath), ItemMasterFileProcessorConfiguration.class);
config = Optional.of(writerConfig);
}
/*
* Sonar is wanting is to add isPresent() before this But if I do
* Sonar complains that it will always evaluate to TRUE
*/
return config.get();
}
catch (Exception e)
{
throw new ItemMasterFileProcessorException("Config is NULL while initializing configuration for config path : {}", e);
}
}
如果您创建一个具有有效配置的临时文件,我在测试您的 class 时没有发现任何问题。您可以使用 JUnit 中的 TemporaryFolder
创建临时文件和一些 IOUtils 以 yaml 格式将配置写入文件。
以下示例来自官方org.junit.rules.TemporaryFolder
javadoc
public static class HasTempFolder {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testUsingTempFolder() throws IOException {
File createdFile= folder.newFile("myfile.txt");
File createdFolder= folder.newFolder("subfolder");
// ...
}
}
根据我的要求,我必须将我的代码覆盖率提高到 80% 我有些达到了 71% 但 30% 的代码是在 main class 和 YAML 配置加载器 class.谁能告诉我如何为以下方法编写测试用例:-
private String configFilePath;
private Optional<ItemMasterFileProcessorConfiguration> config;
public ItemMasterFileProcessorTaskModule(String configFilePath)
{
this.configFilePath = configFilePath;
config = Optional.empty();
}
@Override
public void configure(Binder binder)
{
binder.bind(<Interface>.class).to(<ImplClass>.class);
binder.bind(<Interface>.class).to(<ImplClass>.class);
}
@Provides
public ItemMasterFileProcessorConfiguration getConfig() throws ItemMasterFileProcessorException
{
Yaml yaml = new Yaml();
try
{
if (!config.isPresent())
{
ItemMasterFileProcessorConfiguration writerConfig = yaml.loadAs(ItemMasterFileProcessorConfiguration.class.getClassLoader().getResourceAsStream(configFilePath), ItemMasterFileProcessorConfiguration.class);
config = Optional.of(writerConfig);
}
/*
* Sonar is wanting is to add isPresent() before this But if I do
* Sonar complains that it will always evaluate to TRUE
*/
return config.get();
}
catch (Exception e)
{
throw new ItemMasterFileProcessorException("Config is NULL while initializing configuration for config path : {}", e);
}
}
如果您创建一个具有有效配置的临时文件,我在测试您的 class 时没有发现任何问题。您可以使用 JUnit 中的 TemporaryFolder
创建临时文件和一些 IOUtils 以 yaml 格式将配置写入文件。
以下示例来自官方org.junit.rules.TemporaryFolder
javadoc
public static class HasTempFolder {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testUsingTempFolder() throws IOException {
File createdFile= folder.newFile("myfile.txt");
File createdFolder= folder.newFolder("subfolder");
// ...
}
}