junit: insert test methods inside nested class when @Nested annotated

This commit is contained in:
Anna Kozlova
2016-09-19 11:16:59 +03:00
parent cbbf8ec487
commit 20c9e143bf
7 changed files with 149 additions and 36 deletions
@@ -81,6 +81,7 @@ public class JUnitUtil {
@NonNls public static final String PARAMETERIZED_CLASS_NAME = "org.junit.runners.Parameterized";
@NonNls public static final String SUITE_CLASS_NAME = "org.junit.runners.Suite";
public static final String JUNIT5_NESTED = "org.junit.jupiter.api.Nested";
public static boolean isSuiteMethod(@NotNull PsiMethod psiMethod) {
if (!psiMethod.hasModifierProperty(PsiModifier.PUBLIC)) return false;
@@ -200,6 +201,10 @@ public class JUnitUtil {
final PsiModifierList modifierList = psiClass.getModifierList();
if (modifierList == null) return false;
if (psiClass.getContainingClass() != null && AnnotationUtil.isAnnotated(psiClass, JUNIT5_NESTED, false)) {
return true;
}
if (!PsiClassUtil.isRunnableClass(psiClass, false, checkAbstract)) return false;
for (final PsiMethod method : psiClass.getAllMethods()) {
@@ -207,6 +212,10 @@ public class JUnitUtil {
if (AnnotationUtil.isAnnotated(method, TEST5_ANNOTATIONS)) return true;
}
for (PsiClass aClass : psiClass.getInnerClasses()) {
if (AnnotationUtil.isAnnotated(aClass, JUNIT5_NESTED, false)) return true;
}
return false;
}
@@ -96,7 +96,17 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
private static PsiClass findTargetClass(@NotNull Editor editor, @NotNull PsiFile file) {
int offset = editor.getCaretModel().getOffset();
PsiElement element = file.findElementAt(offset);
return PsiTreeUtil.getParentOfType(element, PsiClass.class, false) == null ? null : TestIntegrationUtils.findOuterClass(element);
PsiClass containingClass = PsiTreeUtil.getParentOfType(element, PsiClass.class, false);
if (containingClass == null) {
return null;
}
final List<TestFramework> frameworks = TestIntegrationUtils.findSuitableFrameworks(containingClass);
for (TestFramework framework : frameworks) {
if (framework instanceof JavaTestFramework && ((JavaTestFramework)framework).acceptNestedClasses()) {
return containingClass;
}
}
return TestIntegrationUtils.findOuterClass(element);
}
@Override
@@ -155,10 +165,10 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
.createPopup().showInBestPositionFor(editor);
}
private static class MyHandler implements CodeInsightActionHandler {
public static class MyHandler implements CodeInsightActionHandler {
private final TestIntegrationUtils.MethodKind myMethodKind;
private MyHandler(TestIntegrationUtils.MethodKind methodKind) {
public MyHandler(TestIntegrationUtils.MethodKind methodKind) {
myMethodKind = methodKind;
}
@@ -206,7 +216,7 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
WriteCommandAction.runWriteCommandAction(file.getProject(), () -> {
try {
PsiDocumentManager.getInstance(file.getProject()).commitAllDocuments();
PsiMethod method = generateDummyMethod(editor, file);
PsiMethod method = generateDummyMethod(file, editor, targetClass);
if (method == null) return;
TestIntegrationUtils.runTestMethodTemplate(myMethodKind, framework, editor, targetClass, method, "name", false, null);
@@ -219,21 +229,24 @@ public class BaseGenerateTestSupportMethodAction extends BaseGenerateAction {
}
@Nullable
private static PsiMethod generateDummyMethod(Editor editor, PsiFile file) throws IncorrectOperationException {
private static PsiMethod generateDummyMethod(PsiFile file, Editor editor, PsiClass targetClass) throws IncorrectOperationException {
final PsiMethod method = TestIntegrationUtils.createDummyMethod(file);
final PsiGenerationInfo<PsiMethod> info = OverrideImplementUtil.createGenerationInfo(method);
int offset = findOffsetToInsertMethodTo(editor, file);
int offset = findOffsetToInsertMethodTo(editor, file, targetClass);
GenerateMembersUtil.insertMembersAtOffset(file, offset, Collections.singletonList(info));
final PsiMethod member = info.getPsiMember();
return member != null ? CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(member) : null;
}
private static int findOffsetToInsertMethodTo(Editor editor, PsiFile file) {
private static int findOffsetToInsertMethodTo(Editor editor, PsiFile file, PsiClass targetClass) {
int result = editor.getCaretModel().getOffset();
PsiClass classAtCursor = PsiTreeUtil.getParentOfType(file.findElementAt(result), PsiClass.class, false);
if (classAtCursor == targetClass) {
return result;
}
while (classAtCursor != null && !(classAtCursor.getParent() instanceof PsiFile)) {
result = classAtCursor.getTextRange().getEndOffset();
@@ -162,4 +162,7 @@ public abstract class JavaTestFramework implements TestFramework {
}
public boolean acceptNestedClasses() {
return false;
}
}
@@ -134,6 +134,11 @@ public class JUnit5Framework extends JavaTestFramework {
return JUnitUtil.isTestMethod(MethodLocation.elementInClass(method, myClass));
}
@Override
public boolean acceptNestedClasses() {
return true;
}
public FileTemplateDescriptor getSetUpMethodFileTemplateDescriptor() {
return new FileTemplateDescriptor("JUnit5 SetUp Method.java");
}
@@ -21,15 +21,8 @@ import com.intellij.execution.junit.JUnitConfiguration;
import com.intellij.execution.junit.JUnitUtil;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.testFramework.EdtTestUtil;
import com.intellij.testFramework.TestRunnerUtil;
import com.intellij.testFramework.fixtures.*;
import com.intellij.testFramework.fixtures.impl.LightTempDirTestFixtureImpl;
import com.intellij.testIntegration.TestFramework;
import com.intellij.util.ThrowableRunnable;
import one.util.streamex.StreamEx;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
@@ -39,23 +32,7 @@ import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
class JUnit5AcceptanceTest {
private JavaCodeInsightTestFixture myFixture;
@BeforeEach
void setUp() throws Exception {
IdeaTestFixtureFactory factory = IdeaTestFixtureFactory.getFixtureFactory();
TestFixtureBuilder<IdeaProjectTestFixture> fixtureBuilder = factory.createLightFixtureBuilder(new DefaultLightProjectDescriptor());
final IdeaProjectTestFixture fixture = fixtureBuilder.getFixture();
myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(fixture, new LightTempDirTestFixtureImpl(true));
myFixture.setUp();
}
@AfterEach
void tearDown() throws Exception {
myFixture.tearDown();
}
class JUnit5AcceptanceTest extends JUnit5CodeInsightTest {
@Test
void testFactoryMethods() {
@@ -127,9 +104,4 @@ class JUnit5AcceptanceTest {
});
}
private static void doTest(ThrowableRunnable<Throwable> run) {
TestRunnerUtil.replaceIdeEventQueueSafely();
EdtTestUtil.runInEdtAndWait(run);
}
}
@@ -0,0 +1,47 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.junit5;
import com.intellij.testFramework.EdtTestUtil;
import com.intellij.testFramework.TestRunnerUtil;
import com.intellij.testFramework.fixtures.*;
import com.intellij.testFramework.fixtures.impl.LightTempDirTestFixtureImpl;
import com.intellij.util.ThrowableRunnable;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
class JUnit5CodeInsightTest {
protected JavaCodeInsightTestFixture myFixture;
@BeforeEach
void setUp() throws Exception {
IdeaTestFixtureFactory factory = IdeaTestFixtureFactory.getFixtureFactory();
TestFixtureBuilder<IdeaProjectTestFixture> fixtureBuilder = factory.createLightFixtureBuilder(new DefaultLightProjectDescriptor());
final IdeaProjectTestFixture fixture = fixtureBuilder.getFixture();
myFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(fixture, new LightTempDirTestFixtureImpl(true));
myFixture.setUp();
}
@AfterEach
void tearDown() throws Exception {
myFixture.tearDown();
}
protected static void doTest(ThrowableRunnable<Throwable> run) {
TestRunnerUtil.replaceIdeEventQueueSafely();
EdtTestUtil.runInEdtAndWait(run);
}
}
@@ -0,0 +1,64 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.junit5;
import com.intellij.testIntegration.BaseGenerateTestSupportMethodAction;
import com.intellij.testIntegration.TestIntegrationUtils;
import org.junit.jupiter.api.Test;
class JUnit5GenerationTest extends JUnit5CodeInsightTest {
@Test
void testMethodInTopLevelClass() {
doTest("import org.junit.jupiter.api.Test; class MyTest {<caret> @Test void m2(){}}",
"import org.junit.jupiter.api.Test; class MyTest {\n" +
" @Test\n" +
" void name() {\n" +
" \n" +
"\n" +
" }\n" +
"\n" +
" @Test void m2(){}}");
}
@Test
void testMethodInNestedClass() {
doTest("import org.junit.jupiter.api.Nested; class MyTest { @Nested class NTest { <caret>}}",
"import org.junit.jupiter.api.Nested;\n" +
"import org.junit.jupiter.api.Test;\n" +
"\n" +
"class MyTest { @Nested class NTest {\n" +
" @Test\n" +
" void name() {\n" +
" \n" +
"\n" +
" }\n" +
"}}");
}
private void doTest(String text, String expected) {
doTest(() -> {
myFixture.addClass("package org.junit.jupiter.api; public @interface Test {}");
myFixture.addClass("package org.junit.jupiter.api; public @interface Nested {}");
myFixture.configureByText("MyTest.java", text);
new BaseGenerateTestSupportMethodAction.MyHandler(TestIntegrationUtils.MethodKind.TEST).invoke(myFixture.getProject(),
myFixture.getEditor(),
myFixture.getFile());
myFixture.checkResult(expected);
}
);
}
}