3

I'm trying to figure out a way to test with MockMVC the serving of content using a WebSlice that only autoconfigs org.springframework.web.servlet.resource.ResourceHttpRequestHandler (and not controllers)

I've tried something like this:

@RunWith(SpringRunner.class)
@WebMvcTest(
    controllers = {}
)
public class StaticAssetsMVCTest {

but {} is the default and it looks for all controllers. Is there a way to exclude all controllers but keep the other basic spring-web things my app has so that i can test just the static assets slices?

Denis Zavedeev
  • 7,627
  • 4
  • 32
  • 53
Chris DaMour
  • 3,650
  • 28
  • 37

1 Answers1

0

What about testing static content against a full blown server?

Here is a sample application to show what I'm talking about.

The entry point for such tests is a @LeanWebTest annotation:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {
    MvcConfig.class,
    WebSecurityConfig.class
})
@EnableAutoConfiguration(exclude = {
    DataSourceAutoConfiguration.class,
    JpaRepositoriesAutoConfiguration.class
})
@Retention(RetentionPolicy.RUNTIME)
public @interface LeanWebTest {
}

@LeanWebTest is a @SpringBootTest using a fully configured server at random port.

It specifies the following @Configuration classes:

Spring Security overrides Cache-Control headers. Probably it is not what we might want especially with static resources.

See also: How to enable HTTP response caching in Spring Boot

@LeanWebTest does not use any @Beans from the application except of specified configuration classes. (No @Controllers created)

I see the following aspects applicable to @LeanWebTests:

  • robots.txt file
  • javax.servlet.Filters
  • static resources compression
  • static resources caching
  • etc..

The sample application has some tests:


Please note that the project is a rough demonstration of the concept

Denis Zavedeev
  • 7,627
  • 4
  • 32
  • 53