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 specific type to Mockito argument matcher any-- remove unused method #6

Merged
Merged
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = 'com.weirddev.testme'
version = 6.2.0
version = 6.3.0

#jvmTargetVersion = 1.8
jvmTargetVersion = 17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class Field {
* true if field has setter in tested class
*/
@Getter private final boolean hasSetter;
/**
* true if field is set to a value on declaration
*/
@Getter private final boolean isInitializedInline;

/**
* name given to field
Expand All @@ -61,6 +65,7 @@ public Field(PsiField psiField, PsiClass srcClass, TypeDictionary typeDictionary
overridden = isOverriddenInChild(psiField, srcClass);
isFinal = psiField.getModifierList() != null && psiField.getModifierList().hasExplicitModifier(PsiModifier.FINAL);
isStatic = psiField.getModifierList() != null && psiField.getModifierList().hasExplicitModifier(PsiModifier.STATIC);
isInitializedInline = typeDictionary!=null && typeDictionary.isTestSubject(srcClass) && psiField.hasInitializer();//hasInitializer - expensive test - call only for test subject (consider moving to a new type - ExtendedField - for usage in test subject
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.intellij.openapi.diagnostic.Logger;
import com.weirddev.testme.intellij.generator.TestBuilderUtil;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -21,6 +20,7 @@ public class MockitoMockBuilder implements MockBuilder{
private static final Logger LOG = Logger.getInstance(MockitoMockBuilder.class.getName());
public static final Map<String, String> TYPE_TO_ARG_MATCHERS;
private static final Pattern SEMVER_PATTERN = Pattern.compile("^(\\d*)\\.(\\d*)\\.*");
public static final Pattern LOGGER_PATTERN = Pattern.compile("(?i).*log.*");

static {
TYPE_TO_ARG_MATCHERS = new HashMap<>();
Expand Down Expand Up @@ -120,14 +120,27 @@ public boolean isMockable(Field field) {
@SuppressWarnings("unused")
@Override
public boolean isMockable(Field field, Type testedClass) {
final boolean isMockable = !field.getType().isPrimitive() && !isWrapperType(field.getType())
&& (!field.getType().isFinal() || isMockitoMockMakerInlineOn) && !field.isOverridden()
&& !field.getType().isArray() && !field.getType().isEnum()
&& !testSubjectInspector.isNotInjectedInDiClass(field, testedClass);
final boolean isMockable = isMockableCommonChecks(field, testedClass) && (!field.getType().isFinal() || isMockitoMockMakerInlineOn);
LOG.debug("field " + field.getType().getCanonicalName() + " " + field.getName() + " is mockable:" + isMockable);
return isMockable;
}

/**
* checks if field in testedClass can be mocked. evaluates conditions common to all currently supported mock frameworks
* @return true if input field in testedClass can be mocked
*/
protected boolean isMockableCommonChecks(Field field, Type testedClass) {
return !field.getType().isPrimitive() && !isWrapperType(field.getType())
&& !field.isOverridden() && !field.getType().isArray() && !field.getType().isEnum()
&& !testSubjectInspector.isNotInjectedInDiClass(field, testedClass) && !isInitInline(field);
}

private static boolean isInitInline(Field field) {
//for now, avoid applying on all such fields since it's hard to deduct if default value overridden in ctor. settling for typical logger initialization pattern
//todo try to also deduct if init in static block
return field.isInitializedInline() && !field.isHasSetter() && LOGGER_PATTERN.matcher(field.getName()).matches();
}

/**
* true - if argument can be mocked given the provided default types
*/
Expand Down Expand Up @@ -249,8 +262,10 @@ private static String resolveMatcherMethod(Type type) {
}
}

