thymeleaf-testing icon indicating copy to clipboard operation
thymeleaf-testing copied to clipboard

How do I bring the context for processing the template to the executor

Open zian92 opened this issue 2 years ago • 1 comments

Hey,

I currently struggle with creating the variables hat should be used to fill the templates from the surrounding java JUnit code. I know I can do it from within the thtest-file but that is not a viable solution as the context contains multiple aggregated objects. Is there an intended way to do so? If not, i would like to request it.

I already asked this on stackoverflow some time ago but did no receive any response or solution to it.

zian92 avatar Jan 26 '23 12:01 zian92

I found this solution:

`public class WebProcessingContextBuilderWithJavaVariables extends WebProcessingContextBuilder {

private final Map<String, Object> vars;

public WebProcessingContextBuilderWithJavaVariables(Map<String, Object> variables) {
	vars = variables;
}

@Override
protected void doAdditionalVariableProcessing(
		final ITest test,
		final HttpServletRequest request, final HttpServletResponse response, final ServletContext servletContext,
		final Locale locale, final Map<String, Object> variables) {

	variables.putAll(vars);
}

}

@Test void someTest() { HashMap<String, Object> vars = new HashMap<>(); vars.put("key", validations); executor.setProcessingContextBuilder(new WebProcessingContextBuilderWithJavaVariables(vars)); executor.execute("classpath:template.thtest");

Assertions.assertTrue(executor.isAllOK());

}`

zian92 avatar May 12 '23 15:05 zian92