Android Static import


Example

JUnit defines quite some assertEquals methods at least one for each primitive type and one for Objects is available. These methods are by default not directly available to call and should be called like this: Assert.assertEquals. But because these methods are used so often people almost always use a static import so that the method can be directly used as if it is part of the class itself.

To add a static import for the assertEquals method use the following import statement:

import static org.junit.Assert.assertEquals;

You can also static import all assert methods including the assertArrayEquals, assertNotNull and assertFalse etc. using the following static import:

import static org.junit.Assert.*;

Without static import:

@Test
public void addition_isCorrect(){
    Assert.assertEquals(4 , 2 + 2);
}

With static import:

@Test
public void addition_isCorrect(){
    assertEquals(4 , 2 + 2);
}