Home > other >  Unit Test for static functions in Java?
Unit Test for static functions in Java?

Time:10-01

I am trying to test the following condition in a service method, but as I cannot mock the necessary parts, I cannot make my test work:

public CountryDTO create(String countryCode) {
  String countryCodeUpper = countryCode.toUpperCase(Locale.ENGLISH);

  if (!Arrays.asList(Locale.getISOCountries()).contains(countryCodeUpper)) {
      throw new EntityNotFoundException("Exception...");
  }
  // code omitted
}

I tried many different things e.g. trying to @Mock Arrays, Locale, ... but none of them is worked. Here is my test shown below, but I think I need to mock Locale.getISOCountries().

@Test(expected = EntityNotFoundException.class)
public void should_Return_ThrowEntityNotFoundException() {
    String countryCode = "qq";
    String countryCodeUpper = countryCode.toUpperCase(Locale.ENGLISH);

    when(!Arrays.asList(Locale.getISOCountries()).contains(countryCode
        .toUpperCase(Locale.ENGLISH))).thenReturn(true);
    demoService.create(countryCode);
}

I know I can make this test work by providing a silly value, but I want to perform this Unit Test properly. How can I test it properly?

CodePudding user response:

I understand that you want the if condition to evaluate as true, even though it will always evaluate as false in your current setup, because you want to test the exception.

In this case, you want to mock the public static String[] getISOCountries() of the class Locale to return something that doesn't contain what you want, for example an empty array.

I did not test this code but it should be something like this (note that you will need to add a dependency to powermockito into your project):

@RunWith(PowerMockRunner.class)
@PrepareForTest(Locale.class) //<-- you want to mock the static methods of this class
public class YourTestClass {
    
    @Test(expected = EntityNotFoundException.class)
    public void test_that_create_throws_exception_when_english_country_code_is_not_available() {
        String countryCode = "qq";
        PowerMockito.mockStatic(Locale.class);
        BDDMockito.given(Locale.getISOCountries()).willReturn(new String[]{}); //when getISOCountries is called it will return an empty string array, which will make your test fail and throw the exception
        demoService.create(countryCode);            
    }
    

}

CodePudding user response:

    public DemoService{
      public static final void updateSoapRequest(Object endpoint) {

               Client cluImportClient = ClientProxy.getClient(endpoint);
      }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(value = { ClientProxy.class })
public class DemoServiceTest {
    private Client mockClient ;

    @Before
    public void setup() throws Exception {
        PowerMockito.mockStatic(ClientProxy.class);
    }

    @Test
    public void testUpdateSoapRequestWithBasicAuth() throws Exception {
        mockClient = mock(Client.class);
        PowerMockito.when(ClientProxy.getClient(any())).thenReturn(mockClient);
        DemoService.updateSoapRequest(new Object());
    }
}
  • Related