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

add the support for powermock and junit4 #26

Merged
merged 8 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class IconRegistry {
static {
add(new IconDescriptor("JUnit4", Icons.JUNIT4, Icons.JUNIT4_DARK));
add(new IconDescriptor("JUnit5", Icons.JUNIT5,Icons.JUNIT5));
add(new IconDescriptor("Powermock", Icons.POWERMOCK,Icons.POWERMOCK));
add(new IconDescriptor("Mockito", Icons.MOCKITO,Icons.MOCKITO));
add(new IconDescriptor("Groovy", Icons.GROOVY,Icons.GROOVY));
add(new IconDescriptor("Scala", Icons.SCALA,Icons.SCALA));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/weirddev/testme/intellij/icon/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface Icons {
Icon GROOVY = getIcon("/icons/groovy.png");
Icon SCALA = getIcon("/icons/scala.png");
Icon MOCKITO = getIcon("/icons/mockito.png");
Icon POWERMOCK = getIcon("/icons/powermock.png");
Icon JUNIT4 = getIcon("/icons/junit.png");
Icon JUNIT5 = getIcon("/icons/junit5.png");
Icon JUNIT4_DARK = getIcon("/icons/junit_dark.png");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum TemplateIcons {
JUnit4( IconFiles.JUNIT4, IconFiles.JUNIT4_DARK),
JUnit5( IconFiles.JUNIT5, IconFiles.JUNIT5),
Mockito( IconFiles.MOCKITO, IconFiles.MOCKITO),
Powermock( IconFiles.POWERMOCK, IconFiles.POWERMOCK),
Groovy( IconFiles.GROOVY, IconFiles.GROOVY),
Scala( IconFiles.SCALA, IconFiles.SCALA),
TestNG( IconFiles.TESTNG, IconFiles.TESTNG),
Expand All @@ -26,6 +27,7 @@ private static class IconFiles {
private static final String JUNIT5 = "/icons/junit5.png";
private static final String JUNIT4_DARK = "/icons/junit_dark.png";
private static final String TESTNG = "/icons/testNG.png";
private static final String POWERMOCK = "/icons/powermock.png";
}

private final Icon icon;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TemplateRegistry {
static private List<TemplateDescriptor> templateDescriptors = new ArrayList<TemplateDescriptor>();

public static final String JUNIT4_MOCKITO_JAVA_TEMPLATE = "JUnit4 & Mockito.java";
public static final String JUNIT4_POWERMOCK_JAVA_TEMPLATE = "JUnit4 & Powermock.java";
public static final String JUNIT5_MOCKITO_JAVA_TEMPLATE = "JUnit5 & Mockito.java";
public static final String TESTNG_MOCKITO_JAVA_TEMPLATE = "TestNG & Mockito.java";
public static final String JUNIT4_MOCKITO_GROOVY_TEMPLATE = "Groovy, JUnit4 & Mockito.groovy";
Expand All @@ -39,6 +40,9 @@ public class TemplateRegistry {
templateDescriptors.add(new TemplateDescriptor("<html><i>JUnit4</i>"+ TemplateIcons.JUnit4.asHtml()+"& <i>Mockito</i>" + TemplateIcons.Mockito.asHtml()+ "</html>",
"<html><i>JUnit4</i></html><JUnit4><html>& <i>Mockito</i></html><Mockito>",
JUNIT4_MOCKITO_JAVA_TEMPLATE, Language.Java, TemplateRole.Tester));
templateDescriptors.add(new TemplateDescriptor("<html><i>JUnit4</i>"+ TemplateIcons.JUnit4.asHtml()+"& <i>Powermock</i>" + TemplateIcons.Powermock.asHtml()+ "</html>",
"<html><i>JUnit4</i></html><JUnit4><html>& <i>Powermock</i></html><Powermock>",
JUNIT4_POWERMOCK_JAVA_TEMPLATE, Language.Java, TemplateRole.Tester));
templateDescriptors.add(new TemplateDescriptor("<html><i>JUnit5</i>"+ TemplateIcons.JUnit5.asHtml()+"& <i>Mockito</i>" + TemplateIcons.Mockito.asHtml()+ "</html>",
"<html><i>JUnit5</i></html><JUnit5><html>& <i>Mockito</i></html><Mockito>",
JUNIT5_MOCKITO_JAVA_TEMPLATE, Language.Java, TemplateRole.Tester));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,12 @@ public boolean hasReturn(){
return returnType != null && !"void".equals(returnType.getName());
}

/**
*
* true - if method has parameters
*/
public boolean hasParams() {
return !methodParams.isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ private String deductMatcherTypeMethod(Param param, Language language) {
return matcherType;
}

/**
*
* @param calledRefMethod call method
* @param testedClass the tested class
* @return true - if the calledRefMethod is class object self called method
*/
public boolean isSelfCalled(Method calledRefMethod, Type testedClass) {
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
List<Method> methods = testedClass.getMethods();
return methods.stream().anyMatch(method -> method.getMethodId().equals(calledRefMethod.getMethodId()));
}

/**
* true - if should stub tested method
* @param testMethod method being tested
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#parse("TestMe macros.java")
#set($hasMocks=$MockitoMockBuilder.hasMocks($TESTED_CLASS))
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
#if($PACKAGE_NAME)
package ${PACKAGE_NAME};
#end

import org.junit.Assert;
import org.junit.Test;
#if($hasMocks)
import static org.powermock.api.mockito.PowerMockito.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import org.junit.Before;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
#end

#parse("File Header.java")
@RunWith(PowerMockRunner.class)
@PrepareForTest({${TESTED_CLASS.name}.class})
@PowerMockIgnore("javax.management.*")
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
public class ${CLASS_NAME} {
#renderMockedFields($TESTED_CLASS.fields,$hasMocks)
#renderPowerMockTestSubjectInit($TESTED_CLASS,$TestSubjectUtils.hasTestableInstanceMethod($TESTED_CLASS.methods),$hasMocks)

## all methods under powermock framework can be tested
#foreach($method in $TESTED_CLASS.methods)
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
#if(!$method.inherited)
@Test
public void #renderTestMethodName($method.name)() throws Exception {
#if($hasMocks && $MockitoMockBuilder.shouldStub($method, $TESTED_CLASS.fields))
#renderMockStubs($method, $TESTED_CLASS.fields)
#end
#renderSelfCallMethodMockStubs($method, $TESTED_CLASS)
#if($method.isPrivate())
#renderPrivateMethodCall($method, $TESTED_CLASS.name)
#else
#renderMethodCall($method, $TESTED_CLASS.name)
#end
#if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields))
#renderMockVerifies($method,$TESTED_CLASS.fields)
#end
#if($method.hasReturn())
Assert.#renderJUnitAssert($method)
#end
}
#end
#end
}

################## Macros #####################
#macro(renderPowerMockTestSubjectInit $testedClass $hasTestableInstanceMethod $hasMocks)
#if($hasMocks)
@InjectMocks
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
#end
$testedClass.canonicalName $StringUtils.deCapitalizeFirstLetter($testedClass.name) = spy($TestBuilder.renderInitType($testedClass,"$class.name",$replacementTypes,$defaultTypeValues));
#end

##----mock self call method call stub------
#macro(renderSelfCallMethodMockStubs $method $testedClass)
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
#foreach($methodCall in $method.directMethodCalls)
#if(${MockitoMockBuilder.isSelfCalled($methodCall.method, $testedClass)} && $methodCall.method.hasReturn())
#if($methodCall.method.hasParams())
doReturn($TestBuilder.renderReturnParam($methodCall.method,$methodCall.method.returnType,"${methodCall.method.name}Response",$replacementTypes,$defaultTypeValues))
.when(${StringUtils.deCapitalizeFirstLetter($testedClass.name)}, "${methodCall.method.name}", $MockitoMockBuilder.buildMockArgsMatchers(${methodCall.method.methodParams},"Java"));
#else
doReturn($TestBuilder.renderReturnParam($methodCall.method,$methodCall.method.returnType,"${methodCall.method.name}Response",$replacementTypes,$defaultTypeValues))
.when(${StringUtils.deCapitalizeFirstLetter($testedClass.name)}, "${methodCall.method.name}");
#end
#end
#end
#end

##-- render private method call for power mock -----
#macro(renderPrivateMethodCall $method, $testedClassName)
#renderJavaReturnVar($method.returnType)
#if($method.static)
$testedClassName.${method.name}($TestBuilder.renderMethodParams($method,$replacementTypes,$defaultTypeValues));
#elseif($method.hasParams())
Whitebox.invokeMethod($StringUtils.deCapitalizeFirstLetter($testedClassName), "${method.name}", $TestBuilder.renderMethodParams($method,$replacementTypes,$defaultTypeValues));
#else
Whitebox.invokeMethod($StringUtils.deCapitalizeFirstLetter($testedClassName), "${method.name}");
#end
#end

#parse("TestMe Footer.java")

Binary file added src/main/resources/icons/powermock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ class TemplateRegistryTest {
@Test
void testTemplateRegistry() throws Exception {
def descriptors = new TemplateRegistry().getTemplateDescriptors()
assert descriptors.size() ==7
assert descriptors.size() ==8
assert descriptors.find({it.htmlDisplayName.contains('JUnit4')&& it.htmlDisplayName.contains('Groovy')}).filename == TemplateRegistry.JUNIT4_MOCKITO_GROOVY_TEMPLATE
assert descriptors.find({it.htmlDisplayName.contains('<i>Spock</i>')}).filename == TemplateRegistry.SPOCK_MOCKITO_GROOVY_TEMPLATE
assert descriptors.find({it.htmlDisplayName.contains('JUnit5')&& it.htmlDisplayName.contains('Mockito')}).filename == TemplateRegistry.JUNIT5_MOCKITO_JAVA_TEMPLATE
assert descriptors.find({it.htmlDisplayName.contains('TestNG')&& it.htmlDisplayName.contains('Mockito')}).filename == TemplateRegistry.TESTNG_MOCKITO_JAVA_TEMPLATE
assert descriptors.find({it.htmlDisplayName.contains('Spock Parameterized')&& it.htmlDisplayName.contains('Mockito')}).filename == TemplateRegistry.SPOCK_PARAMETERIZED_MOCKITO_GROOVY_TEMPLATE
assert descriptors.find({it.htmlDisplayName.contains('Specs2')&& it.htmlDisplayName.contains('Mockito')}).filename == TemplateRegistry.SPECS2_MOCKITO_SCALA_TEMPLATE
assert descriptors.find({it.htmlDisplayName.contains('JUnit5')&& it.htmlDisplayName.contains('Mockito')}).displayName.find("JUnit5.*Mockito")!=null
assert descriptors.find({it.htmlDisplayName.contains('JUnit4')&& it.htmlDisplayName.contains('Powermock')}).filename == TemplateRegistry.JUNIT4_POWERMOCK_JAVA_TEMPLATE
}

}
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading