Skip to content

Commit

Permalink
Merge pull request #4 from zhangfj88/zfj_task
Browse files Browse the repository at this point in the history
add specific type to Mockito argument matcher any(),and  work for array types
  • Loading branch information
zhangfj88 authored Mar 26, 2024
2 parents dc23b0f + cada609 commit f7ab68e
Show file tree
Hide file tree
Showing 30 changed files with 82 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,31 +227,26 @@ public String buildMockArgsMatchers(List<Param> params,String language) {
*/
@NotNull
private String deductMatcherTypeMethod(Param param, Language language) {
String matcherType;
if (param.getType().isVarargs()) {
matcherType = "anyVararg";
Type type = param.getType();
String matcherMethod = resolveMatcherMethod(type);
if (language == Language.Scala) {
return matcherMethod;
}
else {
matcherType = TYPE_TO_ARG_MATCHERS.get(param.getType().getCanonicalName());
}
if (matcherType == null) {
matcherType = "any";
}
//todo support anyCollection(),anyMap(),anySet() and consider arrays
if (language != Language.Scala) {
matcherType += "()";
}
// add specific type to any()
if( matcherType.equals("any()")){
matcherType = addSpecificType(param.getType().getName());
else if (!type.isPrimitive() && "any".equals(matcherMethod)) {
return matcherMethod + "("+ type.getCanonicalName()+(type.isArray()? "[]":"") +".class)";
} else {
return matcherMethod+"()";
}
return matcherType;
}

@SuppressWarnings("unused")
@Deprecated
public boolean shouldStub(Method testMethod, List<Field> testedClassFields) {
return callsMockMethod(testMethod, testedClassFields, Method::hasReturn, null);
private static String resolveMatcherMethod(Type type) {
if (type.isVarargs()) {
return "anyVararg";
} else if(!type.isArray() && TYPE_TO_ARG_MATCHERS.containsKey(type.getCanonicalName())){
return TYPE_TO_ARG_MATCHERS.get(type.getCanonicalName());
} else {
return "any";
}
}

String addSpecificType(String typeName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void setUp() {

@Test
public void testFight() throws Exception {
when(fooFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighter.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");

String result = foo.fight(new com.example.foes.Fire(), "foeName");
Assert.assertEquals("replaceMeWithExpectedResult", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FooTest {

@Test
void testFight() {
when(fooFighter.fight(any(Fire.class))).thenReturn("fightResponse")
when(fooFighter.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse")

java.lang.String result = foo.fight(new com.example.foes.Fire(), "foeName")
assert result == "replaceMeWithExpectedResult"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void setUp() {

@Test
public void testFight() throws Exception {
when(fooFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighter.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");

String result = foo.fight(new com.example.foes.Fire(), "foeName");
Assert.assertEquals("replaceMeWithExpectedResult", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FooTest {

@Test
void testFight() {
when(fooFighter.fight(any(Fire.class))).thenReturn("fightResponse")
when(fooFighter.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse")

java.lang.String result = foo.fight(new com.example.foes.Fire(), "foeName")
assert result == "replaceMeWithExpectedResult"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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
import org.junit.Test
import org.junit.Before
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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
import org.junit.Test
import org.junit.Before
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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
import org.junit.Test
import org.junit.Before
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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;
import com.example.warriers.FooFighter;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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;
import com.example.warriers.FooFighter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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;
import com.example.warriers.FooFighter;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public void setUp() {

@Test
public void testFight() throws Exception {
when(fooFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterProtected.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterDefault.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterPublic.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterFinal.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterStatic.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighter.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterProtected.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterDefault.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterPublic.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterFinal.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterStatic.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(innerOfPublicInnerClass.methodOfInnerClass()).thenReturn(new Foo().new PublicInnerClass().new InnerOfPublicInnerClass());

String result = foo.fight(new com.example.foes.Fire(), "foeName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class FooTest {

@Test
void testFight() {
when(fooFighter.fight(any(Fire.class))).thenReturn("fightResponse")
when(fooFighterProtected.fight(any(Fire.class))).thenReturn("fightResponse")
when(fooFighterDefault.fight(any(Fire.class))).thenReturn("fightResponse")
when(fooFighterPublic.fight(any(Fire.class))).thenReturn("fightResponse")
when(fooFighterFinal.fight(any(Fire.class))).thenReturn("fightResponse")
when(fooFighterStatic.fight(any(Fire.class))).thenReturn("fightResponse")
when(fooFighter.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse")
when(fooFighterProtected.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse")
when(fooFighterDefault.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse")
when(fooFighterPublic.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse")
when(fooFighterFinal.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse")
when(fooFighterStatic.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse")
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public void setUp() {

@Test
public void testFight() throws Exception {
when(fooFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterProtected.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterDefault.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterPublic.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterFinal.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighterStatic.fight(any(Fire.class))).thenReturn("fightResponse");
when(fooFighter.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterProtected.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterDefault.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterPublic.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterFinal.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(fooFighterStatic.fight(any(com.example.foes.Fire.class))).thenReturn("fightResponse");
when(innerOfPublicInnerClass.methodOfInnerClass()).thenReturn(new Foo().new PublicInnerClass().new InnerOfPublicInnerClass());
String result = foo.fight(new com.example.foes.Fire(), "foeName");
verify(publicInnerClass).methodOfInnerClass();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.Assert;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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
import org.junit.Test
import org.junit.Before
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.services.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;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down

0 comments on commit f7ab68e

Please sign in to comment.