| Summary: | [JUnit] Cannot run JUnit tests against Generic Test | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Rob Winch <rwinch> | ||||||
| Component: | UI | Assignee: | 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: |
|
||||||||
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? *** Bug 462854 has been marked as a duplicate of this bug. *** (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. (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. (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? 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
(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. 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. |
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.