@SuppressWarnings("unchecked")
@Test(expected = FreemarkerRequestException.class)
public void testKeySetException() throws TemplateModelException {

    Template template = createMock(Template.class);
    TemplateHashModelEx model = createMock(TemplateHashModelEx.class);
    Configuration configuration = createMock(Configuration.class);
    Set<String> names = createMock(Set.class);
    Writer writer = new StringWriter();
    expect(template.getMacros()).andReturn(new HashMap<Object, Object>());
    expect(model.keys()).andThrow(new TemplateModelException());
    expect(template.getConfiguration()).andReturn(configuration);
    expect(configuration.getSharedVariableNames()).andReturn(names);
    try {
        replay(template, model, configuration, names);
        Environment env = new Environment(template, model, writer);
        EnvironmentScopeMap map = new EnvironmentScopeMap(env);
        map.keySet();
    } finally {
        verify(template, model, configuration, names);
    }
}