Annotations
The Across Test library also provides a set of Across-specific annotations that extend the default Spring Testing annotations (eg @ContextConfiguration
) to support AcrossContext
setups.
@AcrossTestConfiguration
Class-level annotation to be used as an alternative to developing-applications.adoc#enable-across-context
.
Like @EnableAcrossContext
this annotation will configure and bootstrap an AcrossContext
but also add support for:
-
easily exposing additional types for the scope of the test
-
resetting the database before bootstrap
-
creating a
MockMvc
bean instantiated with all dynamically registered filters from the context, when used in a@AcrossWebAppConfiguration
test class
When using either @EnableAcrossContext
or @AcrossTestConfiguration
, all beans exposed can be wired directly in the unit test class.
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@ContextConfiguration
public class TestSample
{
@Autowired
private MyExposedBean myExposedBean;
@Test
public void doSomethingWithMyExposedBean() {
...
}
@AcrossTestConfiguration(modules = MyModule.NAME, expose = MyExposedBean.class)
protected static class Config
{
}
}
@AcrossWebAppConfiguration
Class-level annotation that sets up a @WebAppConfiguration
test that uses a MockAcrossServletContext
for initialization.
@AcrossWebAppConfiguration
contains a @ContextConfiguration
annotation, unless additional annotations are specified, it will load static innter @Configuration
classes of the annotated test class.
When using this annotation on a test class the MockAcrossServletContext
instance can be autowired for introspection.
In combination with an inner static @AcrossTestConfiguration
class, this will support a MockMvc
instance initialized with all dynamically registered filters.
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext
@AcrossWebAppConfiguration
@TestPropertySource( properties = "acrossWebModule.resources.versioning.enabled=false" )
public class TestSample
{
@Autowired
private MockAcrossServletContext servletContext;
@Autowired
private MockMvc mvc;
@Test
public void resourceUrlEncodingFilterShouldNotBeRegistered() {
assertNull( servletContext.getFilterRegistration( ResourceUrlEncodingFilterConfiguration.FILTER_NAME ) );
}
@Test
public void staticResourceShouldBeReturned() {
mvc.perform( get( "/across/resources/static/testResources/test.txt" ) )
.andExpect( status().isOk() )
}
@AcrossTestConfiguration(modules = { AcrossWebModule.NAME })
protected static class Config
{
}
}