1717import java .util .LinkedHashSet ;
1818
1919import org .eclipse .core .resources .IFile ;
20+ import org .eclipse .core .runtime .NullProgressMonitor ;
2021import org .eclipse .jdt .core .ICompilationUnit ;
2122import org .eclipse .jdt .core .IJavaElement ;
2223import org .eclipse .jdt .core .IJavaProject ;
2324import org .eclipse .jdt .core .IMember ;
2425import org .eclipse .jdt .core .IMethod ;
2526import org .eclipse .jdt .core .IType ;
2627import org .eclipse .jdt .core .JavaCore ;
28+ import org .eclipse .jdt .internal .ui .javaeditor .EditorUtility ;
2729import org .eclipse .ui .IEditorPart ;
2830import org .moreunit .actions .RunTestAction ;
2931import org .moreunit .actions .RunTestFromCompilationUnitAction ;
@@ -99,6 +101,7 @@ public void executeRunTestAction(ICompilationUnit compilationUnit, String launch
99101
100102 private void executeRunAllTestsAction (ICompilationUnit compilationUnit , String launchMode )
101103 {
104+ saveIfNeeded (compilationUnit );
102105 Jobs .waitForIndexExecuteAndRunInUI ("Running tests ... " , () -> {
103106
104107 Collection <IType > testCases = new LinkedHashSet <>();
@@ -135,6 +138,15 @@ private void executeRunAllTestsAction(ICompilationUnit compilationUnit, String l
135138 }, testCases -> runTests (testCases , launchMode ));
136139 }
137140
141+ private void saveIfNeeded (ICompilationUnit compilationUnit )
142+ {
143+ IEditorPart editorPart = EditorUtility .isOpenInEditor (compilationUnit );
144+ if (editorPart != null && editorPart .isDirty ())
145+ {
146+ editorPart .doSave (new NullProgressMonitor ());
147+ }
148+ }
149+
138150 public void executeRunTestsOfSelectedMemberAction (IEditorPart editorPart , String launchMode )
139151 {
140152 ICompilationUnit compilationUnit = createCompilationUnitFrom (editorPart );
@@ -143,6 +155,7 @@ public void executeRunTestsOfSelectedMemberAction(IEditorPart editorPart, String
143155
144156 private void executeRunTestsOfSelectedMemberAction (IEditorPart editorPart , ICompilationUnit compilationUnit , String launchMode )
145157 {
158+ saveIfNeeded (compilationUnit );
146159 Jobs .waitForIndexExecuteAndRunInUI ("Running tests ... " , () -> {
147160 Collection <IMember > testElements = new LinkedHashSet <>();
148161 IType selectedJavaType = compilationUnit .findPrimaryType ();
0 commit comments