Running JUnit tests from plugin - 'java 1.7' is bad configured exception

Hi,

I'm trying to run JUnit tests from my plugin tests. The code is below. When I run those I get following exception. Any ideas what I'm doing wrong?

java.lang.AssertionError: Error running SimpleTest:<br>'java 1.7' is bad configured
    at com.intellij.testFramework.LoggedErrorProcessor$1.processError(LoggedErrorProcessor.java:35)
    at com.intellij.testFramework.TestLogger.error(TestLogger.java:53)
    at com.intellij.openapi.diagnostic.Logger.error(Logger.java:113)
    at com.intellij.execution.runners.ExecutionUtil.handleExecutionError(ExecutionUtil.java:92)
    at com.intellij.execution.runners.ExecutionUtil.handleExecutionError(ExecutionUtil.java:52)
    at com.intellij.execution.impl.ExecutionManagerImpl$2.run(ExecutionManagerImpl.java:227)
    at com.intellij.execution.impl.ExecutionManagerImpl.startRunProfile(ExecutionManagerImpl.java:239)
    at com.intellij.execution.impl.ExecutionManagerImpl.startRunProfile(ExecutionManagerImpl.java:255)
    at com.intellij.execution.runners.GenericProgramRunner.execute(GenericProgramRunner.java:77)
    at com.intellij.execution.runners.GenericProgramRunner.execute(GenericProgramRunner.java:61)
    at com.intellij.execution.ProgramRunnerUtil.executeConfiguration(ProgramRunnerUtil.java:138)
    at com.intellij.execution.ProgramRunnerUtil.executeConfiguration(ProgramRunnerUtil.java:73)
    at com.intellij.execution.ProgramRunnerUtil.executeConfiguration(ProgramRunnerUtil.java:153)
    at com.test.SimpleTest.testSimple(SimpleTest.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.intellij.testFramework.UsefulTestCase.access$001(UsefulTestCase.java:75)
    at com.intellij.testFramework.UsefulTestCase$2.run(UsefulTestCase.java:306)



Test code:

public class SimpleTestInPlugin extends LightCodeInsightFixtureTestCase {

    @Override
    protected String getTestDataPath() {
        return "testData";
    }

//
//    @NotNull
//    @Override
//    protected LightProjectDescriptor getProjectDescriptor() {
//        return JAVA_1_7;
//    }

    public void testSimple() {
        PsiFile testFile = myFixture.configureByFile("SimpleTest.java");
        PsiClass testClass = null;

        if(testFile instanceof PsiJavaFile) {
            PsiJavaFile javaFile = (PsiJavaFile) testFile;
            testClass = javaFile.getClasses()[0];
        }

        RunManager runManager = RunManager.getInstance(getProject());

        RunnerAndConfigurationSettings runnerAndConfigurationSettings = runManager.createRunConfiguration(
                "sampleConfig", JUnitConfigurationType.getInstance().getConfigurationFactories()[0]);


        JUnitConfiguration tdaConfiguration = (JUnitConfiguration) runnerAndConfigurationSettings.getConfiguration();

        tdaConfiguration.getPersistentData().TEST_OBJECT = JUnitConfiguration.TEST_CLASS;
        tdaConfiguration.getPersistentData().setMainClass(testClass);
        tdaConfiguration.getPersistentData().setScope(TestSearchScope.WHOLE_PROJECT);
        tdaConfiguration.beClassConfiguration(testClass);

        tdaConfiguration.setModule(myModule);

        tdaConfiguration.setVMParameters("-ea");
        //tdaConfiguration.setWorkingDirectory("X:\\");

        Executor executor = DefaultRunExecutor.getRunExecutorInstance();
        ProgramRunnerUtil.executeConfiguration(getProject(), runnerAndConfigurationSettings, executor);
    }
}


SimpleTest.java (test executed by plugin test):


public class SimpleTest {

    @org.junit.Test
    public void testA() throws Exception {
    }

}

5 comments
Comment actions Permalink

Hi globtroter,

it is a bit confusing that your Test code class and the class that should get Tested have the same name.

And I don't know what you would like to achieve with:

PsiFile testFile = myFixture.configureByFile("SimpleTest.java");


Maybe you could explain a little more details.

Markus

0
Comment actions Permalink

Hi Markus,

I've just renamed tests to be more self-explanatory. My plugin monitors test execution and acts on it. To test the plugin I wrote the SimpleTestInPlugin test. It copies SimpleTest.java (from testData) into virtual project (is it ok to copy it to src/ instead of tst/?) and runs it (testA() method). Does that answer your question?

0
Comment actions Permalink

is it ok to copy it to src/ instead of tst/?)

