0

This is a weird situation I am facing in JUnit.

Given :

  1. I needed to create Unit tests to test the methods defined in an abstract class.

  2. For this, I created a ConcreteAbstractClassImpl which extended the AbstractClass and provided dummy responses for the abstract methods in it.

  3. 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.

chepaiytrath
  • 678
  • 1
  • 9
  • 20
  • why are you mocking the class you are trying to test? – Stultuske Jun 14 '18 at 11:46
  • no i am not. the class i need to test is AbstractClass whos concrete implementation is ConcreteAbstractClassImpl --> for which I have created ConcreteAbstractClassImplTest in which I mock the "DependencyClass object" and inject it into ConcreteAbstractClassImpl using InjectMocks. So confusing, I know. :D – chepaiytrath Jun 14 '18 at 12:03
  • according to this thread, you are using the wrong runner (https://stackoverflow.com/questions/16467685/difference-between-mock-and-injectmocks?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – Stultuske Jun 14 '18 at 12:14
  • If by this u mean, I should be using MockitoJUnitRunner instead of PowerMockRunner, please note that I tried and it didn't work. :( – chepaiytrath Jun 14 '18 at 12:48
  • do you have any testclass with these annotations that work? – Stultuske Jun 14 '18 at 12:50
  • "Regular test classes which do not follow the above pattern are recognized correctly by JUnit". By regular ones, I mean the ones annotated by @PowerMockRunner but not testing an abstract class via dummy concreteImpl as above. – chepaiytrath Jun 15 '18 at 01:32
  • 1
    That's not what I asked. Do you have test classes using these that actually work? Also: why would you test abstract classes? They'll never "work" in their own private scope, since they are, you know, abstract – Stultuske Jun 15 '18 at 05:51
  • Try renaming your class. Looks like it's a problem with JUnit in not picking up classes with Abstract keyword on the front – Sunil Dabburi Oct 30 '19 at 17:38

0 Answers0