Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add specific type to Mockito argument matcher any() #27

Merged
merged 15 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep this method for now. Although its not used. some users might still use it in their custom templates. so it's only marked as @deprecated for now

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
Loading