Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 474525

Summary: [JUnit] Cannot run JUnit tests against Generic Test
Product: [Eclipse Project] JDT Reporter: Rob Winch <rwinch>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: kdevolder, robin.rosenberg, stephan.herrmann
Version: 4.6   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X   
Whiteboard: stalebug
Attachments:
Description Flags
Sample of the issue
none
Screenshot of the Issue none

Description Rob Winch CLA 2015-08-07 16:01:54 EDT
Created attachment 255710 [details]
Sample of the issue

Eclipse does not allow for running against a Test that has generic arguments. For example, the following will run as a JUnit within Maven but cannot be ran as a JUnit within Eclipse:


public class GenericTest<T> {

	@Test
	public void run() {}
}


A workaround is to use a base class and make GenericTest abstract. This is fine in some circumstances, but is not ideal if you use something like Spring that does dependency injection for you. For example consider the following:

public interface Service<T> {

	T create();

	void save(T t);
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ServiceTest<T> {
	@Autowired
	Service<T> service;
	
	@Test
	public void test() {
		T t = service.create();
		
		// ...
		
		service.save(t);
	}

	@Configuration
	static class Config {
		@Bean
		public DefaultService defaultService() {
			return new DefaultService();
		}
	}
}

I'd like to code against the interface in my tests and run them. Again, this works in Maven but not Eclipse.

I have attached a sample project that illustrates these two issues. Simply import it as a Maven project into Eclipse Mars.
Comment 1 Stephan Herrmann CLA 2015-08-08 20:05:14 EDT
If you ask me, letting a framework (JUnit or Spring) instantiate a generic class looks scary to me! Why are those classes generic if there's nobody to specify type arguments?
Comment 2 Noopur Gupta CLA 2015-08-10 06:39:11 EDT
*** Bug 462854 has been marked as a duplicate of this bug. ***
Comment 3 Robin Rosenberg CLA 2015-08-10 08:19:16 EDT
(In reply to Stephan Herrmann from comment #1)
> If you ask me, letting a framework (JUnit or Spring) instantiate a generic
> class looks scary to me! Why are those classes generic if there's nobody to
> specify type arguments?

I haven't investigated why the classes I found were written that way, and indeed they could be changed, but then if it's ok with the Java specs, Eclipse should handle it too.
Comment 4 Rob Winch CLA 2015-08-10 10:14:28 EDT
(In reply to Stephan Herrmann from comment #1)
> If you ask me, letting a framework (JUnit or Spring) instantiate a generic
> class looks scary to me! Why are those classes generic if there's nobody to
> specify type arguments?

Thank you for your response. In my opinion a debate of if one should do this is off topic & unproductive.

The take away from this issue should be that generic tests are possible with JUnit outside of Eclipse, but not within Eclipse itself. This means there is a bug within Eclipse that should be fixed.
Comment 5 Stephan Herrmann CLA 2015-08-10 19:52:57 EDT
(In reply to Robin Rosenberg from comment #3)
> (In reply to Stephan Herrmann from comment #1)
> > If you ask me, letting a framework (JUnit or Spring) instantiate a generic
> > class looks scary to me! Why are those classes generic if there's nobody to
> > specify type arguments?
> 
> I haven't investigated why the classes I found were written that way, and
> indeed they could be changed, but then if it's ok with the Java specs,
> Eclipse should handle it too.

I didn't question legality in terms of JLS. It's legal Java code an hence the compiler accepts it.


I'm just worried by the fact, that reflective instantiation of a generic class implies you're running code that has not been properly type checked, and there isn't even a source location where the compiler could warn about this usage of raw types.


Actually, I can't reproduce any problem using class GenericTest from comment 0 - it runs fine for me. So, how exactly is it failing for you?
Comment 6 Rob Winch CLA 2015-08-11 10:30:33 EDT
Created attachment 255789 [details]
Screenshot of the Issue

Run As -> JUnit Test is missing from the context menu when the test has a generic type
Comment 7 Rob Winch CLA 2015-08-11 10:31:55 EDT
(In reply to Stephan Herrmann from comment #5)

> Actually, I can't reproduce any problem using class GenericTest from comment
> 0 - it runs fine for me. So, how exactly is it failing for you?

Thanks for the fast reply. The steps I use to reproduce are:

* Download fresh eclipse-jee-mars-R-macosx-cocoa-x86_64
* Extract
* Open a fresh workspace
* File->Import->Existing Maven Project
* In Project Explorer navigate to GenericTest
* Right click Generic Test in Project Explorer and try to select Run As->JUnit Test (it is missing). I have added a screenshot of this.

NOTE: I should have mentioned that the key bindings still work.
Comment 8 Eclipse Genie CLA 2019-09-28 02:06:50 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.