Bug 531779 - [JUnit] Provide a helper to know if given IMethod is a JUnit test method
Summary: [JUnit] Provide a helper to know if given IMethod is a JUnit test method
Status: CLOSED WONTFIX
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:
Depends on:
Blocks: 529316
  Show dependency tree
 
Reported: 2018-02-28 04:26 EST by Angelo ZERR CLA
Modified: 2020-10-13 05:51 EDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Angelo ZERR CLA 2018-02-28 04:26:16 EST
In CodeMining/CodeLens context, we need to know if a given IMethod is a JUnit test method or not to drawn codemining/codelens.

Today vscode-java-test (JUnit CodeLens inside VSCode) and jdt-codemining (JUnit CodeMining inside Eclipse IDE) checks if it exists an @test annotation before the method:

 * for vscode-java-test (see isTestMethod): https://github.com/Microsoft/vscode-java-test/search?utf8=%E2%9C%93&q=isTestMethod&type=
 * for jdt-codemining (see isTestMethod): https://github.com/angelozerr/jdt-codemining/blob/master/org.eclipse.jdt.codemining/src/org/eclipse/jdt/junit/codemining/JUnitCodeMiningProvider.java#L130

It seems that it with JUnit4 but not with JUnit5 (@Sarika, could add a sample with JUnit5 which doesn't work please).


To fix this problem, it should be  cool if we could have an helper like isTestMethod(IMethod).

An idea is to add a new method isTestMethod(IMethod method) in the existing interface org.eclipse.jdt.internal.junit.launcher.ITestFinder:

------------------------------------------------------
public interface ITestFinder {

 ...
 boolean isTestMethod(IMethod method) throws CoreException;
}
------------------------------------------------------

Each JUnit version (JUnit3TestFinder, JUnit4TestFinder, JUnit5TestFinder) will implement their logic to know if a given method is a JUnit test method by implementing isTestMethod(IMethod method).

After we could do that:

------------------------------------------------------
IMethod method = ...
ITestKind testKind= TestKindRegistry.getContainerTestKind(method.getParent());
boolean isTestMethod = testKind.getFinder().isTestMethod(method);
------------------------------------------------------
Comment 1 Sarika Sinha CLA 2018-02-28 04:32:32 EST
JDT Core is totally unaware of JUnit so this can be provided by JDT UI (if possible).
Comment 2 Noopur Gupta CLA 2018-03-08 04:59:05 EST
It is the responsibility of JUnit 5 framework to discover all the test methods and hence it is not required in JDT. 

If you need this for code minings then you can refer the implementations of ITestFinder.isTest(IType type) which check for a test method also internally. Example: 
JUnit5TestFinder.Annotation.annotatesAtLeastOneMethod(ITypeBinding type).
Comment 3 Eclipse Genie CLA 2020-10-12 03:45:24 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.
Comment 4 Kalyan Prasad Tatavarthi CLA 2020-10-13 05:51:43 EDT
Reopen the bug if you are planning to work on it.