简体   繁体   English

如何模拟 org.springframework.core.io.Resource object?

[英]How to mock org.springframework.core.io.Resource object?

In my service method, I have a dependency on org.springframework.core.io.Resource reference and I want to write a test case for one of service method with below code在我的服务方法中,我依赖于org.springframework.core.io.Resource参考,我想用下面的代码为服务方法之一编写一个测试用例

The code below is as follows:下面的代码如下:

@Service
public class EmailServiceImpl implements EmailService{

 @Value("classpath:email_template.html")
 private Resource emailTemplateResource;

 private String getEmailTemplate() {

        String template = null;
        try {
            template = new BufferedReader(new InputStreamReader(emailTemplateResource.getInputStream()))
                    .lines().collect(Collectors.joining("\n"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return template;
    }

}

Here, I'm not able to mock emailTemplateResource object in my test case method.在这里,我无法在我的测试用例方法中模拟emailTemplateResource object。 I tried with @Mock and @Spy but each time I'm getting NullPointerException on emailTemplateResource object.我尝试使用@Mock@Spy ,但每次我在emailTemplateResource object 上收到 NullPointerException。

So, what is the right way to mock this emailTemplateResource object?那么,模拟此emailTemplateResource object 的正确方法是什么?

You can do constructor injection in EmailServiceImpl .您可以在EmailServiceImpl中进行构造函数注入。 That way in your mock class when create EmailServiceImpl you can pass your own Resource object in the constructutor.这样,在您的模拟 class 创建EmailServiceImpl时,您可以在构造函数中传递您自己的Resource object 。

The overhead with this approach is that @InjectMocks is of no use as now you are creating your object on your own inside before hook of the test case.这种方法的开销是@InjectMocks没有用,因为现在您在测试用例挂钩before在自己的内部创建 object。

example:例子:

@RunWith(MockitoJUnitRunner.class)
public class EmailServiceImplTest{

    EmailServiceImpl  emailServiceImpl ;

    Resource emailTemplateResource;

       @Before
       public void before() {
            emailTemplateResource = //your impl here
           emailServiceImpl  = new EmailServiceImpl(resource);
       }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为 org.springframework.core.io.Resource 对象分配名称 - Assigning name to a org.springframework.core.io.Resource object 如何处理Swagger Codegen的org.springframework.core.io.Resource - How to handle org.springframework.core.io.Resource for Swagger Codegen 如何记录将返回ResponseEntity的表格规范 <Resource> 其中Resource是org.springframework.core.io.Resource - How to document swagger specification which returns ResponseEntity<Resource> where Resource is org.springframework.core.io.Resource java.lang.ClassNotFoundException: org.springframework.core.io.Resource - java.lang.ClassNotFoundException: org.springframework.core.io.Resource org.springframework.web.multipart.MultipartFile 和 org.springframework.core.io.Resource 之间的转换 - Conversion between org.springframework.web.multipart.MultipartFile and org.springframework.core.io.Resource 为什么JasperReports尝试加载org.springframework.core.io.Resource类? - Why does JasperReports try to load org.springframework.core.io.Resource class? ClassNotFoundException:IntelliJ IDEA在tomcat7上部署Maven Spring MVC Web项目时出现org.springframework.core.io.Resource - ClassNotFoundException: org.springframework.core.io.Resource when intellij idea deploy maven spring mvc web project on tomcat7 Spring MVC“找不到能够将java.lang.String类型转换为org.springframework.core.io.Resource类型的转换器” - Spring MVC “No converter found capable of converting from type java.lang.String to type org.springframework.core.io.Resource” java.lang.NoClassDefFoundError: org/springframework/core/io/Resource 异常 - java.lang.NoClassDefFoundError: org/springframework/core/io/Resource Exception spring org.springframework.core.io.UrlResource不支持https资源 - spring org.springframework.core.io.UrlResource not support https resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM