Search code examples
javaeclipsejunitjunit4

After creation of Junit test cases, the "Run as Junit Test" disappears


I had made a Junit Test case using menu options in Eclipse. I was getting the option "Run as JUnit Test". But when I was done with my changes, I noticed that the "Run as JUnit Test" disappeared.

On further analysis I found that my initially my TestClass definition was as below :

public class SampleTest
{
....
}

But I had to change the class definition to follows

public class SampleTest<T>   
{
..
}

So what I noticed was adding the generic was creating the behavior. I looked at the following links:

Run As JUnit not appearing in Eclipse - using JUnit4

Missing "Run as JUnit Test"

public class SampleTest<T>
{
..
}

But these links are less related to my issue. So, I need to understand the reason what including Generics has to do with the behavior.


Solution

  • For running a JUnit test class Eclipse needs to

    1. create an instance of your test class by calling its default constructor,
    2. call all the @Test methods on that instance

    Here the problem is in step 1: Eclipse doesn't know which type T to use in the constructor.
    Should it use new SampleTest<Object>() or new SampleTest<WhatEver>()?
    So it decides that it is not a valid JUnit test class and doesn't offer the Run as JUnit Test option.