Skip to content

InvalidPathException in log when running SpringBootTest with NIO Path property on Windows #26828

@kascaks

Description

@kascaks

When running following test, an InvalidPathException is logged with warn level. Test completes successfully, however the exception in log is quite misleading.

@SpringBootTest
public class MockServletTest {
	@SpringBootApplication
	public static class TestContext {
		@Value("C:\\tmp")
		private Path path;
	}
	@Autowired
	private TestContext testContext;
	@Test
	public void testPath() {
		Assertions.assertEquals(Paths.get("C:\\tmp"), testContext.path);
	}
}
WARN 17328 --- [           main] o.s.b.t.m.w.SpringBootMockServletContext : Could not get URL for resource src/main/webapp/C:/tmp

java.nio.file.InvalidPathException: Illegal char <:> at index 17: src\main\webapp\C:\tmp
	at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) ~[na:1.8.0_252]
	...
	at java.io.File.toPath(File.java:2234) ~[na:1.8.0_252]
	...
	at org.springframework.mock.web.MockServletContext.getResource(MockServletContext.java:332) ~[spring-test-5.3.6.jar:5.3.6]
	at org.springframework.boot.test.mock.web.SpringBootMockServletContext.getResource(SpringBootMockServletContext.java:90) [spring-boot-test-2.4.5.jar:2.4.5]
	at org.springframework.web.context.support.ServletContextResource.exists(ServletContextResource.java:104) [spring-web-5.3.6.jar:5.3.6]
	at org.springframework.beans.propertyeditors.PathEditor.setAsText(PathEditor.java:104) [spring-beans-5.3.6.jar:5.3.6]
	...

Test environment is spring boot 2.4.5 on Windows.
The exception does not appear when running the application itself (i.e. outside of test)

Seems like this was already reported in this issue and specifically this comment, but it is still there. As said above, it is not affecting test result (test passes), it's just unexpected to see the exception when checking test logs, so the fix would be a 'nice to have' feature.

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions