VaadinServletContext implements VaadinContext { @Override public <T> T getAttribute(Class<T> type, Supplier<T> defaultValueSupplier) { ensureServletContext(); synchronized (this) { Object result = context.getAttribute(type.getName()); if (result == null && defaultValueSupplier != null) { result = defaultValueSupplier.get(); context.setAttribute(type.getName(), result); } return type.cast(result); } } VaadinServletContext(ServletContext context); ServletContext getContext(); @Override T getAttribute(Class<T> type, Supplier<T> defaultValueSupplier); @Override void setAttribute(Class<T> clazz, T value); @Override void removeAttribute(Class<?> clazz); @Override Enumeration<String> getContextParameterNames(); @Override String getContextParameter(String name);  }
@Test public void getAttributeWithProvider() { Assert.assertNull(context.getAttribute(String.class)); String value = context.getAttribute(String.class, VaadinServletContextTest::testAttributeProvider); Assert.assertEquals(testAttributeProvider(), value); Assert.assertEquals("Value from provider should be persisted", testAttributeProvider(), context.getAttribute(String.class)); }
@Test public void getMissingAttributeWithoutProvider() { String value = context.getAttribute(String.class); Assert.assertNull(value); }