<commit_before># Shoebill

Static methods are always a pain in the neck when you want to mock them in Unit Testing.

Shoebill offers you a clean way to generate a simple wrapper class for the one that has a bunch of static methods.

## How to Use

Just annotate your class with static methods with `@WrapStatic` annotation.

```java
@WrapStatic
public class Helper {
    public static boolean isConnectedWifi(@NonNull Context context) { }

    public static boolean isConnected3G(@NonNull Context context) { }
}
```

Shoebill generates a class `HelperWrapper` that has all the proxy instance methods corresponding to the static methods with the same signatures except `static` modifier.

```java
// generated code
public class HelperWrapper {
  private static HelperWrapper singleton = new HelperWrapper();

  public static HelperWrapper getInstance() {
    return singleton;
  }

  @VisibleForTesting(otherwise = VisibleForTesting.NONE)
  public static void setInstance(HelperWrapper wrapper) {
    singleton = wrapper;
  }

  public boolean isConnectedWifi(@NonNull Context context) {
    return Helper.isConnectedWifi(context);
  }

  public boolean isConnected3G(@NonNull Context context) {
    return Helper.isConnected3G(context);
  }
}
```

You can just use the wrapper methods instead of using the static methods directly.

```java
// boolean wifiStatus = Helper.isConnectedWifi(getApplicationContext());
boolean wifiStatus = HelperWrapper.getInstance().isConnectedWifi(getApplicationContext());
```

## How to mock

As you may notice that `HelperWrapper` has `setInstance(HelperWrapper wrapper)`, you can set mock instance when unit testing.

```java
@Before
public void setUp() throws Exception {
    HelperWrapper helperWrapper = mock(HelperWrapper.class);
    when(helperWrapper.isConnectedWifi(any(Context.class))).thenReturn(true);
    when(helperWrapper.isConnected3G(any(Context.class))).thenReturn(false);
    HelperWrapper.setInstance(helperWrapper);
}

@After
public void tearDown() throws Exception {
}

@Test
public void mockStatic() throws Exception {
    assertThat(HelperWrapper.getInstance().isConnectedWifi(null), is(true));
    assertThat(HelperWrapper.getInstance().isConnected3G(null), is(false));
}
```

This setter method is properly annotated with `@VisibleForTesting(otherwise = VisibleForTesting.NONE)` that your lint tool warns you if you try to use this method in production code as of Android Studio 2.3 or later.

## Installation

```
annotationProcessor 'us.shiroyama.android:shoebill-processor:0.1.1'
provided 'us.shiroyama.android:shoebill:0.1.1'
```

## License

```
Copyright 2017 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.<commit_msg>Add screenshot of VisibleForTesting's lint error<commit_after># Shoebill

Static methods are always a pain in the neck when you want to mock them in Unit Testing.

Shoebill offers you a clean way to generate a simple wrapper class for the one that has a bunch of static methods.

## How to Use

Just annotate your class with static methods with `@WrapStatic` annotation.

```java
@WrapStatic
public class Helper {
    public static boolean isConnectedWifi(@NonNull Context context) { }

    public static boolean isConnected3G(@NonNull Context context) { }
}
```

Shoebill generates a class `HelperWrapper` that has all the proxy instance methods corresponding to the static methods with the same signatures except `static` modifier.

```java
// generated code
public class HelperWrapper {
  private static HelperWrapper singleton = new HelperWrapper();

  public static HelperWrapper getInstance() {
    return singleton;
  }

  @VisibleForTesting(otherwise = VisibleForTesting.NONE)
  public static void setInstance(HelperWrapper wrapper) {
    singleton = wrapper;
  }

  public boolean isConnectedWifi(@NonNull Context context) {
    return Helper.isConnectedWifi(context);
  }

  public boolean isConnected3G(@NonNull Context context) {
    return Helper.isConnected3G(context);
  }
}
```

You can just use the wrapper methods instead of using the static methods directly.

```java
// boolean wifiStatus = Helper.isConnectedWifi(getApplicationContext());
boolean wifiStatus = HelperWrapper.getInstance().isConnectedWifi(getApplicationContext());
```

## How to mock

As you may notice that `HelperWrapper` has `setInstance(HelperWrapper wrapper)`, you can set mock instance when unit testing.

```java
@Before
public void setUp() throws Exception {
    HelperWrapper helperWrapper = mock(HelperWrapper.class);
    when(helperWrapper.isConnectedWifi(any(Context.class))).thenReturn(true);
    when(helperWrapper.isConnected3G(any(Context.class))).thenReturn(false);
    HelperWrapper.setInstance(helperWrapper);
}

@After
public void tearDown() throws Exception {
}

@Test
public void mockStatic() throws Exception {
    assertThat(HelperWrapper.getInstance().isConnectedWifi(null), is(true));
    assertThat(HelperWrapper.getInstance().isConnected3G(null), is(false));
}
```

This setter method is properly annotated with `@VisibleForTesting(otherwise = VisibleForTesting.NONE)` that your lint tool warns you if you try to use this method in production code as of Android Studio 2.3 or later.

![VisibleForTesting](https://raw.githubusercontent.com/srym/shoebill/master/images/visible_for_testing.png)

## Installation

```
annotationProcessor 'us.shiroyama.android:shoebill-processor:0.1.1'
provided 'us.shiroyama.android:shoebill:0.1.1'
```

## License

```
Copyright 2017 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
{'fraction_non_alphanumeric': 0.08983825597749648, 'fraction_numerical': 0.006680731364275668, 'mean_word_length': 5.265418502202643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '272429', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1547, 'n_words': 543}