Bug 529722 - [JUnit 5] Cannot launch tests whose name doesn't match the compilation unit
Summary: [JUnit 5] Cannot launch tests whose name doesn't match the compilation unit
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.8   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
: 552537 (view as bug list)
Depends on:
Blocks:
 
Reported: 2018-01-12 07:36 EST by Andreas Sewe CLA
Modified: 2023-12-30 02:46 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Sewe CLA 2018-01-12 07:36:19 EST
As test classes don't have to be public with JUnit 5 anymore, the class name == compilation unit name restriction has been lifted as well. This allows 'fancy' tests like the following, stored in "ExampleTest.java":

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

interface ExampleTest {
	
	@Test
	default void test() {
		assertNotNull(getInput());
	}

	Object getInput();
}

class ObjectScenario implements ExampleTest {

	@Override
	public Object getInput() {
		return new Object();
	}
}

class StringScenario implements ExampleTest {

	@Override
	public Object getInput() {
		return new String();
	}
}

Unfortunately, JDT (tested with Photon M4) doesn't handle them very well.

1. Run As > JUnit Test on the containing package: Executes both ObjectScenario and StringScenario (as expected)

2. Run As > JUnit Test on the ExampleTest.java compilation unit: Opens a test selection dialog for ObjectScenario and StringScenario. Selecting either results in a "The input type of the launch configuration does not exist" error.

3. Run As > JUnit Test on the ObjectScenario type: "The input type of the launch configuration does not exist" error again.

IMHO, it would be nice if option 2 would simply execute both tests without asking (thus allowing users to easily their group tests by placing them in the same compilation unit.

Also, option 3 should just work. After all, JDT can execute the tests just fine, as option 1 shows.
Comment 1 Noopur Gupta CLA 2018-01-19 06:18:16 EST
This happens because the IJavaProject#findType APIs being used everywhere in the jdt.ui code for JUnit do not consider the secondary types (types having names different than their compilation units). Example: JUnitLaunchConfigurationTab.getResolvedType(..).

There are other IJavaProject#findType APIs available which can be used to include secondary types as well. But using those APIs will have bad performance issues and hence their use is discouraged. 

If this issue turns out to be a problem in regular usage by multiple users then we may consider using the other APIs for JUnit 5 only. 

The normal use case should generally have a single test class per compilation unit with the same name (with or without 'public').
Comment 2 Andreas Sewe CLA 2018-01-19 06:58:36 EST
(In reply to Noopur Gupta from comment #1)
> If this issue turns out to be a problem in regular usage by multiple users
> then we may consider using the other APIs for JUnit 5 only. 

Fair enough.
Comment 3 Noopur Gupta CLA 2019-10-30 05:38:02 EDT
*** Bug 552537 has been marked as a duplicate of this bug. ***
Comment 4 Anthony Vanelverdinghe CLA 2020-01-17 04:24:47 EST
This is a problem for me and several other users that use VS Code, see [1].

[1] https://github.com/microsoft/vscode-java-test/issues/859
Comment 5 Eclipse Genie CLA 2022-01-07 15:15:39 EST
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.
Comment 6 Eclipse Genie CLA 2023-12-30 02:46:14 EST
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.