Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw specific exceptions based on the method under testFeature/zfj/add exception on actual #29

Merged

Conversation

zhangfj88
Copy link
Contributor

Throw specific exceptions based on the method under test

Copy link
Member

@yaronyam yaronyam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for contributing this feature. I fear that most users might prefer the simplicity of specifying a general Exception type. would be good if a UI settings can be added to control this feature - i.e. "declare specific test method thrown exception types" - with checkbox defaulted to false

@@ -22,6 +22,7 @@
import com.weirddev.testme.intellij.utils.JavaPsiTreeUtils;
import com.weirddev.testme.intellij.utils.PropertyUtils;
import com.weirddev.testme.intellij.utils.TypeUtils;
import org.apache.commons.lang.StringUtils;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid dependencies on apache commons. it's a transitive dependency added by internal implementation of Jetbrains and may be removed. furthermore, I prefer to maintain small installation size and minimize external dependencies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggestion.I will try to make modifications according to your advice.

@@ -56,7 +57,8 @@ public static Method createMethod(PsiMethod psiMethod, PsiClass srcClass, int ma
Optional<PsiSubstitutor> methodSubstitutor = findMethodSubstitutor(psiMethod, srcClass, ownerClassPsiType);
Type returnType = resolveReturnType(psiMethod, maxRecursionDepth, typeDictionary, methodSubstitutor);
List<Param> methodParams = extractMethodParams(psiMethod, isPrimaryConstructor, maxRecursionDepth, typeDictionary, methodSubstitutor);
return new Method(methodId, methodName, returnType, ownerClassCanonicalType, methodParams, isPrivate, isProtected, isDefault, isPublic, isAbstract, isNative,
String methodExceptionTypes = extractMethodExceptionTypes(psiMethod);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename all references to throwsExceptions

return methodExceptionTypes;
}

private static String getExceptionTypeName(String exceptionTypeName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fully qualified name must be used in generated code. otherwise it may not compile due to missing import statement. a similar comment with example was already posted on previous PR - https://github.com/wrdv/testme-idea/pull/27/files#r1532873546


//analyze the exception types of the method
private static String extractMethodExceptionTypes(PsiMethod psiMethod) {
String methodExceptionTypes = "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some of the following actions on psi tree could be expensive in terms of performance. better check first if psiMethod does not below to tested class - return null.
BTW, default to null rather than empty string

* true - if method has Exception
*/
public boolean hasException(){
return StringUtils.isNotEmpty(methodExceptionTypes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think we can do without this method. better simplify and init field to null if resolved as empty string

@@ -29,7 +29,7 @@ public class ${CLASS_NAME} {
#if($TestSubjectUtils.shouldBeTested($method))

@Test
public void #renderTestMethodName($method.name)() throws Exception {
public void #renderTestMethodName($method.name)() #if($method.hasException()) throws $method.methodExceptionTypes #end{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove redundant spaces. before #if and #end. comment relevant for other templates as well

zhangfj88 and others added 10 commits March 26, 2024 11:43
# Conflicts:
#	src/main/java/com/weirddev/testme/intellij/configuration/TestMeConfig.java
#	src/main/java/com/weirddev/testme/intellij/template/FileTemplateConfig.java
#	src/main/java/com/weirddev/testme/intellij/ui/settings/TestMeSettingsForm.form
#	src/main/java/com/weirddev/testme/intellij/ui/settings/TestMeSettingsForm.java
Throw specific exceptions based on the method and configuration
Throw specific exceptions based on the method and configuration
Copy link
Member

@yaronyam yaronyam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR 💪 . Would be even greater if at least 1 integration test would be added. review TestMeGeneratorJunit4Test#testOverrideAbstractIgnoreInherited for example of a test case effected by configuration setting. if you're short on time - let me know I'll add one later. Thank You

PsiReferenceList throwsList = psiMethod.getThrowsList();
PsiClassType[] referencedTypes = throwsList.getReferencedTypes();

for (PsiClassType type : referencedTypes) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be simplified. please replace with:

String throwsExceptions = Arrays.stream(referencedTypes)
    .map(PsiClassType::resolve)
    .filter(Objects::nonNull)
    .map(PsiClass::getQualifiedName)
    .filter(Objects::nonNull)
    .collect(Collectors.joining(","));
return throwsException.isEmpty() ? "Exception" : throwsException;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks,I will Modify as per your advice and add integration test💪

@@ -89,4 +89,6 @@ public interface TestMeTemplateParams {
* Language of class under test: Groovy, Java, Scala
*/
String TESTED_CLASS_LANGUAGE = "TESTED_CLASS_LANGUAGE";

String THROW_SPECIFIC_EXCEPTION_TYPES = "throwSpecificExceptionTypes";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems unused. please remove

<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="declare specific test method thrown exception types"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please Capitalize: "Declare ...

zhangfj88 and others added 5 commits April 8, 2024 11:42
.filter(Objects::nonNull)
.collect(Collectors.joining(","));
}
return throwsExceptions == null ? "Exception" : throwsExceptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in case non found - throwsException would be empty string rather then null, if so please replace with
return throwsException.isEmpty() ? "Exception" : throwsException;
would also be good to change/add integration test to throw a more specific exception (i.e. java.io.IOException) rather then Exception in order to verify mapping logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,Let me modify the code then submit it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. in case non found - throwsException would be empty string rather then null --YES
  2. in case "Declare specific test method thrown exception types " is not checked --throwsException is null
  3. in cased "Declare specific test method thrown exception types " is checked and The testing method does not contain any exceptions--- then test method does not throw any exceptions.
    I prefer to case 3,
    so ,you prefer to throwing Exception in case 3?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean...thanks for elaborating
Ok, lets keep this logic as you suggest. Would like to refine the UI text to better express this into:
"Declare exceptions thrown by tested class" - so it would suggest that only if tested class actually declares exceptions - so will the test method.

Furthermore please do a small refactor... I find that mutating throwsExceptions variable and using it to transitively reflect throwSpecificExceptionTypes state - a bit confusing.

please use this alternative:

    private static String extractMethodExceptionTypes(PsiMethod psiMethod ,Boolean throwSpecificExceptionTypes,boolean testable) {
        if(!testable){
            return null;
        }
        if(throwSpecificExceptionTypes){
            PsiReferenceList throwsList = psiMethod.getThrowsList();
            PsiClassType[] referencedTypes = throwsList.getReferencedTypes();
            String throwsExceptions = Arrays.stream(referencedTypes)
                    .map(PsiClassType::resolve)
                    .filter(Objects::nonNull)
                    .map(PsiClass::getQualifiedName)
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining(","));
            return throwsExceptions.isEmpty() ? null : throwsExceptions;
        }
        else {
            return "Exception";
        }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for optimizing the code. I am modifying the code and then submit.

zhangfj88 and others added 2 commits April 11, 2024 11:15
Throw specific exceptions based on the method and configuration--add integration test
@yaronyam
Copy link
Member

Released in TestMe plugin version 6.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants