This is a weird situation I am facing in JUnit.
Given :
I needed to create Unit tests to test the methods defined in an abstract class.
For this, I created a ConcreteAbstractClassImpl which extended the AbstractClass and provided dummy responses for the abstract methods in it.
To define tests, I created a test class ConcreteAbstractClassImplTest. All non-abstract methods of AbstractClass were unit tested in this class using @Test.
Code sample provided below :
import static org.junit.Assert.*;
@RunWith(PowerMockRunner.class)
public class AbstractClassTest {
@Mock
private DependencyClass dependencyClassObj;
@InjectMocks
private ConcreteAbstractClassImpl concreteAbstractClassImpl ;
@Before
public void setUP() {
.........
}
@Test
public void test_non_abstract_method_AbstractClassImpl () {
String response = concreteAbstractClassImpl.method_non_abstract();
assertEquals(response, "method_non_abstract");
}
}
class ConcreteAbstractClassImpl extends AbstractClass {
private DependencyClass dependencyClassObj;
public abstract String method_abstract(){
return "dummy_response";
}
}
class AbstractClass {
@Autowired
private DependencyClass dependencyClassObj;
public abstract String method_abstract();
public String method_non_abstract(){
return "method_non_abstract";
}
}
Problem : The test class runs perfectly and recognizes each test case when run individually in Eclipse via Run As > Junit Test. But when I build the whole project using Run As > Maven Build on pom.xml and unchecking Skip Tests, all the tests mentioned in ConcreteAbstractClassImplTest are ignored by JUnit. Regular test classes which do not follow the above pattern are recognized correctly by JUnit. I don't know if this is a bug or something in JUnit but would be of great help if someone had a workaround/fix for this.
This issue is also affecting my Sonar coverage due to the test classes being skipped.