Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

AOT test code gen fails when same name test classes exist in different package #1313

Closed
ttddyy opened this issue Nov 30, 2021 · 2 comments
Closed
Assignees
Labels
type: bug A general bug
Milestone

Comments

@ttddyy
Copy link
Contributor

ttddyy commented Nov 30, 2021

When there are test classes with the same name in different packages that have independent MergedContextConfiguration, the AOT test code generation fails.

For example, if MyTest class exists in com.example.foo and com.example.bar packages, the following exception is thrown.

java.lang.IllegalArgumentException: context with id 'MyTestContextInitializer' already exists
	at org.springframework.aot.context.bootstrap.generator.infrastructure.DefaultBootstrapWriterContext.fork(DefaultBootstrapWriterContext.java:129)
	at org.springframework.aot.context.bootstrap.generator.infrastructure.DefaultBootstrapWriterContext.fork(DefaultBootstrapWriterContext.java:123)
	at org.springframework.aot.test.context.bootstrap.generator.TestContextAotProcessor.generateTestContext(TestContextAotProcessor.java:107)
	at org.springframework.aot.test.context.bootstrap.generator.TestContextAotProcessor.generateTestContexts(TestContextAotProcessor.java:78)
	at org.springframework.aot.test.build.TestContextBootstrapContributor.contribute(TestContextBootstrapContributor.java:61)
	at org.springframework.aot.build.BootstrapCodeGenerator.generate(BootstrapCodeGenerator.java:89)
	at org.springframework.aot.build.BootstrapCodeGenerator.generate(BootstrapCodeGenerator.java:68)
	at org.springframework.aot.test.build.GenerateTestBootstrapCommand.call(GenerateTestBootstrapCommand.java:87)
	at org.springframework.aot.test.build.GenerateTestBootstrapCommand.call(GenerateTestBootstrapCommand.java:45)
	at picocli.CommandLine.executeUserObject(CommandLine.java:1953)
	at picocli.CommandLine.access$1300(CommandLine.java:145)
	at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2352)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2346)
	at picocli.CommandLine$RunLast.handle(CommandLine.java:2311)
	at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2179)
	at picocli.CommandLine.execute(CommandLine.java:2078)

This is because when a MergedContextConfiguration is different, TestContextConfigurationDescriptorFactory creates different TestContextConfigurationDescriptors. When this descriptor contains a single test class, it uses the test class name(MyTestContextInitializer in this case) as a map key:
https://github.com/spring-projects-experimental/spring-native/blob/d8adb77be77da3f07a18dc95d54a8fec839edb0a/spring-aot-test/src/main/java/org/springframework/aot/test/context/bootstrap/generator/TestContextAotProcessor.java#L149-L153
https://github.com/spring-projects-experimental/spring-native/blob/d8adb77be77da3f07a18dc95d54a8fec839edb0a/spring-aot-test/src/main/java/org/springframework/aot/test/context/bootstrap/generator/TestContextAotProcessor.java#L106-L107

Then, in the fork method, the unique check(this.allContexts.containsKey(id))) is failing since the test classes in both test descriptors have the same name:
https://github.com/spring-projects-experimental/spring-native/blob/2833e110534bce100820faac881932ca0ae5535e/spring-aot/src/main/java/org/springframework/aot/context/bootstrap/generator/infrastructure/DefaultBootstrapWriterContext.java#L127-L134

In this case, the key needs to be a unique valid java class name.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 30, 2021
@sdeleuze
Copy link
Contributor

@snicoll Should we consider this one for for 0.11.0.?

@snicoll
Copy link
Contributor

snicoll commented Nov 30, 2021

Yes

@snicoll snicoll added type: bug A general bug theme: aot and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 30, 2021
@snicoll snicoll added this to the 0.11.0 milestone Nov 30, 2021
@snicoll snicoll self-assigned this Nov 30, 2021
@snicoll snicoll closed this as completed in 6950476 Dec 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug A general bug
Development

No branches or pull requests

4 participants