12
class MyWebAppInitializer extends WebApplicationInitializer {
  def onStartup(servletContext: ServletContext): Unit = {
      ...
  }
}

@RunWith(classOf[SpringJUnit4ClassRunner])
@WebAppConfiguration
@ContextConfiguration(classes = Array(classOf[MyConfig]),
    initializers=Array(classOf[MyWebAppInitializer]))     // <<< ERROR
class MyTest {
  ...
}

Complains about :

annotation argument needs to be a constant; found: classOf[MyWebAppInitializer] 

UPDATE: @M. Deinum points out that only ApplicationContextInitializers are allowed here - so the error is a badly reported type mistmatch.

So... how can I use my own MyWebAppInitializer in order and test the functionality defined therein?

user48956
  • 14,850
  • 19
  • 93
  • 154
  • You can provide `ApplicationContextInitializer`s NOT `WebApplicationInitializer`s. See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/ContextConfiguration.html#initializers-- – M. Deinum Jun 12 '14 at 05:54
  • Quite right (though I despair at Scala's error reporting, my supplied value _is_ a constant, but the error is a type mismatch). So, how to start my test with a custom WebApplicationInitializer -- Spring provides the ability to customize its onStartup (and people frequently do). Is there a way to test this customization in unit tests? – user48956 Jun 12 '14 at 14:55
  • Have updated the question to reflect the remaining issue -- how to test a WebApplicationInitializer. – user48956 Jun 12 '14 at 15:02
  • 3
    The `WebApplicationInitializer` is for bootstrapping your application it works in conjunction with a `ServletContainerInitializer`. If you want to test it then simply test it. Create an instance, pass an instance of `MockServletContext` to the startup method and see if it does what you expect it does. – M. Deinum Jun 13 '14 at 06:12

2 Answers2

10

In your context configuration I don't see that you have a context loader listed. The AnnotationConfigWebContextLoader will locate instances of WebApplicationInitializer on your classpath, by adding this and removing the intializers (which, as you have noted, are for ApplicationContextInitializers and not WebApplicationInitializers) then you should be all set.

@RunWith(classOf[SpringJUnit4ClassRunner])
@WebAppConfiguration
@ContextConfiguration(classes = {ConfigClass.class, AnotherConfigClass.class}, loader=AnnotationConfigWebContextLoader.class))
class MyTest {
...

Here is a working example

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={WebConfiguration.class, SecurityConfig.class}, loader=AnnotationConfigWebContextLoader.class)
@ActiveProfiles("dev")
public class AppTests {
    private MockMvc mockMvc;

    @Autowired
    protected WebApplicationContext webApplicationContext;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void simple() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.view().name("index"));
    }
}
lane.maxwell
  • 5,002
  • 1
  • 20
  • 30
2

The answer is "Sorry,you can't". You could refer to this: Spring FrameworkSPR-10199 Add capability to use WebApplicationInitializer for testing Spring web applications

Just as Sam Brannen said:

Although Spring does provide mocks for the Servlet API, Spring does not mock a Servlet container and currently has no intention to. Spring has always focused on out of container integration testing. Fully mocking a container is therefore beyond the scope of Spring's testing support. Please see comments from Rossen and me above for further details.

Dennis
  • 21
  • 1