String addSpecificType(String typeName) {
return StringUtils.isNotEmpty(typeName) ? "any("+typeName+".class)" : "any()";
@SuppressWarnings("unused")
@Deprecated
public boolean shouldStub(Method testMethod, List<Field> testedClassFields) {
return callsMockMethod(testMethod, testedClassFields, Method::hasReturn, null);
}
/**
* true - if should stub tested method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public boolean hasInternalMethodCall(Method method, Type testedClass) {
*/
@Override
public boolean isMockable(Field field, Type testedClass) {
final boolean isMockable = !field.getType().isPrimitive() && !isWrapperType(field.getType())
&& !field.isOverridden() && !field.getType().isArray() && !field.getType().isEnum()
&& !testSubjectInspector.isNotInjectedInDiClass(field, testedClass);
final boolean isMockable = isMockableCommonChecks(field, testedClass);
LOG.debug("field " + field.getType().getCanonicalName() + " " + field.getName() + " is mockable:" + isMockable);
return isMockable;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>com.weirddev.testme</id>
<name>TestMe</name>
<version>6.2.0</version>
<version>6.3.0</version>
<vendor email="testme@weirddev.com" url="https://weirddev.com">WeirdDev</vendor>

<description><![CDATA[
Expand All @@ -10,7 +10,7 @@
<h3>Features:</h3>
<ul>
<li>Auto generate Java, Scala or Groovy test code with JUnit 4/5, TestNG, Spock or Specs2 frameworks</li>
<li>Auto generate Mockito mocked dependencies and relevant return statements</li>
<li>Generate mocked dependencies and stub return values</li>
<li>Generate test params and assertion statements</li>
<li>Integrates with IDEA menus: Code->TestMe, Code->Generate</li>
<li>Configure custom test code generation: Preferences -> TestMe -> TestMe Templates</li>
Expand All @@ -20,10 +20,10 @@

<change-notes><![CDATA[
<ul>
<li>Generate mock verify method call statements if method retruns void(contributed by <a href="https://github.com/huangliang992">HuangLiang</a> - <a href="https://github.com/wrdv/testme-idea/pull/23">PR #23</a>) </li>
<li>Do not generate Mockito mocks for classes with only private constructors (contributed by <a href="https://github.com/huangliang992">HuangLiang</a> - <a href="https://github.com/wrdv/testme-idea/pull/22">PR #22</a>) </li>
<li>Generate UT for enum methods (raised by gadeji on Feb 15 '24)</li>
<li>Do not generate UT for methods inherited from java base enum class</li>
<li>Support unit test generation with PowerMock - with JUnit4 template (contributed by <a href="https://github.com/huangliang992">HuangLiang</a> - <a href="https://github.com/wrdv/testme-idea/pull/26">PR #26</a>) </li>
<li>Stub internal method calls, if configured in Settings -> TestMe (contributed by <a href="https://github.com/huangliang992">HuangLiang</a> - <a href="https://github.com/wrdv/testme-idea/pull/26">PR #26</a>) </li>
<li>Mock fields injected by DI if class has DI annotations (contributed by <a href="https://github.com/huangliang992">HuangLiang</a> - <a href="https://github.com/wrdv/testme-idea/pull/24">PR #24</a>) </li>
<li>Avoid mocking log/logger fields initialized inline</li>
</ul>
<a href="https://weirddev.com/testme/release-notes">Complete Release Notes listing</a>
]]>
Expand Down
Binary file modified 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 @@ -12,7 +12,7 @@ public class Foo{

private FooFighter fooFighter;
private Supplier<Integer> result;
private Logger logger;
private Logger logger = Logger.getLogger(Foo.class.getName());;

public String fight(Fire withFire,String foeName) {
logger.trace("this method does not return a value so it should not be stubbed");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.services.impl;

import com.example.beans.ConvertedBean;
import com.example.dependencies.Logger;
import com.example.foes.Fear;
import com.example.foes.Fire;
import com.example.foes.Ice;
Expand All @@ -25,8 +24,7 @@ public class FooTest {
FooFighter fooFighter;
@Mock
Supplier<Integer> result;
@Mock
Logger logger;

@InjectMocks
Foo foo;

Expand All @@ -41,7 +39,6 @@ public void testFight() throws Exception {
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
verify(logger).trace(anyString());
Assert.assertEquals("replaceMeWithExpectedResult", result);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.services.impl

import com.example.beans.ConvertedBean
import com.example.dependencies.Logger
import com.example.foes.Fear
import com.example.foes.Fire
import com.example.foes.Ice
Expand All @@ -21,8 +20,7 @@ class FooTest {
FooFighter fooFighter
@Mock
Supplier<Integer> result
@Mock
Logger logger

@InjectMocks
Foo foo

Expand All @@ -37,7 +35,6 @@ class FooTest {
when(result.get()).thenReturn(0)

String result = foo.fight(new Fire(), "foeName")
verify(logger).trace(anyString())
assert result == "replaceMeWithExpectedResult"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.services.impl;

import com.example.beans.ConvertedBean;
import com.example.dependencies.Logger;
import com.example.foes.Fear;
import com.example.foes.Fire;
import com.example.foes.Ice;
Expand All @@ -25,8 +24,7 @@ class FooTest {
FooFighter fooFighter;
@Mock
Supplier<Integer> result;
@Mock
Logger logger;

@InjectMocks
Foo foo;

Expand All @@ -41,7 +39,6 @@ void testFight() {
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
verify(logger).trace(anyString());
Assertions.assertEquals("replaceMeWithExpectedResult", result);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.services.impl

import com.example.beans.ConvertedBean
import com.example.dependencies.Logger
import com.example.foes.Fear
import com.example.foes.Fire
import com.example.foes.Ice
Expand All @@ -20,8 +19,7 @@ class FooTest extends Specification {
FooFighter fooFighter
@Mock
Supplier<Integer> result
@Mock
Logger logger

@InjectMocks
Foo foo

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.example.services.impl;

import com.example.beans.ConvertedBean;
import com.example.dependencies.Logger;
import com.example.foes.Fear;
import com.example.foes.Fire;
import com.example.foes.Ice;
Expand All @@ -25,8 +24,7 @@ public class FooTest {
FooFighter fooFighter;
@Mock
Supplier<Integer> result;
@Mock
Logger logger;

@InjectMocks
Foo foo;

Expand All @@ -41,7 +39,6 @@ public void testFight() {
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
verify(logger).trace(anyString());
Assert.assertEquals(result, "replaceMeWithExpectedResult");
}
}
Expand Down