3

Update: The mutation coverage failure went away since this question was posted, probably due to a bugfix in pit?

So this question is invalid now. I don't know what to do in this case. Delete the question? Leave it here?

I am using pit for mutation testing. There is a zombie in a very simple function:

public boolean isAuthenticated() {
    return true;
}

It is tested with:

 assertEquals(true, isAuthenticated());

But I get the error replaced return of integer sized value with (x == 0 ? 1 : 0) → SURVIVED

I did try to check the identity with System.identityHashCode(), but that did not help.

Are there any way to kill this zombie? If not, how can I turn off this particular check for booleans or this particular line of code?

Árpád Magosányi
  • 1,394
  • 2
  • 19
  • 35
  • A simple check of the return value should kill this mutant, if it does not then something else is going on in your project. Can you provide more details - the full test, the test framework you're using etc. Are other mutants in the same class killed? – henry Oct 27 '19 at 07:54
  • are you sure that mutation is on that method? it doesn't make sense to replace a boolean with an integer result – pedrorijo91 Oct 27 '19 at 09:31
  • Yes, I am sure. And yes, it does not make sense. – Árpád Magosányi Nov 21 '19 at 18:25

1 Answers1

0

I think the error does not reveal the source of the problem, which in my case (most probably to any case) would be impossible to do so.

In my case I had a list of objects that contained a function which returns a predicate. So I used stream to iterate through all of them and I filtered the one of them that matched the predicate with another object as candidate. So in terms of code I had:

  public List<ParkingSlot> getFreeParkingSlots(List<DepartmentParkingRule> ruleList, Employee candidate){
      return 
          ruleList.stream().
          .filter(j -> j.getPredicate().test(candidate)) // <-- here appeared the error *replaced return of integer sized value with (x == 0 ? 1 : 0) → SURVIVED*
          .map(t -> t.getFreeParkingSlots(candidate))
          .findFirst()
          .orElseThrow(new NotMatchedException(candidate));
  }

I expected (by analysis/design) a candidate employee to match only one department rule. I thought using .findFirst() was a good fit. PIT denied. By mutating the result of predicate of a normally non matched case (department) to a 'mutant match', there could possibly be more than one match, not exactly one.

So I modified my code to throw an exception in case of having more than one match. After this change the mutant was KILLED. So my function is now:

  public List<ParkingSlot> getFreeParkingSlots(List<DepartmentParkingRule>  ruleList,
                                                 Employee candidate){

      Optional<List<ParkingSlot>> parkingSlots = ruleList.stream().
           .filter(j -> j.getPredicate().test(candidate)) 
           .map(t -> t.getFreeParkingSlots(candidate))
           .reduce((anElement, otherElement) -> 
                    throw new DuplicateException());//reduce is executed when a possible false positive duplicate rule match

      if (parkingSlots.isPresent() && CollectionUtils.isNotEmpty(parkingSlots.get())){
         return parkingSlots.get();
      }else {
         throw new NotMatchedException(candidate);
      }
  }

Thus the error replaced return of integer sized value with (x == 0 ? 1 : 0) → SURVIVED indicated the mutation happened and most importantly that this mutation did not cause a failure to my tests.

Next time I see this error, I will look to the impact on my tests as a whole and not to the specific boolean function.

Just to note that the above code is not real but rather useful to show how to interpret the error and actions made to modify the source code. Change to tests could possibly help killing that mutant but this depends on each case.

Hope it helps someone.

  • As I understand your case is about a situation where an unexpected behaviour of a dependency was "mocked bad" by PIT. How can it be applied to my case, where the production code have no dependency? My code is almost literally copied (only the class reference is missing in calling the production method). – Árpád Magosányi Dec 05 '19 at 02:37
  • @Árpád Magosányi: No, it is not about mocked dependency. It is about how to interpret the error and the actions you need to do to resolve it. Provide a working showcase of your error on Github and I will help you with it. – Nikos Stais Jan 01 '20 at 22:20
  • https://github.com/kode-konveyor/tidy-up/blob/develop/src/main/java/com/kodekonveyor/webapp/RemoteAuthentication.java#L43 And the test: https://github.com/kode-konveyor/tidy-up/blob/develop/src/test/java/com/kodekonveyor/webapp/RemoteAuthenticationTest.java#L70 It does not fail the build, because the class have an @InterfaceClass annotation, but you can find it in the pit report. – Árpád Magosányi Jan 10 '20 at 13:48
  • @ÁrpádMagosányi after I removed dependency of xml-doclet which requires java 7 tools.jar, I cannot reproduce it. It gives me 100% mutation coverage. Please see changes here: https://github.com/nikosstais/tidy-up/commit/aeee1361a3047463cc70ab2c5c32316ae78a7802#diff-600376dffeb79835ede4a0b285078036 . Perhaps you may suffer from configuration/dependency issues. – Nikos Stais Mar 08 '20 at 23:15