You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC与Gradle多模块:如何将/WEB-INF/内容作为模块测试依赖引用

Accessing /WEB-INF/ Content in Multi-Module Spring + Thymeleaf Tests

Hey there! I’ve worked through similar multi-module Spring testing scenarios before, so let’s break down how to get your /WEB-INF/ content accessible during module tests.

1. Configure Gradle to Expose WEB-INF Resources to Tests

Since you’re using Gradle, the first step is to make sure your test module can access the WEB-INF resources from your web module.

For the Test Module’s build.gradle:

Add a dependency on your web module, and explicitly include the web module’s src/main/webapp directory (where /WEB-INF/ lives) in your test resources:

// Depend on your web module
testImplementation project(':your-web-module')

// Add the webapp directory to test resources so WEB-INF is available
sourceSets.test.resources.srcDirs += project(':your-web-module').file('src/main/webapp')

Optional: Copy WEB-INF Resources in the Web Module

You can also configure the web module’s build.gradle to automatically copy /WEB-INF/ content to the test classpath during the processTestResources task:

processTestResources {
    from('src/main/webapp/WEB-INF') {
        into('WEB-INF')
    }
}

This ensures all /WEB-INF/ files are copied to build/resources/test/WEB-INF, making them accessible via the classpath in tests.

2. Adjust the TemplateResolver for Testing

Your production TemplateResolver uses a prefix pointing to /WEB-INF/view/html/, but in tests, you’ll need to update this to use the classpath (since resources are now in the test classpath).

Create a Test-Specific Configuration

Define a test-only @Configuration class to override the TemplateResolver:

@Configuration
public class TestThymeleafConfig {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public SpringResourceTemplateResolver testTemplateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        // Point to the classpath version of WEB-INF
        templateResolver.setPrefix("classpath:/WEB-INF/view/html/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCacheable(false); // Disable caching for tests
        return templateResolver;
    }

    // Don't forget to define the TemplateEngine and ViewResolver beans too, if needed
}

For Slice Tests (e.g., @WebMvcTest)

If you’re using Spring’s test slices, use @WebAppConfiguration to specify the root path of your web resources:

@WebMvcTest(YourController.class)
@WebAppConfiguration("classpath:webapp") // Maps to the copied src/main/webapp content
@Import(TestThymeleafConfig.class)
public class YourControllerTest {

    @Autowired
    private MockMvc mockMvc;

    // Your test methods here
}

3. Alternative: Move Views to src/main/resources

If you’re open to adjusting your project structure, moving Thymeleaf views from /WEB-INF/view/html/ to src/main/resources/templates/html/ simplifies things.

Update your production TemplateResolver to use the classpath prefix:

templateResolver.setPrefix("classpath:/templates/html/");

Since resources in src/main/resources are automatically included in both production and test classpaths, you won’t need any extra Gradle or test configuration—tests will pick up the views seamlessly.


内容的提问来源于stack exchange,提问作者Manuel Jordan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:13:13