Content Application Developer Manual / Version 2307
Table Of Contents
This example demonstrates how to set up an infrastructure that can be used for testing project
link schemes. In the project's bean declaration
myproject-linkschemes-beans.xml
several link schemes are defined, as
well as some CAE basic infrastructure such as the LinkFormatter bean. It is very useful to load
exactly this file into a test application context, in order to...
test the contents of the file itself, for example detect whether there a syntactical or wiring problems
test the service instances with a configuration that is (nearly) equal to the configuration used in the project
test the service (in this example: the links scheme) in interaction with similar services, for example make sure that a certain link scheme is addressed for certain parameters and not a different link scheme instance.
Use the configuration pattern to construct the application context with the desired configuration:
@SpringJunitConfig(MyTest.LocalConfig.class) @ActiveProfiles(MyTest.LocalConfig.PROFILE) class MyTest { @Configuration(proxyBeanMethods = false) @ImportResource( value = { XmlRepoResources.LINK_FORMATTER, "classpath:/com/mycompany" + "/myproject/myproject-linkschemes-beans.xml" }, reader = ResourceAwareXmlBeanDefinitionReader.class ) @Import(XmlRepoConfiguration.class) @Profile(PROFILE) public static class LocalConfig { public static final String PROFILE = "MyTest"; } // ... }
Using a local test-only profile is recommended if you are using component scan to find your beans.
If not using the ActiveProfile
, Profile
annotation pair
LocalConfig
classes of other tests might be found through component scan.
Now you can just inject the LinkFormatter
and use it as in production code:
@Autowired LinkFormatter linkFormatter; String link = linkFormatter.formatLink( new MyPage(123), "myView", new MockHttpServletRequest(), new MockHttpServletResponse(), false); Assertions.assertThat(link).isEqualTo("/123?view=myView");