1

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.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Deep
  • 929
  • 2
  • 16
  • 32

2 Answers2

1

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.

Deep
  • 929
  • 2
  • 16
  • 32
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • I think it is a valid explanation, because as there is no way to deduce that the class is a valid JUnit test case. – Deep Jan 04 '19 at 12:48
0

I may not be able to add much as to why Generics would remove JUnit from starting, but usually test classes act as a wrapper to test a specific class, such as:

public class Sample<T>
{
    ...
}

public class SampleTest
{

    Sample<TypeHere> sample;

    @Before
    public void setUp() throws Exception {
    }
        sample= new Sample<OrTypeHere>(...);
        ...
    }

    @Test
    public void whenConditionOne_verifyExpectedSituation() throws Exception {

        ...
    }
}
Deep
  • 929
  • 2
  • 16
  • 32
DarceVader
  • 98
  • 7
  • Adding `@RunWith(JUnit4.class)` before your test class is optional when `JUnit3` tests are not present in your test class, this is something I noticed just a little while ago, but may help in forcing tests to be of `JUnit4` – DarceVader Jan 04 '19 at 12:01