1

I have added two conditions in code:
n the code if gets covered properly, however I am not able to cover the else if part

public Optional < Flavors > callIcecream() {
    try {
        if (icecream == 1) {
            //do something
            return Optional.of(someString)
        } else if (icecream > 1) {
            //do something else
            log.error("");
        }
        return empty();
    } catch (Exception e) {
        throw new IceCreamException()
    }
}

I have added below code for mutation coverage

@Test
void getIcreamValues(){
  when(abc.buyIcream(Mockito.anyString())).thenReturn(getIcream(1));
  final Optional<Falvors>icecreamInfo = xyz.callIcecream();
  assertThat(icecreamInfo).isPresent();
}


@Test
void getIcreamValues(){
  when(abc.buyIcream(Mockito.anyString())).thenReturn(getIcream(0));
  final Optional<Falvors>icecreamInfo = xyz.callIcecream();
  assertThat(icecreamInfo).isEmpty();
}

@Test
void getIcreamValues(){
  when(abc.buyIcream(Mockito.anyString())).thenReturn(getIcream(2));
  final Optional<Falvors>icecreamInfo = xyz.callIcecream();
  assertThat(icecreamInfo).isPresent();
}
Bashir
  • 2,057
  • 5
  • 19
  • 44
Shruti Joshi
  • 301
  • 1
  • 4
  • 16
  • how do you set the value for iceCream? – Stultuske Jun 16 '20 at 12:25
  • using the value generated from function in the class: public Optional callIcecream(){ try{ final int icecream = getValues(); if(icecream==1){ //do something return Optional.of(someString) } else if(icecream > 1){ //do something else log.error(""); } return empty(); }catch(Exception e){ throw new IceCreamException() } } – Shruti Joshi Jun 16 '20 at 12:44
  • 1
    that is not the code you have in your question. we don't see a value set before that method is called. – Stultuske Jun 16 '20 at 12:51
  • so you need the code of getValues()? – Shruti Joshi Jun 16 '20 at 13:23
  • Can you post the method empty() – мalay мeнтa Jun 16 '20 at 14:27
  • Since the code is in java 11 and the return type of the function is Optional, I am able to use the inbuilt empty() method – Shruti Joshi Jun 16 '20 at 14:38
  • If you still need to refer the inbuilt code. It goes something like this: private static final Optional> EMPTY = new Optional<>(); public static Optional empty() { @SuppressWarnings("unchecked") Optional t = (Optional) EMPTY; return t; } – Shruti Joshi Jun 16 '20 at 14:39
  • 1
    this doesn't seem to be a mutation testing problem. You are just not being able to make your test get icecream == 2. It is not clear how icecream get set..maybe post the remaining class code? – pedrorijo91 Jun 16 '20 at 15:31

0 Answers0