Skip to content

Commit 3b58a1a

Browse files
committed
fix: replace reflection with package-private test seam in CargoProvider test
1 parent 44544ff commit 3b58a1a

2 files changed

Lines changed: 7 additions & 25 deletions

File tree

src/main/java/io/github/guacsec/trustifyda/providers/CargoProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private void handleVirtualWorkspace(
208208
}
209209
}
210210

211-
private void processWorkspaceDependencies(
211+
void processWorkspaceDependencies(
212212
Sbom sbom,
213213
PackageURL root,
214214
Map<String, CargoPackage> packageMap,

src/test/java/io/github/guacsec/trustifyda/providers/CargoProviderCargoParsingTest.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package io.github.guacsec.trustifyda.providers;
1818

19+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -28,13 +29,11 @@
2829
import io.github.guacsec.trustifyda.sbom.SbomFactory;
2930
import io.github.guacsec.trustifyda.tools.Ecosystem.Type;
3031
import java.io.IOException;
31-
import java.lang.reflect.InvocationTargetException;
3232
import java.nio.charset.StandardCharsets;
3333
import java.nio.file.Files;
3434
import java.nio.file.Path;
3535
import java.util.HashMap;
3636
import java.util.HashSet;
37-
import java.util.Map;
3837
import java.util.Set;
3938
import org.junit.jupiter.api.Test;
4039
import org.junit.jupiter.api.io.TempDir;
@@ -760,28 +759,11 @@ public void testVirtualWorkspaceWithoutWorkspaceDepsDoesNotThrowNPE(@TempDir Pat
760759
new PackageURL(Type.CARGO.getType(), null, "test-workspace", "1.0.0", null, null);
761760
sbom.addRoot(root);
762761

763-
// Call the private processWorkspaceDependencies via reflection
764-
var method =
765-
CargoProvider.class.getDeclaredMethod(
766-
"processWorkspaceDependencies",
767-
Sbom.class,
768-
PackageURL.class,
769-
Map.class,
770-
Set.class,
771-
TomlParseResult.class);
772-
method.setAccessible(true);
773-
774762
// This should NOT throw NPE when [workspace.dependencies] is absent
775-
try {
776-
method.invoke(provider, sbom, root, new HashMap<>(), new HashSet<>(), tomlResult);
777-
} catch (InvocationTargetException e) {
778-
if (e.getCause() instanceof NullPointerException) {
779-
throw new AssertionError(
780-
"processWorkspaceDependencies threw NPE when [workspace.dependencies] is absent."
781-
+ " The method should handle a missing workspace.dependencies table gracefully.",
782-
e.getCause());
783-
}
784-
throw e;
785-
}
763+
assertDoesNotThrow(
764+
() ->
765+
provider.processWorkspaceDependencies(
766+
sbom, root, new HashMap<>(), new HashSet<>(), tomlResult),
767+
"processWorkspaceDependencies should handle missing [workspace.dependencies] gracefully");
786768
}
787769
}

0 commit comments

Comments
 (0)