Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Run the generated unit tests from the dsl test programmatically
Run the generated unit tests from the dsl test programmatically [message #1843413] Wed, 28 July 2021 09:26 Go to next message
Loredana Hozan is currently offline Loredana HozanFriend
Messages: 32
Registered: January 2019
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) {

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?
Re: Run the generated unit tests from the dsl test programmatically [message #1843414 is a reply to message #1843413] Wed, 28 July 2021 09:37 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14431
Registered: July 2009
Senior Member
i have no idea. maybe you can debug what
org.eclipse.xtext.xbase.ui.launching.JUnitLaunchShortcut.launch(IEditorPart, String)
does when you debug an xtend test from editor.

Need professional support for Xtext, Xpand, EMF?
Go to:
Twitter : @chrdietrich
Blog :
Previous Topic:Two DSLs or two-tier-build?
Next Topic:Xpand or Xtext function NOCODE
Goto Forum:

Current Time: Wed Feb 01 16:49:15 GMT 2023

Powered by FUDForum. Page generated in 0.04467 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top