Run the generated unit tests from the dsl test programmatically [message #1843413] |
Wed, 28 July 2021 09:26 |
Loredana Hozan Messages: 34 Registered: January 2019 |
Member |
|
|
Hello,
in our company we have a dsls implemented with Xtext, the test language is designed so the user will be able to write unit tests for the other dsl languages. A test file generates a java test class containing junit tests. I am writing an eclipse rcp application from which the user has the option to run the junit tests from the test file instead of the generated java file. I have implemented a ILaunchShortcut2 class which will run the tests. If the user selects the test file from the project explorer and does Run As> DSL Test file , using org.eclipse.xtext.xbase.ui.launching.JUnitLaunchShortcut in the following code, it works as expected, all the tests from the generated java file run:
public void launch(ISelection selection, String mode) {
if(selection instanceof TreeSelection && ((TreeSelection)selection).getFirstElement() instanceof IFile) {
//code to get the generated java file using the test file selection
ICompilationUnit javaFile = (ICompilationUnit) JavaCore.create(testClassFile);
Object[] segments = {javaFile};
TreePath treePath = new TreePath(segments);
TreeSelection javaFileSelection= new TreeSelection(treePath);
JUnitLaunchShortcut jUnitLaunchShortcut = new JUnitLaunchShortcut();
jUnitLaunchShortcut.launch((ISelection)javaFileSelection, mode);
}
However, when i try to run the tests/or single test from the dsl test editor, i get a dialog with the issue "No JUnit tests found", no logs registered. Here is my code:
public void launch(IEditorPart editor, String mode) {
if(editor instanceof XtextEditor) {
//code to get the generated java file using the test editor
ICompilationUnit javaFile = (ICompilationUnit) JavaCore.create(testClassFile);
try {
CompilationUnitEditor javaEditor = (CompilationUnitEditor) JavaUI.openInEditor(javaFile);
JavaElementDelegate javaElementDelegate =editor.getAdapter(JavaElementDelegateJunitLaunch.class);
//other types of implementation
//JavaElementDelegate javaElementDelegate = Adapters.adapt(editor, JavaElementDelegateJunitLaunch.class);
//JavaElementDelegate javaElementDelegate = Adapters.adapt(javaEditor, JavaElementDelegateJunitLaunch.class);
//JavaElementDelegate javaElementDelegate = javaEditor.getAdapter(JavaElementDelegateJunitLaunch.class);
JUnitLaunchShortcut jUnitLaunchShortcut = new JUnitLaunchShortcut();
jUnitLaunchShortcut.launch(new StructuredSelection(javaElementDelegate), mode);
//other types of implementation
//jUnitLaunchShortcut.launch(editor, mode);
//jUnitLaunchShortcut.launch(javaEditor, mode);
} catch (PartInitException | JavaModelException e) {
e.printStackTrace();
}
}
}
I also tried resolve this by creating an eclipse launch configuration by using org.eclipse.debug.internal.core.LaunchConfiguration which is what the JUnit code is calling when you run a junit test from java, but i came upon many restrictions and protected methods, so that's out of the way.
Any idea on what might be wrong or other type of implementation i could do to let the user run a single test method from a dsl test file?
|
|
|
|
Powered by
FUDForum. Page generated in 0.01656 seconds