gnodet commented on code in PR #3516:
URL: https://github.com/apache/maven-surefire/pull/3516#discussion_r4792372822


##########
maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoToolchainsTest.java:
##########
@@ -68,6 +69,35 @@ public void shouldCallMethodWhenSpecSet() throws Exception {
         assertThat(actual).isSameAs(expectedMethod);
     }
 
+    @Test
+    public void 
shouldUseMatchingBuildContextToolchainWhenNoConfiguredToolchainMatches() throws 
Exception {
+        AbstractSurefireMojoTest.Mojo mojo = new 
AbstractSurefireMojoTest.Mojo();
+        ToolchainPrivate expectedFromContext = mock(ToolchainPrivate.class);
+        Map<String, String> requirements = singletonMap("version", "11");
+        
when(expectedFromContext.matchesRequirements(requirements)).thenReturn(true);
+        mojo.setToolchainManager(new MockToolchainManager(null, 
expectedFromContext));
+        mojo.setJdkToolchain(requirements);
+
+        Toolchain actual = invokeMethod(mojo, "getToolchain");
+
+        assertThat(actual).isSameAs(expectedFromContext);
+    }
+
+    @Test
+    public void shouldNotUseNonMatchingBuildContextToolchain() {
+        AbstractSurefireMojoTest.Mojo mojo = new 
AbstractSurefireMojoTest.Mojo();
+        ToolchainPrivate toolchainFromContext = mock(ToolchainPrivate.class);
+        Map<String, String> requirements = singletonMap("version", "11");
+        
when(toolchainFromContext.matchesRequirements(requirements)).thenReturn(false);
+        mojo.setToolchainManager(new MockToolchainManager(null, 
toolchainFromContext));
+        mojo.setJdkToolchain(requirements);
+
+        MojoFailureException exception =
+                assertThrows(MojoFailureException.class, () -> 
invokeMethod(mojo, "getToolchain"));
+
+        assertThat(exception.getMessage()).contains("{version=11}");
+    }

Review Comment:
   _Claude Code on behalf of Guillaume Nodet_
   
   Consider adding test coverage for these edge cases:
   
   1. **Null build context toolchain**: When `getToolchainFromBuildContext()` 
returns null
   2. **Non-ToolchainPrivate instance**: When build context returns a 
`Toolchain` that is not an instance of `ToolchainPrivate`
   
   Example tests:
   ```java
   @Test
   public void shouldFailWhenBuildContextToolchainIsNull() {
       AbstractSurefireMojoTest.Mojo mojo = new AbstractSurefireMojoTest.Mojo();
       Map<String, String> requirements = singletonMap("version", "11");
       mojo.setToolchainManager(new MockToolchainManager(null, null));
       mojo.setJdkToolchain(requirements);
   
       MojoFailureException exception =
               assertThrows(MojoFailureException.class, () -> 
invokeMethod(mojo, "getToolchain"));
   
       assertThat(exception.getMessage()).contains("{version=11}");
   }
   
   @Test
   public void shouldFailWhenBuildContextToolchainIsNotToolchainPrivate() {
       AbstractSurefireMojoTest.Mojo mojo = new AbstractSurefireMojoTest.Mojo();
       Toolchain regularToolchain = mock(Toolchain.class); // Not 
ToolchainPrivate
       Map<String, String> requirements = singletonMap("version", "11");
       mojo.setToolchainManager(new MockToolchainManager(null, 
regularToolchain));
       mojo.setJdkToolchain(requirements);
   
       MojoFailureException exception =
               assertThrows(MojoFailureException.class, () -> 
invokeMethod(mojo, "getToolchain"));
   
       assertThat(exception.getMessage()).contains("{version=11}");
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to