这是一个很神奇的错误。

常规的出错是因为在mock方法里,其中某一个或者几个参数使用了EasyMock.anyxx(),而其他的使用了具体的值。

java.lang.IllegalStateException: 1 matchers expected, 5 recorded.
This exception usually occurs when matchers are mixed with raw values when recording a method:
foo(5, eq(6)); // wrong
You need to use no matcher at all or a matcher for every single param:
foo(eq(5), eq(6)); // right
foo(5, 6); // also right
at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:52)
at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:41)
at org.easymock.internal.RecordState.invoke(RecordState.java:51)
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:40)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:101)
at org.easymock.internal.ClassProxyFactory$MockMethodInterceptor.intercept(ClassProxyFactory.java:97)
at com.unit_test_easymock_mockito.database.MyDatabase$$EnhancerByCGLIB$$f0fe0b8a.updateBook(<generated>)
at com.unit_test_easymock_mockito.zexception.BookDaoImplTest.updateBookTest(BookDaoImplTest.java:85)

但本例中出现的错误并不是这个原因,附上代码:

/**
* 更新书本信息 单元测试
*/
@Test
public void updateBookTest() { Book book = new Book(); myDatabase.updateBook(EasyMock.anyObject());
EasyMock.expectLastCall(); mockControl.replay(); bookDaoImpl.updateBook(book); mockControl.verify();
}

可以看到,这里mock的myDatabase.updateBook(EasyMock.anyObject());只有一个参数,不存在一个为anyObject(),一个为具体值的情况。

并且,单独执行junit是正常通过的:

但通过maven clean install打包时,就报错了。

我们来通过debug的方式,看看错误到底出在了什么地方。

可以看到在这里报错了,错误信息也和之前报出的一致。

再仔细分析,发现是这一句代码导致的抛异常:

if (matchers.size() != invocation.getArguments().length) {
invocation.getArguments().length是调用方法的参数个数,那么matchers.size()又是什么呢,从哪里获取的呢?
继续分析代码:
public ExpectedInvocation(Invocation invocation, List<IArgumentMatcher> matchers) {
this.invocation = invocation;
this.matchers = createMissingMatchers(invocation, matchers);
}
createMissingMatchers方法是在ExpectedInvocation构造方法里调用的,再来看:
public Object invoke(Invocation invocation) {
closeMethod();
List<IArgumentMatcher> lastMatchers = LastControl.pullMatchers();
lastInvocation = new ExpectedInvocation(invocation, lastMatchers);
lastInvocationUsed = false;
return emptyReturnValueFor(invocation.getMethod().getReturnType());
}

在invoke方法里,会将matchers传递给ExpectedInvocation,而matchers是通过LastControl.pullMatchers()获取的:

public static List<IArgumentMatcher> pullMatchers() {
List<IArgumentMatcher> stack = threadToArgumentMatcherStack.get();
if (stack == null) {
return null;
}
threadToArgumentMatcherStack.remove();
return new ArrayList<>(stack);
}

这里可以看出,matchers最终是从threadToArgumentMatcherStack里获取的,并且,在获取后,会及时的threadToArgumentMatcherStack.remove();

再来了解下threadToArgumentMatcherStack是什么以及matchers是什么时候放到threadToArgumentMatcherStack里的:

private static final ThreadLocal<List<IArgumentMatcher>> threadToArgumentMatcherStack = new ThreadLocal<>();

threadToArgumentMatcherStack是一个ThreadLocal

public static void reportMatcher(IArgumentMatcher matcher) {
List<IArgumentMatcher> stack = threadToArgumentMatcherStack.get();
if (stack == null) {
stack = new ArrayList<>(5); // methods of more than 5 parameters are quite rare
threadToArgumentMatcherStack.set(stack);
}
stack.add(matcher);
}

在reportMatcher方法里,会把matchers放到threadToArgumentMatcherStack里。

那么,reportMatcher方法又是在什么时候调用的呢?

可以看出,在进行对参数的Mock的时候,anyxx(),都会调用,添加matcher。

那么问题来了,在pullMatchers()方法里,每次获取时都会及时进行清空,为什么出现“1 matchers expected, 5 recorded.”,明明只有一个参数,matchers却被添加了5次的情况?

思考分析一下,发现有一种可能性,就是调用了reportMatcher方法,但是没有调用pullMatchers()方法,这样,对于ThreadLocal类型的threadToArgumentMatcherStack,在同一个线程里,matchers会一直增加。

但对于EasyMock来说,Mock一个方法必然会调用pullMatchers()方法,那么是不是可能没有使用EasyMock来Mock方法,但却使用了EasyMock.anyObject()来Mock参数了呢?

结果果然如此:

@Test
public void queryBookByIdTest() { Integer id = 1; Mockito.when(bookDao.queryBookById(EasyMock.anyObject())).thenReturn(null); bookServiceImpl.queryBookById(id);
}

在这个单元测试里,使用了Mockito来mock方法,却同时使用了EasyMock.anyObject()来mock参数,这样就导致执行到真正EasyMock来mock方法的时候,出问题了,matchers和方法的参数对应不上,导致了问题出现。

最后还存在一个疑问:为什么单独执行单元测试不会报错,而执行clean install打包时就报出这个错呢?

个人推测执行单元测试时,是对各个单元测试方法单个执行的,即单元测试之间不会相互影响;而clean install打包时,所有的单元测试方法在同一个线程里具有相同的上下文,导致了问题出现。

所以说,在项目里写单元测试时,尽量只使用一种单元测试框架,混合使用多种单元测试框架,可能会造成很神奇的问题出现。

java.lang.IllegalStateException: 1 matchers expected, 5 recorded.的更多相关文章

  1. Caused by: java.lang.IllegalStateException: Expected raw type form of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$Match

    spring 4.0.2,mybatis 3.2.6,aspectjweaver 1.8.10 使用的时候,报错: Caused by: java.lang.IllegalStateException ...

  2. 解决java.lang.IllegalStateException: The application’s PagerAdapter changed the adapter’s content

    A界面中有viewpager的动态加载,从界面A跳到界面B,再finish掉B返回A时报出此异常. java.lang.IllegalStateException: The application's ...

  3. myeclipse 无法启动 java.lang.IllegalStateException: Unable to acquire application service. Ensure that the org.eclipse.core.runtime bundle is resolved and started (see config.ini).

    把myeclipse10 按照目录完整拷贝到了另外一台电脑, 另外的目录 原安装目录 D\:\soft\i\myeclipse10 新安装目录 E\:\soft\myeclipse10 双击启动失败, ...

  4. java.lang.IllegalStateException:Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx...}: java.lang.IllegalSta ...

  5. java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

    java.lang.IllegalStateException: Not allowed to create transaction on sharedEntityManager - use Spri ...

  6. java.lang.IllegalStateException: getOutputStream() has already been called for this response

    ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exceptionjava.lang ...

  7. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response

    1. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this ...

  8. eclipse启动报错java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' befo

    报错: java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invo ...

  9. java.lang.IllegalStateException: Couldn't read row 1, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data fr

    Android中操作Sqlite遇到的错误:java.lang.IllegalStateException: Couldn't read row 1, col 0 from CursorWindow. ...

