wrdv / testme-idea

TestMe IntelliJ IDEA Plugin - Auto generates Unit Test code
http://weirddev.com/testme
Other
170 stars 63 forks source link

issues with test generation for single method #38

Closed yaronyam closed 4 months ago

yaronyam commented 4 months ago

Found some issues with test generation for single method

1st issue A. try to generate UT for method (cursor on method) that already has a UT that is not named as <className>Test - error alert pops with NPE - PSB stack trace. empty test class generated B. try to generate UT for method (cursor on method) that already has an empty UT class (without any methods or imports) getting error alert

2024-05-20 22:09:00,087 [  50197] SEVERE - com.weirddev.testme.intellij.generator.TestMeGenerator - error generating test class
java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:209)
    at java.base/java.util.ImmutableCollections$List12.<init>(ImmutableCollections.java:556)
    at java.base/java.util.List.of(List.java:812)
    at com.weirddev.testme.intellij.utils.TestFileUpdateUtil.getPsiTestFile(TestFileUpdateUtil.java:39)
    at com.weirddev.testme.intellij.utils.TestFileUpdateUtil.generateOrUpdateTestFile(TestFileUpdateUtil.java:53)
    at com.weirddev.testme.intellij.generator.TestMeGenerator.createTestClassFromCodeTemplate(TestMeGenerator.java:140)
    at com.weirddev.testme.intellij.generator.TestMeGenerator.createTestClass(TestMeGenerator.java:110)
    at com.weirddev.testme.intellij.generator.TestMeGenerator$1$1.compute(TestMeGenerator.java:66)
    at com.weirddev.testme.intellij.generator.TestMeGenerator$1$1.compute(TestMeGenerator.java:61)
    at com.intellij.openapi.application.impl.ApplicationImpl.lambda$runWriteAction$9(ApplicationImpl.java:1001)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteActionWithClass(ApplicationImpl.java:980)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1001)
    at com.weirddev.testme.intellij.generator.TestMeGenerator$1.compute(TestMeGenerator.java:61)
    at com.weirddev.testme.intellij.generator.TestMeGenerator$1.compute(TestMeGenerator.java:59)
    at com.intellij.psi.impl.source.PostprocessReformattingAspectImpl.postponeFormattingInside(PostprocessReformattingAspectImpl.java:149)
    at com.weirddev.testme.intellij.generator.TestMeGenerator.generateTest(TestMeGenerator.java:59)
    at com.weirddev.testme.intellij.action.CreateTestMeAction$1.run(CreateTestMeAction.java:142)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:219)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:174)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:164)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:150)
    at com.weirddev.testme.intellij.action.CreateTestMeAction.invoke(CreateTestMeAction.java:139)
    at com.weirddev.testme.intellij.action.TestMeCreator.invoke(TestMeCreator.java:35)
    at com.weirddev.testme.intellij.action.TestMeCreator.createTest(TestMeCreator.java:25)
    at com.weirddev.testme.intellij.action.TestMeAdditionalAction.execute(TestMeAdditionalAction.java:49)
    at com.weirddev.testme.intellij.ui.popup.TestMePopUpHandler.lambda$show$1(TestMePopUpHandler.java:99)
    at com.intellij.openapi.ui.popup.PopupChooserBuilder.lambda$setItemChosenCallback$0(PopupChooserBuilder.java:207)

2nd issue added test method doesn't consider target language. to reproduce: generate UT in lang A(i.e. Java) , place cursor on test method, select a template for lang B( i.e Groovy). test method in lang B added to UT in lang A.

also found this error in logs. didn't notice if it was raised right after test scenario described above

2024-05-20 22:16:59,163 [ 529273] SEVERE - com.weirddev.testme.intellij.generator.TestMeGenerator - error generating test class
java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0
    at com.weirddev.testme.intellij.utils.TestFileUpdateUtil.getImportStartElementToAdd(TestFileUpdateUtil.java:201)
    at com.weirddev.testme.intellij.utils.TestFileUpdateUtil.generateOrUpdateTestFile(TestFileUpdateUtil.java:67)
    at com.weirddev.testme.intellij.generator.TestMeGenerator.createTestClassFromCodeTemplate(TestMeGenerator.java:140)
    at com.weirddev.testme.intellij.generator.TestMeGenerator.createTestClass(TestMeGenerator.java:110)
    at com.weirddev.testme.intellij.generator.TestMeGenerator$1$1.compute(TestMeGenerator.java:66)
    at com.weirddev.testme.intellij.generator.TestMeGenerator$1$1.compute(TestMeGenerator.java:61)
    at com.intellij.openapi.application.impl.ApplicationImpl.lambda$runWriteAction$9(ApplicationImpl.java:1001)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteActionWithClass(ApplicationImpl.java:980)
    at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1001)
    at com.weirddev.testme.intellij.generator.TestMeGenerator$1.compute(TestMeGenerator.java:61)
    at com.weirddev.testme.intellij.generator.TestMeGenerator$1.compute(TestMeGenerator.java:59)
    at com.intellij.psi.impl.source.PostprocessReformattingAspectImpl.postponeFormattingInside(PostprocessReformattingAspectImpl.java:149)
    at com.weirddev.testme.intellij.generator.TestMeGenerator.generateTest(TestMeGenerator.java:59)
    at com.weirddev.testme.intellij.action.CreateTestMeAction$1.run(CreateTestMeAction.java:142)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:219)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:174)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:164)
    at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:150)
    at com.weirddev.testme.intellij.action.CreateTestMeAction.invoke(CreateTestMeAction.java:139)
    at com.weirddev.testme.intellij.action.TestMeCreator.invoke(TestMeCreator.java:35)
    at com.weirddev.testme.intellij.action.TestMeCreator.createTest(TestMeCreator.java:25)
    at com.weirddev.testme.intellij.action.TestMeAdditionalAction.execute(TestMeAdditionalAction.java:49)
    at com.weirddev.testme.intellij.ui.popup.TestMePopUpHandler.lambda$show$1(TestMePopUpHandler.java:99)
    at com.intellij.openapi.ui.popup.PopupChooserBuilder.lambda$setItemChosenCallback$0(PopupChooserBuilder.java:207)
    at com.intellij.ui.popup.AbstractPopup.lambda$dispose$18(AbstractPopup.java:1636)
    at com.intellij.openapi.wm.impl.FocusManagerImpl.lambda$doWhenFocusSettlesDown$3(FocusManagerImpl.java:173)

Bumped into other issues as well...but could not reproduce after clearing some UT's on simple setting. More testing required on more use cases. also think it would be very useful to add integration testing. As mentioned previously, UX feels awkward when behavior is different if cursor is on/off a test method name and if UT exists or not. think it would be good to support this feature as an Intention
FYI @huangliang992 Thanks

huangliang992 commented 4 months ago

I fixed the 1st issue,but for the second [consider target language] i have no good idea how to fix it。I will try to support this feature as an intention and add some integration tests some times later。

huangliang992 commented 4 months ago

I added a intention action on method.

yaronyam commented 4 months ago

Issues fixed by https://github.com/wrdv/testme-idea/pull/39 - many thanks @huangliang992 ❤️