Trying to unit test a method and I'm unable to get rid of a mutation

1.3k Views Asked by At

I'm using JUnit 5 and Pitest 1.6.2, and I'm trying to unit test the following method

public class Validator {

  public static final int MAX_REQUEST_ID_LENGTH = 36;

  public void validateHeader(String requestId, String retryIndicator, ChannelIndicator channelIndicator, String apiSignature, Instant transactionTime) {
    if (requestId.length() > MAX_REQUEST_ID_LENGTH) {
      throw new ServerValidationException(SCHEMA_VALIDATION_ERROR, DEPOSIT);
    }
  }

}

using the following unit test

@ExtendWith(MockitoExtension.class)
class ValidatorTests {

  @InjectMocks
  Validator validator;

  @Mock
  private ErrorLogFactory errorLogFactory;

  private final String REQUEST_ID = "1612207630840";
  private final String REQUEST_ID_GREATER_THAN_MAX = StringUtils.repeat("*", MAX_REQUEST_ID_LENGTH + 1);
  private final String RETRY_INDICATOR = "false";
  private final String INVALID_RETRY_INDICATOR = "ABCD";
  private final String API_SIGNATURE = "api-signature";

  @Test()
  void validateHeader_requestId_greater_than_max() {
    Exception exception = assertThrows(ServerValidationException.class, () ->
    { validator.validateHeader(REQUEST_ID_GREATER_THAN_MAX, RETRY_INDICATOR, ChannelIndicator.ONLINE, API_SIGNATURE, Instant.now()); });

    assertTrue(exception.getMessage().contains(SCHEMA_VALIDATION_ERROR));
  }

}

and when I look at my pitest report it's telling me the following

  1. Location : validateHeader Killed by : none changed conditional boundary → SURVIVED
  2. negated conditional → KILLED

Any ideas on how I can kill this mutation?

1

There are 1 best solutions below

0
On

You have only a single test case defining the expected behaviour when a request id of length > 36 is used.

To kill the mutant you need an additional test showing the behaviour for an id with length == 36.