Checker for Static Import
When you write code, it is vital to keep consistency. It makes better readability and maintainability. It is easier when you work alone. However, it would be hard to keep it when many engineers work on the same project. Checkers are fit well for this situation. Among many consistency points, let’s see how to keep consistency on a static import.
For example, Mockito has several methods where static import can be applied. That is widely used in the unit testing and static and non-static import can be mixed in the same file or in another file. This style inconsistency is what we want to avoid.
Checkstyle is a tool to check those style matters. There are lots of useful style rules but no rule for static import. Static import is highly related on the classes in your code or third-party libraries. So, you need to define a custom rule. First of all, you can set up the Checkstyle for Android project:
build.gradle
Then, add a configure file with a custom rule. The rule uses a regular expression to find non-static Mockito imports. If there is a match, it is considered as an error.
config/checkstyle/checkstyle.xml
Now, you can run checkstyle
task to see a result on your code. Below there are two static import errors at line 16 and 21.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package io.github.staticimportchecker;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
public class ExampleUnitTest {
@Mock
private ConnectionInfo connectionInfo;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
ConnectionInfo connectionInfo = Mockito.mock(ConnectionInfo.class);
}
}
Conclusion
Consistency is an important factor to make a solid code. Checkstyle is a handy checker in terms of styling during collaboration. After discussion, you can add a custom rule, such as static import, to keep the consistency now and future.
If you want to try, you can clone this repository and run the task.