Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final methods in TemplateEngine make it difficult to mock #314

Closed
whitneyh opened this issue Aug 29, 2014 · 5 comments
Closed

Final methods in TemplateEngine make it difficult to mock #314

whitneyh opened this issue Aug 29, 2014 · 5 comments

Comments

@whitneyh
Copy link

All the process(...) methods in TemplateEngine are marked as 'final'. This makes creating a Mockito mock object for it difficult. Please consider removing this keyword from methods in this class.

@danielfernandez
Copy link
Member

I understand your point. Thymeleaf v3 will actually make most of its structures implement interfaces so that they are easier to test or mock (e.g. ITemplateEngine).

@danielfernandez
Copy link
Member

This is already so in Thymelaf 3.0, including an ITemplateEngine interface for the TemplateEngine class.

Already in `3.0.0-SNAPSHOT´

@T3rm1
Copy link

T3rm1 commented Dec 10, 2015

Over one year later, nothing has changed, 3.0 isn't out and we are still not able to mock Thymeleaf with Mockito. Why didn't you change the code? It's really easy and fast to do...

@karollewandowski
Copy link

If it's a blocker for you, you can use PowerMockito to mock final methods.

@markot6
Copy link

markot6 commented Jun 24, 2016

A workaround is create a temporary interface and a related implementation:

public interface ITemplateEngine {
    String process(String template, IContext context);
}
@Component
public class TemplateEngineWrapper implements ITemplateEngine {
    @Autowired
    private SpringTemplateEngine templateEngine;

    @Override
    public String process(String templateName, IContext context) {
        return templateEngine.process(templateName, context);
    }

}

Then, in the tested component instead of:

@Autowired
private SpringTemplateEngine templateEngine;

use:

@Autowired
private ITemplateEngine templateEngine;

Now, you can easily mock(ITemplateEngine.class) and its process() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants