Skip to content

Commit

Permalink
Merge pull request #27 from huangliang992/feature/add-specific-type-t…
Browse files Browse the repository at this point in the history
…o-any()

Generate Mockito stub matcher with relevant type class: any(<Type>.class) instead of any()
  • Loading branch information
yaronyam authored Mar 28, 2024
2 parents 530a959 + 7517edc commit c33c108
Show file tree
Hide file tree
Showing 38 changed files with 141 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,30 +240,33 @@ 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";
else if (!type.isPrimitive() && "any".equals(matcherMethod)) {
return matcherMethod + "("+ type.getCanonicalName()+(type.isArray()? "[]":"") +".class)";
} else {
return matcherMethod+"()";
}
//todo support anyCollection(),anyMap(),anySet() and consider arrays
if (language != Language.Scala) {
matcherType += "()";
}

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";
}
return matcherType;
}

@SuppressWarnings("unused")
@Deprecated
public boolean shouldStub(Method testMethod, List<Field> testedClassFields) {
return callsMockMethod(testMethod, testedClassFields, Method::hasReturn, null);
}


/**
* true - if should stub tested method
* @param testMethod method being tested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void setUp() {

@Test
public void testMethodOfInnerClass() throws Exception {
when(innerFooFighter.fight(any())).thenReturn("fightResponse");
when(innerFooFighter.fight(any(Fire.class))).thenReturn("fightResponse");

String result = innerStaticClassWithMember.methodOfInnerClass(new Fire());
Assert.assertEquals("replaceMeWithExpectedResult", result);
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())).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())).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())).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())).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 Expand Up @@ -33,8 +35,8 @@ public void setUp() {

@Test
public void testFight() throws Exception {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
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 Expand Up @@ -29,8 +31,8 @@ class FooTest {

@Test
void testFight() {
when(techFighter.fight(any())).thenReturn("fightResponse")
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse")
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(result.get()).thenReturn(0)

String result = foo.fight(new Fire(), "foeName")
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 Expand Up @@ -33,8 +35,8 @@ void setUp() {

@Test
void testFight() {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
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 Expand Up @@ -42,8 +44,8 @@ public void setUp() {

@Test
public void testFight() throws Exception {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));
String result = foo.fight(new Fire(), "foeName");
verify(techFighter).initSelfArming(anyString());
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 spock.lang.*
import org.mockito.InjectMocks
Expand All @@ -27,8 +29,8 @@ class FooTest extends Specification {

def "test fight"() {
given:
when(techFighter.fight(any())).thenReturn("fightResponse")
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse")
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(result.get()).thenReturn(0)

when:
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 Expand Up @@ -33,8 +35,8 @@ public void setUp() {

@Test
public void testFight() {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
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 Expand Up @@ -32,8 +34,8 @@ public void setUp() {

@Test
public void testFight() throws Exception {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
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 All @@ -28,8 +30,8 @@ class FooTest {

@Test
void testFight() {
when(techFighter.fight(any())).thenReturn("fightResponse")
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse")
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(result.get()).thenReturn(0)

String result = foo.fight(new Fire(), "foeName")
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 Expand Up @@ -32,8 +34,8 @@ void setUp() {

@Test
void testFight() {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
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 spock.lang.*
import org.mockito.InjectMocks
Expand All @@ -26,8 +28,8 @@ class FooTest extends Specification {

def "test fight"() {
given:
when(techFighter.fight(any())).thenReturn("fightResponse")
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse")
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(result.get()).thenReturn(0)

when:
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 Expand Up @@ -32,8 +34,8 @@ public void setUp() {

@Test
public void testFight() {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
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 Expand Up @@ -33,8 +35,8 @@ public void setUp() {

@Test
public void testFight() throws Exception {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
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 Expand Up @@ -29,8 +31,8 @@ class FooTest {

@Test
void testFight() {
when(techFighter.fight(any())).thenReturn("fightResponse")
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse")
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean(myString: "myString", someNum: 0))
when(result.get()).thenReturn(0)

String result = foo.fight(new Fire(), "foeName")
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 Expand Up @@ -33,8 +35,8 @@ void setUp() {

@Test
void testFight() {
when(techFighter.fight(any())).thenReturn("fightResponse");
when(techFighter.surrender(any(), any(), anyInt())).thenReturn(new ConvertedBean());
when(techFighter.fight(any(Fire.class))).thenReturn("fightResponse");
when(techFighter.surrender(any(Fear.class), any(Ice.class), anyInt())).thenReturn(new ConvertedBean());
when(result.get()).thenReturn(Integer.valueOf(0));

String result = foo.fight(new Fire(), "foeName");
Expand Down
Loading

0 comments on commit c33c108

Please sign in to comment.