随机推荐

  1. leetcode-80-删除排序数组中的重复项②

    题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums: List[int]) -> int: nums.reverse() f ...

  2. c语言学习笔记 - 二进制文件

    在进行文件操作的时候,有时候是用文本的形式存在文件里面,例如用 fprintf(fp,"%d",123) 存一个数据123,实际的存储是已1,2,3这3个ASCII码存入,打开文件 ...

  3. JavaScript中数组的集合和映射

    集合 集合(set)是在ES6中引入的一种数据结构,用于表示唯一值的集合,所以它不能包含重复值.接 下来这一小节,就让我们具体来看一下这种新的数据结构. Set集合是一种无重复元素的列表,这是这种数据 ...

  4. System.Web.Mvc.HttpUnauthorizedResult.cs

    ylbtech-System.Web.Mvc.HttpUnauthorizedResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neut ...

  5. codeforces 1136E-Nastya Hasn't Written a Legend

    传送门:QAQQAQ 题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比 ...

  6. Spring注解驱动(下)

    9.@PropertySource 加载配置文件 在xml中 我们加载property配置文件,是使用 <context:property-placeholder location=" ...

  7. ssh连接Ubuntu之access denied

    解决方法是: 修改/etc/ssh/ssh_config文件, #PermitRootLogin prohibit-password改为PermitRootLogin yes 然后 PasswordA ...

  8. 2019-8-31-C#-性能分析-反射-VS-配置文件-VS-预编译

    title author date CreateTime categories C# 性能分析 反射 VS 配置文件 VS 预编译 lindexi 2019-08-31 16:55:58 +0800 ...

  9. 解决mysql因内存不足导致启动报错

    报错如下所示: 解决方案: nano /etc/my.cnf 添加如下设置: key_buffer=16K table_open_cache=4 query_cache_limit=256K quer ...

  10. sql草稿

    参考:MySQL 内连接.外连接.左连接.右连接.全连接 SELECT count(*) FROM `t_product_base` select m_name from t_medicinal_in ...