actually I don't know the answer.

But I see some weird code:

tdaConfiguration.beClassConfiguration(testClass);

This line of code overwrites these two lines:

 tdaConfiguration.getPersistentData().TEST_OBJECT = JUnitConfiguration.TEST_CLASS;
        tdaConfiguration.getPersistentData().setMainClass(testClass);

Another thing you could try is to set the name of the Testclass to SimpleTest istead of SimpleTest.java:

PsiFile testFile = myFixture.configureByFile("SimpleTest.java");
tdaConfiguration.getPersistentData().setMainClass(
"SimpleTest"
);


You could also deactivate the Assertions with:

//tdaConfiguration.setVMParameters("-ea");

But this could be not smart if you want to test something.

I can't see where you do the copy action of the SimpleTest class

Markus

0
Comment actions Permalink

I've tried all of these to no avail (same exception occurs). Any other ideas?

@Markus: could you possibly share a full piece of code that you're using to run the tests from the other thread?

0
Comment actions Permalink

package analyzer.ui;

import analyzer.testexecution.TDAConfiguration;
import analyzer.testexecution.TDAConfigurationType;
import analyzer.util.Annotations;
import com.intellij.execution.Executor;
import com.intellij.execution.JavaExecutionUtil;
import com.intellij.execution.ProgramRunnerUtil;
import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.execution.junit.JUnitConfiguration;
import com.intellij.execution.junit.JUnitUtil;
import com.intellij.execution.testframework.TestSearchScope;
import com.intellij.ide.util.DefaultPsiElementCellRenderer;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.FileIndexFacade;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiImportList;
import com.intellij.psi.PsiImportStatementBase;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.impl.java.stubs.index.JavaStubIndexKeys;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StubIndexImpl;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class Dialog extends DialogWrapper {
    public static final String VM_PARAMETER_ENABLE_ASSERTIONS = "-ea";

    private final LabeledComponent<JPanel> component;
    private Set<PsiClass> selectedElements = new HashSet<PsiClass>();
    private DialogWrapperAction runAction;
    private Project project;

    public Dialog(Project project, CollectionListModel<PsiClass> testClasses) {
        super(project);
        this.project = project;
        setTitle("Dependent Tests");

        initializeRunAction();

        JBList testClassList = new JBList(testClasses);
        testClassList.addListSelectionListener(new DialogListSelectionListener(project, testClassList));

        testClassList.addMouseListener(new DialogMouseAdapter(project, testClasses));
        testClassList.setCellRenderer(new DefaultPsiElementCellRenderer());
        ToolbarDecorator toolbarDecorator = ToolbarDecorator.createDecorator(testClassList);
        toolbarDecorator.disableAddAction();

        JLabel label = new JLabel();
        int size = testClasses.getSize();
        int totalTestNumber = getTotalTestNumber();
        int totalWebDriverTestNumber = getTotalWebDriverTestNumber();

        int webdriverTestCount = 0;

        for (PsiClass psiClass : testClasses.getItems()) {
            PsiJavaFile psiJavaFile = (PsiJavaFile) psiClass.getContainingFile();
            PsiImportList importList = psiJavaFile.getImportList();

            if (importList != null) {
                PsiImportStatementBase singleImportStatement = importList.findSingleClassImportStatement("importstring");
                if (singleImportStatement != null) {
                    webdriverTestCount++;
                }
            }
        }

        label.setText("Testcounter: " + String.valueOf(size) + "/" + String.valueOf(totalTestNumber) + "  WebDriverTests: " + String.valueOf(webdriverTestCount) + "/" + String.valueOf(totalWebDriverTestNumber));

        JPanel panel = toolbarDecorator.createPanel();
        panel.add(label, BorderLayout.SOUTH);
        component = LabeledComponent.create(panel, "Tests that must be executed");
        this.setModal(false);
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        return component;
    }

    @NotNull
    @Override
    protected Action[] createActions() {
        return new Action[]{runAction, getCancelAction()};
    }

    private int getTotalTestNumber() {
        Collection<PsiAnnotation> psiAnnotations = StubIndexImpl.getInstance().get(JavaStubIndexKeys.ANNOTATIONS, Annotations.TEST, project, GlobalSearchScope.allScope(project));

        Set<PsiClass> allClasses = new HashSet<PsiClass>();

        for (PsiAnnotation psiAnnotation : psiAnnotations) {
            if (psiAnnotation.getContainingFile() instanceof PsiJavaFile) {
                PsiJavaFile psiJavaFile = (PsiJavaFile) psiAnnotation.getContainingFile();
                if (!psiJavaFile.getFileType().isBinary()) {
                    if (JUnitUtil.isTestClass(psiJavaFile.getClasses()[0])) {
                        allClasses.add(psiJavaFile.getClasses()[0]);
                    }
                }
            }
        }

        return allClasses.size();
    }

    private int getTotalWebDriverTestNumber() {
        Collection<PsiAnnotation> webDriverAnnotations = StubIndexImpl.getInstance().get(JavaStubIndexKeys.ANNOTATIONS, Annotations.BROWSER, project, GlobalSearchScope.allScope(project));
        Set<PsiClass> webDriverClasses = new HashSet<PsiClass>();

        for (PsiAnnotation webDriverAnnotation : webDriverAnnotations) {
            if (webDriverAnnotation.getContainingFile() instanceof PsiJavaFile) {
                PsiJavaFile psiJavaFile = (PsiJavaFile) webDriverAnnotation.getContainingFile();
                if (!psiJavaFile.getFileType().isBinary()) {
                    webDriverClasses.add(psiJavaFile.getClasses()[0]);
                }
            }
        }

        return webDriverClasses.size();
    }

    private void initializeRunAction() {
        runAction = new DialogWrapperAction("Run") {
            @Override
            protected void doAction(ActionEvent actionEvent) {
                VirtualFile virtualFile = selectedElements.iterator().next().getContainingFile().getVirtualFile();
                Module module = FileIndexFacade.getInstance(project).getModuleForFile(virtualFile);

                Set<String> testClassPattern = new HashSet<String>();

                for (PsiClass selectedElement : selectedElements) {
                    String qualifiedName = JavaExecutionUtil.getRuntimeQualifiedName(selectedElement);
                    testClassPattern.add(qualifiedName);
                }

                if (isEnabled()) {
                    RunManager runManager = RunManager.getInstance(project);

                    RunnerAndConfigurationSettings runnerAndConfigurationSettings = runManager.createRunConfiguration(module.toString(), new TDAConfigurationType().getConfigurationFactories()[0]);
                    TDAConfiguration tdaConfiguration = (TDAConfiguration) runnerAndConfigurationSettings.getConfiguration();

                    tdaConfiguration.getPersistentData().TEST_OBJECT = JUnitConfiguration.TEST_PATTERN;
                    tdaConfiguration.getPersistentData().setPatterns(testClassPattern);
                    tdaConfiguration.getPersistentData().setScope(TestSearchScope.WHOLE_PROJECT);

                    tdaConfiguration.setModule(module);

                    tdaConfiguration.setVMParameters(VM_PARAMETER_ENABLE_ASSERTIONS);
                    tdaConfiguration.setWorkingDirectory(project.getBasePath());

                    Executor executor = DefaultRunExecutor.getRunExecutorInstance();
                    ProgramRunnerUtil.executeConfiguration(project, runnerAndConfigurationSettings, executor);
                }
            }
        };
    }

    private class DialogMouseAdapter extends MouseAdapter {
        private final Project project;
        private CollectionListModel<PsiClass> testClasses;

        public DialogMouseAdapter(Project project, CollectionListModel<PsiClass> testClasses) {
            this.project = project;
            this.testClasses = testClasses;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            int clickCount = e.getClickCount();
            JBList list = (JBList) e.getSource();
            if (clickCount >= 2) {
                FileEditorManager.getInstance(project).openFile(testClasses.getElementAt(list.getSelectedIndex()).getContainingFile().getVirtualFile(), true);
            }
        }
    }

    private class DialogListSelectionListener implements ListSelectionListener {
        private final Project project;
        private JBList testClassList;

        public DialogListSelectionListener(Project project, JBList testClassList) {
            this.project = project;
            this.testClassList = testClassList;
        }

        @Override
        public void valueChanged(ListSelectionEvent e) {
            Module previousModule = null;
            runAction.setEnabled(true);
            selectedElements.clear();

            for (Object object : testClassList.getSelectedValues()) {
                if (object instanceof PsiClass) {
                    Module currentModule = FileIndexFacade.getInstance(project).getModuleForFile(((PsiClass) object).getContainingFile().getVirtualFile());

                    if (previousModule == null) {
                        previousModule = currentModule;
                    }

                    if (previousModule != currentModule) {
                        runAction.setEnabled(false);
                    }

                    selectedElements.add((PsiClass) object);
                }
            }
        }
    }
}


This is the whole class which executes the tests I selected in a Dialog. It is implemented as an Listener for a Button in the Dialog itself. I hope this helps you.

0

Please sign in to leave a comment.