@Test(expected = FreemarkerAutotagException.class)
public void testGetAsObjectException() throws TemplateModelException {

    TemplateNumberModel model = createMock(TemplateNumberModel.class);
    Template template = createMock(Template.class);
    TemplateHashModel rootDataModel = createMock(TemplateHashModel.class);
    Writer out = createMock(Writer.class);
    expect(model.getAsNumber()).andThrow(new TemplateModelException());
    expect(template.getMacros()).andReturn(new HashMap<String, Macro>());
    replay(template, rootDataModel, out);
    new Environment(template, rootDataModel, out);
    replay(model);
    try {
        assertEquals(new Integer(42), FreemarkerUtil.getAsObject(model, Integer.class, new Integer(1)));
    } finally {
        verify(template, rootDataModel, out, model);
    }
}