• jinusa2007@gmail.com
  • 1-646-580-5887

Mockito how to mock and get proper values for ENUM

     -

Mockito how to mock and get proper values for ENUM

JavaMockito code  download

 

First of all Mockito can create mock data which can be integer long etc It cannot create right enum as enum has specific number of ordinal name value etc so if i have enum
public enum HttpMethod {

GET, POST, PUT, DELETE, HEAD, PATCH;

} so i have total 5 ordinal in enum HttpMethod but mockito does not know it .Mockito creates mock data and its null all the time and you will end up in passing a null value . So here is proposed solution that you randomize the ordinal and get a right enum which can be passed for other test

import static org.mockito.Mockito.mock;

import java.util.Random;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.internal.util.reflection.Whitebox;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.amazonaws.HttpMethod;




//@Test(expected = {"LoadableBuilderTestGroup"})
//@RunWith(PowerMockRunner.class)
public class testjava {
   // private static final Class HttpMethod.getClass() = null;
    private HttpMethod mockEnumerable;

    @Test
    public void setUpallpossible_value_of_enum () {
        for ( int i=0 ;i<10;i++){
        String name;
        mockEnumerable=    Matchers.any(HttpMethod.class);
        if(mockEnumerable!= null){
            System.out.println(mockEnumerable.ordinal());
         System.out.println(mockEnumerable.name());

         System.out.println(mockEnumerable.name()+"mocking suceess");
        }
        else
        {

                //Randomize all possible  value of  enum 
                Random rand = new Random();
                int ordinal = rand.nextInt(HttpMethod.values().length); 
                // 0-9. mockEnumerable=
                mockEnumerable= HttpMethod.values()[ordinal];
             System.out.println(mockEnumerable.ordinal());
             System.out.println(mockEnumerable.name());

        }
        }
    }







    @Test
    public void setUpallpossible_value_of_enumwithintany () {
        for ( int i=0 ;i<10;i++){
        String name;
        mockEnumerable=    Matchers.any(HttpMethod.class);
        if(mockEnumerable!= null){
            System.out.println(mockEnumerable.ordinal());
         System.out.println(mockEnumerable.name());

         System.out.println(mockEnumerable.name()+"mocking suceess");
        }
        else
        {
            int ordinal;
                //Randomize all possible  value of  enum 
                Random rand = new Random();
                int imatch =  Matchers.anyInt();
                if(  imatch>HttpMethod.values().length)
                     ordinal = 0    ;
                else
                 ordinal = rand.nextInt(HttpMethod.values().length);

                // 0-9. mockEnumerable=
                mockEnumerable= HttpMethod.values()[ordinal];
             System.out.println(mockEnumerable.ordinal());
             System.out.println(mockEnumerable.name());

        }

        }

    }
}

Output : 0 GET 0 GET 5 PATCH 5 PATCH 4 HEAD 5 PATCH 3 DELETE 0 GET 4 HEAD 2 PUT

Archives