Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map.of() and Set.of() being replaced with Collections.emptyMap() and Collections.emptySet() #1385

Closed
ryanc16 opened this issue Feb 10, 2025 · 2 comments

Comments

@ryanc16
Copy link

ryanc16 commented Feb 10, 2025

This is very similar behavior as #1106 , but that says it was fixed on release 1.9.10. I was using 1.17.4 and just updated to 1.18.0 today and still seeing this behavior. However, I am wondering if this has something to do with the fact I am using Lombok on this class.

I am using org.pitest.pitest-maven (tried 1.17.4 and 1.18.0) and org.projectlombok.lombok 1.18.28

I will try to provide a minimal reproducible example from what I have going on.

ApplicationState.java

@Data
public class ApplicationState {

    private InnerState inner = new InnerState();

    @Data
    @Builder(toBuilder = true)
    @AllArgsConstructor
    public static class InnerState {
        @Builder.Default
        private Map<String, String> info = Map.of();
    }
}

My test looks something like this:
ApplicationStateTest.java

public class ApplicationStateTest {

    @Test
    public void testDefaultState() {
        ApplicationState defaultState = new ApplicationState();

        Assertions.assertThat(defaultState.getInner().getInfo()).isEmpty();
    }
}

My assertion of course passes because PITest in its generated report, says it replaced Map.of() with Collections.emptyMap() which are both initially empty maps. I would prefer to avoid adding rather unnecessary assertions to check that the property isn't the same as Collections.emptyMap() since I don't even directly use that in my code. Adding that would only be to satisfy the mutation testing. I would also like to avoid adding anything to the PITest maven config that excludes all calls to Map.of() or similar since I assume that non-empty maps wouldn't get coverage.

Interestingly if I replace my usage of Map.of() with Collections.emptyMap() in my source code class, PITest report still reports it replaced Collections.emptyMap() with Collections.emptyMap().

Also note, there are other properties in my real implementation that use Set.of() and also initial values such as empty string "" and PITest is reporting it replaced those with Collections.emptySet() and "" respectively as well, leading me to believe there is a misunderstanding between PITest and Lombok Builder.

@hcoles
Copy link
Owner

hcoles commented Feb 12, 2025

If you can create a minimal project that reproduces the issue, I'll take a look.

@ryanc16
Copy link
Author

ryanc16 commented Feb 15, 2025

Ok so I was looking over things again and I now that I am looking at it a bit more, the message in the tooltip for the info property line is saying

  1. getInfo : replaced return value with Collections.emptyMap for com/pitestrepro/ApplicationState$InnerState::getInfo → SURVIVED

Which I am now realizing probably is talking about replacing the return value in the auto-generated getInfo method that lombok is creating in the bytecode, but isn't present in my source code, and doesn't really have anything to do with replacing the initial value I am providing to the property for the purpose of the lombok @Builder.Default. I think it was tripping me up because of the line it was showing the message on, but I don't really know where else it would have to show message due to it not being present in the source code. It's interesting its able to backwards associate it.

I also came across #347 and I think this discussion is more along the lines of what I was experiencing, but not realizing was happening. Reading that issue, I found the lombok config that includes @Generated to the generated methods/classes and this seems to clear that up. However unless I add any addition non-lombok generated code then there's really nothing left for PITest to mutate, so no report is created for the class.

Anyway, I think this issue can be closed. Thank you for your time.

@hcoles hcoles closed this as completed Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants