From 3880580bf62b889c5d57e7dd82b27e694591e107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E4=BA=AC-=E9=BB=84=E4=BA=AE?= Date: Tue, 5 Mar 2024 10:30:07 +0800 Subject: [PATCH 1/8] add verify for void return type of method calls --- .../template/context/MockitoMockBuilder.java | 22 +++++++++++++++++++ .../testMeIncludes/TestMe macros.groovy.ft | 12 ++++++++++ .../testMeIncludes/TestMe macros.java.ft | 12 ++++++++++ .../Groovy, JUnit4 & Mockito.groovy.ft | 4 ++++ .../testMeTests/JUnit4 & Mockito.java.ft | 4 ++++ .../testMeTests/JUnit5 & Mockito.java.ft | 4 ++++ .../testMeTests/TestNG & Mockito.java.ft | 4 ++++ 7 files changed, 62 insertions(+) diff --git a/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java b/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java index c4da47c3..881c528b 100644 --- a/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java +++ b/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java @@ -263,6 +263,28 @@ public boolean shouldStub(Method testMethod, List testedClassFields) { return shouldStub; } + /** + * true - if should verify tested method + * @param testMethod method being tested + * @param testedClassFields fields of owner type being tested + */ + @SuppressWarnings("unused") + public boolean shouldVerify(Method testMethod, List testedClassFields) { + boolean shouldVerity = false; + for (Field testedClassField : testedClassFields) { + if (isMockable(testedClassField)) { + for (Method fieldMethod : testedClassField.getType().getMethods()) { + if (fieldMethod.getReturnType() != null && "void".equals(fieldMethod.getReturnType().getCanonicalName()) && testSubjectInspector.isMethodCalled(fieldMethod, testMethod)) { + shouldVerity = true; + break; + } + } + } + } + LOG.debug("method "+testMethod.getMethodId()+" should be verified:"+shouldVerity); + return shouldVerity; + } + /** * true - if tested method should be stubbed * @param testMethod - method being tested diff --git a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft index ed34032a..110267f6 100644 --- a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft +++ b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft @@ -102,4 +102,16 @@ result#if($TestSubjectUtils.isJavaFuture($method.returnType)).get()#end == $Test #end #end #end +#end +## +#macro(grRenderMockVerifies $method $testedClassFields) +#foreach($field in $testedClassFields) +#if($MockitoMockBuilder.isMockable($field)) +#foreach($fieldMethod in $field.type.methods) +#if($fieldMethod.returnType && $fieldMethod.returnType.name =="void" && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) + verify($field.name).${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java")) +#end +#end +#end +#end #end \ No newline at end of file diff --git a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft index 4cd735d5..047edb96 100644 --- a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft +++ b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft @@ -94,4 +94,16 @@ assertEquals(result#{if}($TestSubjectUtils.isJavaFuture($method.returnType)).get #end #end #end +#end +## +#macro(renderMockVerifies $method $$testedClassFields) +#foreach($field in $testedClassFields) +#if($MockitoMockBuilder.isMockable($field)) +#foreach($fieldMethod in $field.type.methods) +#if($fieldMethod.returnType && $fieldMethod.returnType.name =="void" && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) + verify($field.name).${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java")); +#end +#end +#end +#end #end \ No newline at end of file diff --git a/src/main/resources/fileTemplates/testMeTests/Groovy, JUnit4 & Mockito.groovy.ft b/src/main/resources/fileTemplates/testMeTests/Groovy, JUnit4 & Mockito.groovy.ft index 3a51809d..576424b6 100644 --- a/src/main/resources/fileTemplates/testMeTests/Groovy, JUnit4 & Mockito.groovy.ft +++ b/src/main/resources/fileTemplates/testMeTests/Groovy, JUnit4 & Mockito.groovy.ft @@ -34,6 +34,10 @@ class ${CLASS_NAME} { #end $TAB$TAB#grRenderMethodCall($method,$TESTED_CLASS.name) + +#if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields)) +#grRenderMockVerifies($method,$TESTED_CLASS.fields) +#end #if($method.hasReturn()) assert #grRenderAssert($method) #end diff --git a/src/main/resources/fileTemplates/testMeTests/JUnit4 & Mockito.java.ft b/src/main/resources/fileTemplates/testMeTests/JUnit4 & Mockito.java.ft index a3f7e33d..fcb285a5 100644 --- a/src/main/resources/fileTemplates/testMeTests/JUnit4 & Mockito.java.ft +++ b/src/main/resources/fileTemplates/testMeTests/JUnit4 & Mockito.java.ft @@ -35,6 +35,10 @@ public class ${CLASS_NAME} { #end $TAB$TAB#renderMethodCall($method,$TESTED_CLASS.name) + +#if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields)) +#renderMockVerifies($method,$TESTED_CLASS.fields) +#end #if($method.hasReturn()) Assert.#renderJUnitAssert($method)#end } diff --git a/src/main/resources/fileTemplates/testMeTests/JUnit5 & Mockito.java.ft b/src/main/resources/fileTemplates/testMeTests/JUnit5 & Mockito.java.ft index 7e2a872b..cadecbb0 100644 --- a/src/main/resources/fileTemplates/testMeTests/JUnit5 & Mockito.java.ft +++ b/src/main/resources/fileTemplates/testMeTests/JUnit5 & Mockito.java.ft @@ -34,6 +34,10 @@ class ${CLASS_NAME} { #end $TAB$TAB#renderMethodCall($method,$TESTED_CLASS.name) + +#if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields)) +#renderMockVerifies($method,$TESTED_CLASS.fields) +#end #if($method.hasReturn()) Assertions.#renderJUnitAssert($method)#end } #end diff --git a/src/main/resources/fileTemplates/testMeTests/TestNG & Mockito.java.ft b/src/main/resources/fileTemplates/testMeTests/TestNG & Mockito.java.ft index 4fc70d4f..fd5e0958 100644 --- a/src/main/resources/fileTemplates/testMeTests/TestNG & Mockito.java.ft +++ b/src/main/resources/fileTemplates/testMeTests/TestNG & Mockito.java.ft @@ -34,6 +34,10 @@ public class ${CLASS_NAME} { #end $TAB$TAB#renderMethodCall($method,$TESTED_CLASS.name) + +#if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields)) +#renderMockVerifies($method,$TESTED_CLASS.fields) +#end #if($method.hasReturn()) Assert.#renderTestNgAssert($method)#end } #end From 5c13701b2dbe12896f9f761f7c950c64e89934e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E4=BA=AC-=E9=BB=84=E4=BA=AE?= Date: Tue, 5 Mar 2024 10:32:54 +0800 Subject: [PATCH 2/8] add tests --- .../TestMeGeneratorGroovyJunit4Test.java | 5 ++ .../generator/TestMeGeneratorJunit4Test.java | 4 ++ .../generator/TestMeGeneratorJunit5Test.java | 3 ++ .../generator/TestMeGeneratorTestNgTest.java | 4 ++ .../com/example/warriers/TechFighter.java | 13 ++++++ .../warriers/impl/TechFighterImpl.java | 31 +++++++++++++ .../src/com/example/services/impl/Foo.java | 23 ++++++++++ .../com/example/services/impl/FooTest.java | 46 +++++++++++++++++++ .../com/example/services/impl/FooTest.groovy | 42 +++++++++++++++++ .../com/example/services/impl/FooTest.java | 46 +++++++++++++++++++ .../com/example/services/impl/FooTest.java | 46 +++++++++++++++++++ 11 files changed, 263 insertions(+) create mode 100644 testData/commonSrc/com/example/warriers/TechFighter.java create mode 100644 testData/commonSrc/com/example/warriers/impl/TechFighterImpl.java create mode 100644 testData/testMeGenerator/verifyMethodCall/src/com/example/services/impl/Foo.java create mode 100644 testData/testMeGenerator/verifyMethodCall/test/com/example/services/impl/FooTest.java create mode 100644 testData/testMeGenerator/verifyMethodCall/testGroovy/com/example/services/impl/FooTest.groovy create mode 100644 testData/testMeGenerator/verifyMethodCall/testJunit5/com/example/services/impl/FooTest.java create mode 100644 testData/testMeGenerator/verifyMethodCall/testTestNg/com/example/services/impl/FooTest.java diff --git a/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorGroovyJunit4Test.java b/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorGroovyJunit4Test.java index 4874ac46..3c67ca1d 100644 --- a/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorGroovyJunit4Test.java +++ b/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorGroovyJunit4Test.java @@ -1,5 +1,7 @@ package com.weirddev.testme.intellij.generator; +import com.weirddev.testme.intellij.configuration.TestMeConfigPersistent; +import com.weirddev.testme.intellij.template.FileTemplateConfig; import com.weirddev.testme.intellij.template.TemplateRegistry; import com.weirddev.testme.intellij.template.context.Language; @@ -47,4 +49,7 @@ public void testUtilWithoutAccessableCtor() { doTest(true, true, true); } + public void testVerifyMethodCall() { + doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState())); + } } diff --git a/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorJunit4Test.java b/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorJunit4Test.java index 349a2c06..f896d575 100644 --- a/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorJunit4Test.java +++ b/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorJunit4Test.java @@ -176,6 +176,10 @@ public void testUtilWithoutAccessableCtor() { doTest(true, true, true); } + public void testVerifyMethodCall() { + doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState())); + } + //todo TC - use static init method when constructor not available // TODO TC different test target dir diff --git a/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorJunit5Test.java b/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorJunit5Test.java index 5fbf7593..77807cd6 100644 --- a/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorJunit5Test.java +++ b/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorJunit5Test.java @@ -33,4 +33,7 @@ public void testUtilWithoutAccessableCtor() { doTest(true, true, true); } + public void testVerifyMethodCall() { + doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState())); + } } diff --git a/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorTestNgTest.java b/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorTestNgTest.java index 2f81e537..4c3b2d23 100644 --- a/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorTestNgTest.java +++ b/src/integrationTest/java/com/weirddev/testme/intellij/generator/TestMeGeneratorTestNgTest.java @@ -33,4 +33,8 @@ public void testUtilWithoutAccessableCtor() { doTest(true, true, true); } + public void testVerifyMethodCall() { + doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState())); + } + } \ No newline at end of file diff --git a/testData/commonSrc/com/example/warriers/TechFighter.java b/testData/commonSrc/com/example/warriers/TechFighter.java new file mode 100644 index 00000000..d4e1ac8f --- /dev/null +++ b/testData/commonSrc/com/example/warriers/TechFighter.java @@ -0,0 +1,13 @@ +package com.example.warriers; + +import com.example.beans.ConvertedBean; +import com.example.foes.Fear; +import com.example.foes.Fire; +import com.example.foes.Ice; + +/** Test input class*/ +public interface TechFighter { + void initSelfArming(String weapon) + String fight(Fire withFire); + ConvertedBean surrender(Fear fear, Ice ice,int times); +} \ No newline at end of file diff --git a/testData/commonSrc/com/example/warriers/impl/TechFighterImpl.java b/testData/commonSrc/com/example/warriers/impl/TechFighterImpl.java new file mode 100644 index 00000000..787ee6c2 --- /dev/null +++ b/testData/commonSrc/com/example/warriers/impl/TechFighterImpl.java @@ -0,0 +1,31 @@ +package com.example.warriers.impl; + +import com.example.beans.ConvertedBean; +import com.example.foes.Fear; +import com.example.foes.Fire; +import com.example.foes.Ice; +import com.example.warriers.TechFighter; + +/** Test input class*/ +public class TechFighterImpl implements TechFighter { + + private String weapon; + + @Override + public String fight(Fire withFire) { + return "flames"; + } + + @Override + public ConvertedBean surrender(Fear fear, Ice ice,int times) { + System.out.println("times:" + times); + System.out.println("fear:" + fear.toString()); + return new ConvertedBean(); + } + + @Override + public void initSelfArming(String weapon) { + this.weapon = weapon; + } + +} diff --git a/testData/testMeGenerator/verifyMethodCall/src/com/example/services/impl/Foo.java b/testData/testMeGenerator/verifyMethodCall/src/com/example/services/impl/Foo.java new file mode 100644 index 00000000..d1577282 --- /dev/null +++ b/testData/testMeGenerator/verifyMethodCall/src/com/example/services/impl/Foo.java @@ -0,0 +1,23 @@ +package com.example.services.impl; + +import com.example.beans.ConvertedBean; +import java.util.function.Supplier; +import com.example.dependencies.Logger; +import com.example.foes.Fear; +import com.example.foes.Fire; +import com.example.foes.Ice; +import com.example.warriers.TechFighter; + +public class Foo{ + + private TechFighter techFighter; + private Supplier result; + + public String fight(Fire withFire,String foeName) { + techFighter.initSelfArming("gun"); + String fail = techFighter.fight(withFire); + ConvertedBean convertedBean = techFighter.surrender(new Fear(), new Ice(), 666); + convertedBean.setSomeNum(result.get()); + return "returning response from dependency "+ fail + " " + convertedBean.getMyString(); + } +} diff --git a/testData/testMeGenerator/verifyMethodCall/test/com/example/services/impl/FooTest.java b/testData/testMeGenerator/verifyMethodCall/test/com/example/services/impl/FooTest.java new file mode 100644 index 00000000..024047e5 --- /dev/null +++ b/testData/testMeGenerator/verifyMethodCall/test/com/example/services/impl/FooTest.java @@ -0,0 +1,46 @@ +package com.example.services.impl; + +import com.example.beans.ConvertedBean; +import com.example.foes.Fire; +import com.example.warriers.TechFighter; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.function.Supplier; + +import static org.mockito.Mockito.*; + +/** + * created by TestMe integration test on MMXVI + */ +public class FooTest { + @Mock + TechFighter techFighter; + @Mock + Supplier result; + @InjectMocks + Foo foo; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testFight() throws Exception { + when(techFighter.fight(any())).thenReturn("fightResponse"); + when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean()); + when(result.get()).thenReturn(Integer.valueOf(0)); + + String result = foo.fight(new Fire(), "foeName"); + + verify(techFighter).initSelfArming(anyString()); + Assert.assertEquals("replaceMeWithExpectedResult", result); + } +} + +//Generated with love by TestMe :) Please raise issues & feature requests at: https://weirddev.com/forum#!/testme \ No newline at end of file diff --git a/testData/testMeGenerator/verifyMethodCall/testGroovy/com/example/services/impl/FooTest.groovy b/testData/testMeGenerator/verifyMethodCall/testGroovy/com/example/services/impl/FooTest.groovy new file mode 100644 index 00000000..e0f6f775 --- /dev/null +++ b/testData/testMeGenerator/verifyMethodCall/testGroovy/com/example/services/impl/FooTest.groovy @@ -0,0 +1,42 @@ +package com.example.services.impl + +import com.example.beans.ConvertedBean +import com.example.foes.Fire +import com.example.warriers.TechFighter +import org.junit.Test +import org.junit.Before +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.MockitoAnnotations + +import java.util.function.Supplier +import static org.mockito.Mockito.* + +/** created by TestMe integration test on MMXVI */ +class FooTest { + @Mock + TechFighter techFighter + @Mock + Supplier result + @InjectMocks + Foo foo + + @Before + void setUp() { + MockitoAnnotations.initMocks(this) + } + + @Test + void testFight() { + when(techFighter.fight(any())).thenReturn("fightResponse") + when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0)) + when(result.get()).thenReturn(0) + + String result = foo.fight(new Fire(), "foeName") + + verify(techFighter).initSelfArming(anyString()) + assert result == "replaceMeWithExpectedResult" + } +} + +//Generated with love by TestMe :) Please raise issues & feature requests at: https://weirddev.com/forum#!/testme \ No newline at end of file diff --git a/testData/testMeGenerator/verifyMethodCall/testJunit5/com/example/services/impl/FooTest.java b/testData/testMeGenerator/verifyMethodCall/testJunit5/com/example/services/impl/FooTest.java new file mode 100644 index 00000000..6e9abfe7 --- /dev/null +++ b/testData/testMeGenerator/verifyMethodCall/testJunit5/com/example/services/impl/FooTest.java @@ -0,0 +1,46 @@ +package com.example.services.impl; + +import com.example.beans.ConvertedBean; +import com.example.foes.Fire; +import com.example.warriers.TechFighter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.function.Supplier; + +import static org.mockito.Mockito.*; + +/** + * created by TestMe integration test on MMXVI + */ +class FooTest { + @Mock + TechFighter techFighter; + @Mock + Supplier result; + @InjectMocks + Foo foo; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void testFight() { + when(techFighter.fight(any())).thenReturn("fightResponse"); + when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean()); + when(result.get()).thenReturn(Integer.valueOf(0)); + + String result = foo.fight(new Fire(), "foeName"); + + verify(techFighter).initSelfArming(anyString()); + Assertions.assertEquals("replaceMeWithExpectedResult", result); + } +} + +//Generated with love by TestMe :) Please raise issues & feature requests at: https://weirddev.com/forum#!/testme \ No newline at end of file diff --git a/testData/testMeGenerator/verifyMethodCall/testTestNg/com/example/services/impl/FooTest.java b/testData/testMeGenerator/verifyMethodCall/testTestNg/com/example/services/impl/FooTest.java new file mode 100644 index 00000000..0b0de813 --- /dev/null +++ b/testData/testMeGenerator/verifyMethodCall/testTestNg/com/example/services/impl/FooTest.java @@ -0,0 +1,46 @@ +package com.example.services.impl; + +import com.example.beans.ConvertedBean; +import com.example.foes.Fire; +import com.example.warriers.TechFighter; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.function.Supplier; + +import static org.mockito.Mockito.*; + +/** + * created by TestMe integration test on MMXVI + */ +public class FooTest { + @Mock + TechFighter techFighter; + @Mock + Supplier result; + @InjectMocks + Foo foo; + + @BeforeMethod + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testFight() { + when(techFighter.fight(any())).thenReturn("fightResponse"); + when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean()); + when(result.get()).thenReturn(Integer.valueOf(0)); + + String result = foo.fight(new Fire(), "foeName"); + + verify(techFighter).initSelfArming(anyString()); + Assert.assertEquals(result, "replaceMeWithExpectedResult"); + } +} + +//Generated with love by TestMe :) Please raise issues & feature requests at: https://weirddev.com/forum#!/testme \ No newline at end of file From 19bf3f3037951f5e5303797c5fa6aec87bcc1911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E4=BA=AC-=E9=BB=84=E4=BA=AE?= Date: Tue, 5 Mar 2024 11:22:25 +0800 Subject: [PATCH 3/8] Adjust previous test cases --- .../testme/intellij/template/context/MockitoMockBuilder.java | 3 +++ .../testMeTests/Groovy, JUnit4 & Mockito.groovy.ft | 1 - .../fileTemplates/testMeTests/JUnit4 & Mockito.java.ft | 1 - .../fileTemplates/testMeTests/JUnit5 & Mockito.java.ft | 1 - .../fileTemplates/testMeTests/TestNG & Mockito.java.ft | 1 - .../mockReturned/src/com/example/services/impl/Foo.java | 2 -- .../mockReturned/test/com/example/services/impl/FooTest.java | 3 --- .../testGroovy/com/example/services/impl/FooTest.groovy | 3 --- .../testJunit5/com/example/services/impl/FooTest.java | 3 --- .../testSpock/com/example/services/impl/FooTest.groovy | 3 --- .../testTestNg/com/example/services/impl/FooTest.java | 3 --- .../test/com/example/services/impl/FooTest.java | 4 ++++ .../testGroovy/com/example/services/impl/FooTest.groovy | 4 ++++ .../test/com/example/services/impl/FooTest.java | 1 - .../testGroovy/com/example/services/impl/FooTest.groovy | 1 - .../testJunit5/com/example/services/impl/FooTest.java | 1 - .../testTestNg/com/example/services/impl/FooTest.java | 1 - 17 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java b/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java index 881c528b..fb0b07fe 100644 --- a/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java +++ b/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java @@ -270,6 +270,9 @@ public boolean shouldStub(Method testMethod, List testedClassFields) { */ @SuppressWarnings("unused") public boolean shouldVerify(Method testMethod, List testedClassFields) { + if (!stubMockMethodCallsReturnValues) { + return false; + } boolean shouldVerity = false; for (Field testedClassField : testedClassFields) { if (isMockable(testedClassField)) { diff --git a/src/main/resources/fileTemplates/testMeTests/Groovy, JUnit4 & Mockito.groovy.ft b/src/main/resources/fileTemplates/testMeTests/Groovy, JUnit4 & Mockito.groovy.ft index 576424b6..e9f966f5 100644 --- a/src/main/resources/fileTemplates/testMeTests/Groovy, JUnit4 & Mockito.groovy.ft +++ b/src/main/resources/fileTemplates/testMeTests/Groovy, JUnit4 & Mockito.groovy.ft @@ -34,7 +34,6 @@ class ${CLASS_NAME} { #end $TAB$TAB#grRenderMethodCall($method,$TESTED_CLASS.name) - #if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields)) #grRenderMockVerifies($method,$TESTED_CLASS.fields) #end diff --git a/src/main/resources/fileTemplates/testMeTests/JUnit4 & Mockito.java.ft b/src/main/resources/fileTemplates/testMeTests/JUnit4 & Mockito.java.ft index fcb285a5..4b72fb4f 100644 --- a/src/main/resources/fileTemplates/testMeTests/JUnit4 & Mockito.java.ft +++ b/src/main/resources/fileTemplates/testMeTests/JUnit4 & Mockito.java.ft @@ -35,7 +35,6 @@ public class ${CLASS_NAME} { #end $TAB$TAB#renderMethodCall($method,$TESTED_CLASS.name) - #if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields)) #renderMockVerifies($method,$TESTED_CLASS.fields) #end diff --git a/src/main/resources/fileTemplates/testMeTests/JUnit5 & Mockito.java.ft b/src/main/resources/fileTemplates/testMeTests/JUnit5 & Mockito.java.ft index cadecbb0..4913b3ff 100644 --- a/src/main/resources/fileTemplates/testMeTests/JUnit5 & Mockito.java.ft +++ b/src/main/resources/fileTemplates/testMeTests/JUnit5 & Mockito.java.ft @@ -34,7 +34,6 @@ class ${CLASS_NAME} { #end $TAB$TAB#renderMethodCall($method,$TESTED_CLASS.name) - #if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields)) #renderMockVerifies($method,$TESTED_CLASS.fields) #end diff --git a/src/main/resources/fileTemplates/testMeTests/TestNG & Mockito.java.ft b/src/main/resources/fileTemplates/testMeTests/TestNG & Mockito.java.ft index fd5e0958..e5adcbbb 100644 --- a/src/main/resources/fileTemplates/testMeTests/TestNG & Mockito.java.ft +++ b/src/main/resources/fileTemplates/testMeTests/TestNG & Mockito.java.ft @@ -34,7 +34,6 @@ public class ${CLASS_NAME} { #end $TAB$TAB#renderMethodCall($method,$TESTED_CLASS.name) - #if($hasMocks && $MockitoMockBuilder.shouldVerify($method,$TESTED_CLASS.fields)) #renderMockVerifies($method,$TESTED_CLASS.fields) #end diff --git a/testData/testMeGenerator/mockReturned/src/com/example/services/impl/Foo.java b/testData/testMeGenerator/mockReturned/src/com/example/services/impl/Foo.java index 362f9531..6c82f3a4 100644 --- a/testData/testMeGenerator/mockReturned/src/com/example/services/impl/Foo.java +++ b/testData/testMeGenerator/mockReturned/src/com/example/services/impl/Foo.java @@ -12,10 +12,8 @@ public class Foo{ private FooFighter fooFighter; private Supplier result; - private Logger logger; public String fight(Fire withFire,String foeName) { - logger.trace("this method does not return a value so it should not be stubbed"); System.out.println(withFire); System.out.println(foeName); ConvertedBean convertedBean = fooFighter.surrender(new Fear(), new Ice(), 666); diff --git a/testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java b/testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java index c473da7b..88d1bd62 100644 --- a/testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java @@ -1,7 +1,6 @@ package com.example.services.impl; import com.example.beans.ConvertedBean; -import com.example.dependencies.Logger; import com.example.foes.Fire; import com.example.warriers.FooFighter; import org.junit.Assert; @@ -23,8 +22,6 @@ public class FooTest { FooFighter fooFighter; @Mock Supplier result; - @Mock - Logger logger; @InjectMocks Foo foo; diff --git a/testData/testMeGenerator/mockReturned/testGroovy/com/example/services/impl/FooTest.groovy b/testData/testMeGenerator/mockReturned/testGroovy/com/example/services/impl/FooTest.groovy index 64bdbfe9..5cc6dc77 100644 --- a/testData/testMeGenerator/mockReturned/testGroovy/com/example/services/impl/FooTest.groovy +++ b/testData/testMeGenerator/mockReturned/testGroovy/com/example/services/impl/FooTest.groovy @@ -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 @@ -21,8 +20,6 @@ class FooTest { FooFighter fooFighter @Mock Supplier result - @Mock - Logger logger @InjectMocks Foo foo diff --git a/testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java b/testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java index 72b80c5e..8327de76 100644 --- a/testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java @@ -1,7 +1,6 @@ package com.example.services.impl; import com.example.beans.ConvertedBean; -import com.example.dependencies.Logger; import com.example.foes.Fire; import com.example.warriers.FooFighter; import org.junit.jupiter.api.Assertions; @@ -23,8 +22,6 @@ class FooTest { FooFighter fooFighter; @Mock Supplier result; - @Mock - Logger logger; @InjectMocks Foo foo; diff --git a/testData/testMeGenerator/mockReturned/testSpock/com/example/services/impl/FooTest.groovy b/testData/testMeGenerator/mockReturned/testSpock/com/example/services/impl/FooTest.groovy index 976e0685..7ebb3014 100644 --- a/testData/testMeGenerator/mockReturned/testSpock/com/example/services/impl/FooTest.groovy +++ b/testData/testMeGenerator/mockReturned/testSpock/com/example/services/impl/FooTest.groovy @@ -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 @@ -20,8 +19,6 @@ class FooTest extends Specification { FooFighter fooFighter @Mock Supplier result - @Mock - Logger logger @InjectMocks Foo foo diff --git a/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java b/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java index ceeeea8a..61072b1f 100644 --- a/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java @@ -1,7 +1,6 @@ package com.example.services.impl; import com.example.beans.ConvertedBean; -import com.example.dependencies.Logger; import com.example.foes.Fire; import com.example.warriers.FooFighter; import org.mockito.InjectMocks; @@ -23,8 +22,6 @@ public class FooTest { FooFighter fooFighter; @Mock Supplier result; - @Mock - Logger logger; @InjectMocks Foo foo; diff --git a/testData/testMeGenerator/variousFieldTypes/test/com/example/services/impl/FooTest.java b/testData/testMeGenerator/variousFieldTypes/test/com/example/services/impl/FooTest.java index 78c43207..a8c9c531 100644 --- a/testData/testMeGenerator/variousFieldTypes/test/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/variousFieldTypes/test/com/example/services/impl/FooTest.java @@ -54,6 +54,10 @@ public void testFight() throws Exception { when(innerOfPublicInnerClass.methodOfInnerClass()).thenReturn(new Foo().new PublicInnerClass().new InnerOfPublicInnerClass()); String result = foo.fight(new com.example.foes.Fire(), "foeName"); + verify(publicInnerClass).methodOfInnerClass(); + verify(innerStaticClass).methodOfInnerClass(); + verify(innerClass).methodOfInnerClass(); + verify(anonymousPublicInnerClass).methodOfInnerClass(); Assert.assertEquals("replaceMeWithExpectedResult", result); } } diff --git a/testData/testMeGenerator/variousFieldTypes/testGroovy/com/example/services/impl/FooTest.groovy b/testData/testMeGenerator/variousFieldTypes/testGroovy/com/example/services/impl/FooTest.groovy index 9c670296..96e848e4 100644 --- a/testData/testMeGenerator/variousFieldTypes/testGroovy/com/example/services/impl/FooTest.groovy +++ b/testData/testMeGenerator/variousFieldTypes/testGroovy/com/example/services/impl/FooTest.groovy @@ -50,6 +50,10 @@ class FooTest { when(innerOfPublicInnerClass.methodOfInnerClass()).thenReturn(new com.example.services.impl.Foo.PublicInnerClass.InnerOfPublicInnerClass(new com.example.services.impl.Foo.PublicInnerClass(new com.example.services.impl.Foo()))) java.lang.String result = foo.fight(new com.example.foes.Fire(), "foeName") + verify(publicInnerClass).methodOfInnerClass() + verify(innerStaticClass).methodOfInnerClass() + verify(innerClass).methodOfInnerClass() + verify(anonymousPublicInnerClass).methodOfInnerClass() assert result == "replaceMeWithExpectedResult" } } diff --git a/testData/testMeGenerator/verifyMethodCall/test/com/example/services/impl/FooTest.java b/testData/testMeGenerator/verifyMethodCall/test/com/example/services/impl/FooTest.java index 024047e5..a3db85f5 100644 --- a/testData/testMeGenerator/verifyMethodCall/test/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/verifyMethodCall/test/com/example/services/impl/FooTest.java @@ -37,7 +37,6 @@ public void testFight() throws Exception { when(result.get()).thenReturn(Integer.valueOf(0)); String result = foo.fight(new Fire(), "foeName"); - verify(techFighter).initSelfArming(anyString()); Assert.assertEquals("replaceMeWithExpectedResult", result); } diff --git a/testData/testMeGenerator/verifyMethodCall/testGroovy/com/example/services/impl/FooTest.groovy b/testData/testMeGenerator/verifyMethodCall/testGroovy/com/example/services/impl/FooTest.groovy index e0f6f775..cd787760 100644 --- a/testData/testMeGenerator/verifyMethodCall/testGroovy/com/example/services/impl/FooTest.groovy +++ b/testData/testMeGenerator/verifyMethodCall/testGroovy/com/example/services/impl/FooTest.groovy @@ -33,7 +33,6 @@ class FooTest { when(result.get()).thenReturn(0) String result = foo.fight(new Fire(), "foeName") - verify(techFighter).initSelfArming(anyString()) assert result == "replaceMeWithExpectedResult" } diff --git a/testData/testMeGenerator/verifyMethodCall/testJunit5/com/example/services/impl/FooTest.java b/testData/testMeGenerator/verifyMethodCall/testJunit5/com/example/services/impl/FooTest.java index 6e9abfe7..8d0c0725 100644 --- a/testData/testMeGenerator/verifyMethodCall/testJunit5/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/verifyMethodCall/testJunit5/com/example/services/impl/FooTest.java @@ -37,7 +37,6 @@ void testFight() { when(result.get()).thenReturn(Integer.valueOf(0)); String result = foo.fight(new Fire(), "foeName"); - verify(techFighter).initSelfArming(anyString()); Assertions.assertEquals("replaceMeWithExpectedResult", result); } diff --git a/testData/testMeGenerator/verifyMethodCall/testTestNg/com/example/services/impl/FooTest.java b/testData/testMeGenerator/verifyMethodCall/testTestNg/com/example/services/impl/FooTest.java index 0b0de813..25c95234 100644 --- a/testData/testMeGenerator/verifyMethodCall/testTestNg/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/verifyMethodCall/testTestNg/com/example/services/impl/FooTest.java @@ -37,7 +37,6 @@ public void testFight() { when(result.get()).thenReturn(Integer.valueOf(0)); String result = foo.fight(new Fire(), "foeName"); - verify(techFighter).initSelfArming(anyString()); Assert.assertEquals(result, "replaceMeWithExpectedResult"); } From c652fbd661b07dd28eddc2190500726989563894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E4=BA=AC-=E9=BB=84=E4=BA=AE?= Date: Thu, 7 Mar 2024 09:31:43 +0800 Subject: [PATCH 4/8] Code optimization and adjustment --- .../intellij/template/context/Method.java | 10 +++- .../template/context/MockitoMockBuilder.java | 47 +++++++++---------- .../testMeIncludes/TestMe macros.groovy.ft | 4 +- .../testMeIncludes/TestMe macros.java.ft | 6 +-- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/weirddev/testme/intellij/template/context/Method.java b/src/main/java/com/weirddev/testme/intellij/template/context/Method.java index 4508efbe..f959b6bc 100644 --- a/src/main/java/com/weirddev/testme/intellij/template/context/Method.java +++ b/src/main/java/com/weirddev/testme/intellij/template/context/Method.java @@ -138,10 +138,18 @@ public class Method { /** * - * true - if method has a return type + * true - if method has a return type and not void */ public boolean hasReturn(){ return returnType != null && !"void".equals(returnType.getName()); } + /** + * + * @return true - if the method has a void return type + */ + public boolean hasVoidReturn() { + return returnType != null && "void".equals(returnType.getName()); + } + } diff --git a/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java b/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java index fb0b07fe..a53183e9 100644 --- a/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java +++ b/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.function.Predicate; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -245,22 +246,7 @@ private String deductMatcherTypeMethod(Param param, Language language) { */ @SuppressWarnings("unused") public boolean shouldStub(Method testMethod, List testedClassFields) { - boolean shouldStub = false; - if (stubMockMethodCallsReturnValues) { - for (Field testedClassField : testedClassFields) { - if (isMockable(testedClassField)) { - LOG.debug("field "+testedClassField.getName()+" type "+testedClassField.getType().getCanonicalName()+" type methods:"+testedClassField.getType().getMethods().size()); - for (Method fieldMethod : testedClassField.getType().getMethods()) { - if (fieldMethod.getReturnType() != null && !"void".equals(fieldMethod.getReturnType().getCanonicalName()) && testSubjectInspector.isMethodCalled(fieldMethod, testMethod)) { - shouldStub = true; - break; - } - } - } - } - } - LOG.debug("method "+testMethod.getMethodId()+" should be stabbed:"+shouldStub); - return shouldStub; + return callsMockMethod(testMethod, testedClassFields, Method::hasReturn); } /** @@ -270,22 +256,31 @@ public boolean shouldStub(Method testMethod, List testedClassFields) { */ @SuppressWarnings("unused") public boolean shouldVerify(Method testMethod, List testedClassFields) { + return callsMockMethod(testMethod, testedClassFields, Method::hasVoidReturn); + } + + private boolean callsMockMethod(Method testMethod, List testedClassFields, + Predicate mockMethodRelevant) { + boolean callsMockMethod = false; if (!stubMockMethodCallsReturnValues) { - return false; + LOG.debug("method " + testMethod.getMethodId() + " is calling a mock method:" + callsMockMethod); + return callsMockMethod; } - boolean shouldVerity = false; for (Field testedClassField : testedClassFields) { - if (isMockable(testedClassField)) { - for (Method fieldMethod : testedClassField.getType().getMethods()) { - if (fieldMethod.getReturnType() != null && "void".equals(fieldMethod.getReturnType().getCanonicalName()) && testSubjectInspector.isMethodCalled(fieldMethod, testMethod)) { - shouldVerity = true; - break; - } + if (!isMockable(testedClassField)) { + continue; + } + LOG.debug("field " + testedClassField.getName() + " type " + testedClassField.getType().getCanonicalName() + + " type methods:" + testedClassField.getType().getMethods().size()); + for (Method fieldMethod : testedClassField.getType().getMethods()) { + if (mockMethodRelevant.test(fieldMethod) + && testSubjectInspector.isMethodCalled(fieldMethod, testMethod)) { + return true; } } } - LOG.debug("method "+testMethod.getMethodId()+" should be verified:"+shouldVerity); - return shouldVerity; + LOG.debug("method " + testMethod.getMethodId() + " is calling a mock method:" + callsMockMethod); + return callsMockMethod; } /** diff --git a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft index 110267f6..49085f5b 100644 --- a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft +++ b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft @@ -96,7 +96,7 @@ result#if($TestSubjectUtils.isJavaFuture($method.returnType)).get()#end == $Test #foreach($field in $testedClassFields) #if($MockitoMockBuilder.isMockable($field)) #foreach($fieldMethod in $field.type.methods) -#if($fieldMethod.returnType && $fieldMethod.returnType.name !="void" && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) +#if($fieldMethod.hasReturn() && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) when($field.name.${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java"))).thenReturn($TestBuilder.renderReturnParam($method,$fieldMethod.returnType,"${fieldMethod.name}Response",$grReplacementTypes,$grDefaultTypeValues)) #end #end @@ -108,7 +108,7 @@ result#if($TestSubjectUtils.isJavaFuture($method.returnType)).get()#end == $Test #foreach($field in $testedClassFields) #if($MockitoMockBuilder.isMockable($field)) #foreach($fieldMethod in $field.type.methods) -#if($fieldMethod.returnType && $fieldMethod.returnType.name =="void" && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) +#if($fieldMethod.hasVoidReturn() && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) verify($field.name).${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java")) #end #end diff --git a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft index 047edb96..61c31ae8 100644 --- a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft +++ b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft @@ -88,7 +88,7 @@ assertEquals(result#{if}($TestSubjectUtils.isJavaFuture($method.returnType)).get #foreach($field in $testedClassFields) #if($MockitoMockBuilder.isMockable($field)) #foreach($fieldMethod in $field.type.methods) -#if($fieldMethod.returnType && $fieldMethod.returnType.name !="void" && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) +#if($fieldMethod.hasReturn() && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) when($field.name.${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java"))).thenReturn($TestBuilder.renderReturnParam($method,$fieldMethod.returnType,"${fieldMethod.name}Response",$replacementTypes,$defaultTypeValues)); #end #end @@ -96,11 +96,11 @@ assertEquals(result#{if}($TestSubjectUtils.isJavaFuture($method.returnType)).get #end #end ## -#macro(renderMockVerifies $method $$testedClassFields) +#macro(renderMockVerifies $method $testedClassFields) #foreach($field in $testedClassFields) #if($MockitoMockBuilder.isMockable($field)) #foreach($fieldMethod in $field.type.methods) -#if($fieldMethod.returnType && $fieldMethod.returnType.name =="void" && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) +#if($fieldMethod.hasVoidReturn() && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) verify($field.name).${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java")); #end #end From fe0aff440f71124c9198caa259bb8933e19f70dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E4=BA=AC-=E9=BB=84=E4=BA=AE?= Date: Thu, 7 Mar 2024 09:46:17 +0800 Subject: [PATCH 5/8] add the logger back --- .../mockReturned/src/com/example/services/impl/Foo.java | 2 ++ .../mockReturned/test/com/example/services/impl/FooTest.java | 4 ++++ .../testGroovy/com/example/services/impl/FooTest.groovy | 4 ++++ .../testJunit5/com/example/services/impl/FooTest.java | 4 ++++ .../testSpock/com/example/services/impl/FooTest.groovy | 3 +++ .../testTestNg/com/example/services/impl/FooTest.java | 3 +++ 6 files changed, 20 insertions(+) diff --git a/testData/testMeGenerator/mockReturned/src/com/example/services/impl/Foo.java b/testData/testMeGenerator/mockReturned/src/com/example/services/impl/Foo.java index 6c82f3a4..362f9531 100644 --- a/testData/testMeGenerator/mockReturned/src/com/example/services/impl/Foo.java +++ b/testData/testMeGenerator/mockReturned/src/com/example/services/impl/Foo.java @@ -12,8 +12,10 @@ public class Foo{ private FooFighter fooFighter; private Supplier result; + private Logger logger; public String fight(Fire withFire,String foeName) { + logger.trace("this method does not return a value so it should not be stubbed"); System.out.println(withFire); System.out.println(foeName); ConvertedBean convertedBean = fooFighter.surrender(new Fear(), new Ice(), 666); diff --git a/testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java b/testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java index 88d1bd62..5cddbb56 100644 --- a/testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java @@ -1,6 +1,7 @@ package com.example.services.impl; import com.example.beans.ConvertedBean; +import com.example.dependencies.Logger; import com.example.foes.Fire; import com.example.warriers.FooFighter; import org.junit.Assert; @@ -22,6 +23,8 @@ public class FooTest { FooFighter fooFighter; @Mock Supplier result; + @Mock + Logger logger; @InjectMocks Foo foo; @@ -36,6 +39,7 @@ 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); } } diff --git a/testData/testMeGenerator/mockReturned/testGroovy/com/example/services/impl/FooTest.groovy b/testData/testMeGenerator/mockReturned/testGroovy/com/example/services/impl/FooTest.groovy index 5cc6dc77..6fd7bf1c 100644 --- a/testData/testMeGenerator/mockReturned/testGroovy/com/example/services/impl/FooTest.groovy +++ b/testData/testMeGenerator/mockReturned/testGroovy/com/example/services/impl/FooTest.groovy @@ -1,6 +1,7 @@ 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 @@ -20,6 +21,8 @@ class FooTest { FooFighter fooFighter @Mock Supplier result + @Mock + Logger logger @InjectMocks Foo foo @@ -34,6 +37,7 @@ class FooTest { when(result.get()).thenReturn(0) String result = foo.fight(new Fire(), "foeName") + verify(logger).trace(anyString()) assert result == "replaceMeWithExpectedResult" } } diff --git a/testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java b/testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java index 8327de76..1f917260 100644 --- a/testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java @@ -1,6 +1,7 @@ package com.example.services.impl; import com.example.beans.ConvertedBean; +import com.example.dependencies.Logger; import com.example.foes.Fire; import com.example.warriers.FooFighter; import org.junit.jupiter.api.Assertions; @@ -22,6 +23,8 @@ class FooTest { FooFighter fooFighter; @Mock Supplier result; + @Mock + Logger logger; @InjectMocks Foo foo; @@ -36,6 +39,7 @@ void testFight() { when(result.get()).thenReturn(Integer.valueOf(0)); String result = foo.fight(new Fire(), "foeName"); + verify(logger).trace(anyString()); Assertions.assertEquals("replaceMeWithExpectedResult", result); } } diff --git a/testData/testMeGenerator/mockReturned/testSpock/com/example/services/impl/FooTest.groovy b/testData/testMeGenerator/mockReturned/testSpock/com/example/services/impl/FooTest.groovy index 7ebb3014..976e0685 100644 --- a/testData/testMeGenerator/mockReturned/testSpock/com/example/services/impl/FooTest.groovy +++ b/testData/testMeGenerator/mockReturned/testSpock/com/example/services/impl/FooTest.groovy @@ -1,6 +1,7 @@ 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 @@ -19,6 +20,8 @@ class FooTest extends Specification { FooFighter fooFighter @Mock Supplier result + @Mock + Logger logger @InjectMocks Foo foo diff --git a/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java b/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java index 61072b1f..ceeeea8a 100644 --- a/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java @@ -1,6 +1,7 @@ package com.example.services.impl; import com.example.beans.ConvertedBean; +import com.example.dependencies.Logger; import com.example.foes.Fire; import com.example.warriers.FooFighter; import org.mockito.InjectMocks; @@ -22,6 +23,8 @@ public class FooTest { FooFighter fooFighter; @Mock Supplier result; + @Mock + Logger logger; @InjectMocks Foo foo; From 1ef6c3470791749688433ba9c31f3c106520cc60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E4=BA=AC-=E9=BB=84=E4=BA=AE?= Date: Thu, 7 Mar 2024 09:57:41 +0800 Subject: [PATCH 6/8] add the logger back --- .../testTestNg/com/example/services/impl/FooTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java b/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java index ceeeea8a..0e389647 100644 --- a/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java +++ b/testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java @@ -39,6 +39,7 @@ 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"); } } From bf8f57923f9bed984b31e6d4bdea1536f905332e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E4=BA=AC-=E9=BB=84=E4=BA=AE?= Date: Fri, 8 Mar 2024 18:58:59 +0800 Subject: [PATCH 7/8] delete hasVoidReturn method --- .../weirddev/testme/intellij/template/context/Method.java | 8 -------- .../fileTemplates/testMeIncludes/TestMe macros.groovy.ft | 2 +- .../fileTemplates/testMeIncludes/TestMe macros.java.ft | 2 +- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/weirddev/testme/intellij/template/context/Method.java b/src/main/java/com/weirddev/testme/intellij/template/context/Method.java index f959b6bc..111d8288 100644 --- a/src/main/java/com/weirddev/testme/intellij/template/context/Method.java +++ b/src/main/java/com/weirddev/testme/intellij/template/context/Method.java @@ -144,12 +144,4 @@ public boolean hasReturn(){ return returnType != null && !"void".equals(returnType.getName()); } - /** - * - * @return true - if the method has a void return type - */ - public boolean hasVoidReturn() { - return returnType != null && "void".equals(returnType.getName()); - } - } diff --git a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft index 49085f5b..975c4604 100644 --- a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft +++ b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.groovy.ft @@ -108,7 +108,7 @@ result#if($TestSubjectUtils.isJavaFuture($method.returnType)).get()#end == $Test #foreach($field in $testedClassFields) #if($MockitoMockBuilder.isMockable($field)) #foreach($fieldMethod in $field.type.methods) -#if($fieldMethod.hasVoidReturn() && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) +#if(!$fieldMethod.hasReturn() && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) verify($field.name).${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java")) #end #end diff --git a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft index 61c31ae8..044f6393 100644 --- a/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft +++ b/src/main/resources/fileTemplates/testMeIncludes/TestMe macros.java.ft @@ -100,7 +100,7 @@ assertEquals(result#{if}($TestSubjectUtils.isJavaFuture($method.returnType)).get #foreach($field in $testedClassFields) #if($MockitoMockBuilder.isMockable($field)) #foreach($fieldMethod in $field.type.methods) -#if($fieldMethod.hasVoidReturn() && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) +#if(!$fieldMethod.hasReturn() && $TestSubjectUtils.isMethodCalled($fieldMethod,$method)) verify($field.name).${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java")); #end #end From aae1d5720277f4841c0d2d4180fbcedeebc74f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=97=E4=BA=AC-=E9=BB=84=E4=BA=AE?= Date: Fri, 8 Mar 2024 19:04:45 +0800 Subject: [PATCH 8/8] delete hasVoidReturn method --- .../testme/intellij/template/context/MockitoMockBuilder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java b/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java index a53183e9..177bc2b2 100644 --- a/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java +++ b/src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java @@ -256,7 +256,7 @@ public boolean shouldStub(Method testMethod, List testedClassFields) { */ @SuppressWarnings("unused") public boolean shouldVerify(Method testMethod, List testedClassFields) { - return callsMockMethod(testMethod, testedClassFields, Method::hasVoidReturn); + return callsMockMethod(testMethod, testedClassFields, method -> !method.hasReturn()); } private boolean callsMockMethod(Method testMethod, List testedClassFields,