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

Feature/add verify #23

Merged
merged 9 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -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;

Expand Down Expand Up @@ -47,4 +49,7 @@ public void testUtilWithoutAccessableCtor() {
doTest(true, true, true);
}

public void testVerifyMethodCall() {
doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ public void testUtilWithoutAccessableCtor() {
doTest(true, true, true);
}

public void testVerifyMethodCall() {
doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public void testUtilWithoutAccessableCtor() {
doTest(true, true, true);
}

public void testVerifyMethodCall() {
doTest(new FileTemplateConfig(TestMeConfigPersistent.getInstance().getState()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,31 @@ public boolean shouldStub(Method testMethod, List<Field> 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<Field> testedClassFields) {
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
if (!stubMockMethodCallsReturnValues) {
return false;
}
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
verify($field.name).${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java"))
#end
#end
#end
#end
#end
Original file line number Diff line number Diff line change
Expand Up @@ -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))
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved
verify($field.name).${fieldMethod.name}($MockitoMockBuilder.buildMockArgsMatchers(${fieldMethod.methodParams},"Java"));
#end
#end
#end
#end
#end
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ 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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ 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
Expand Down
13 changes: 13 additions & 0 deletions testData/commonSrc/com/example/warriers/TechFighter.java
Original file line number Diff line number Diff line change
@@ -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);
}
31 changes: 31 additions & 0 deletions testData/commonSrc/com/example/warriers/impl/TechFighterImpl.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ public class Foo{

private FooFighter fooFighter;
private Supplier<Integer> result;
private Logger logger;
huangliang992 marked this conversation as resolved.
Show resolved Hide resolved

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);
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.Fire;
import com.example.warriers.FooFighter;
import org.junit.Assert;
Expand All @@ -23,8 +22,6 @@ public class FooTest {
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 @@ -21,8 +20,6 @@ class FooTest {
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.Fire;
import com.example.warriers.FooFighter;
import org.junit.jupiter.api.Assertions;
Expand All @@ -23,8 +22,6 @@ class FooTest {
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 @@ -20,8 +19,6 @@ 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.Fire;
import com.example.warriers.FooFighter;
import org.mockito.InjectMocks;
Expand All @@ -23,8 +22,6 @@ public class FooTest {
FooFighter fooFighter;
@Mock
Supplier<Integer> result;
@Mock
Logger logger;
@InjectMocks
Foo foo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer> 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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<Integer> 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
Loading
Loading