测试用例不一致,有时会通过失败

时间:2020-10-23 04:18:26

标签: junit mockito powermock easymock

我有这个测试用例

  public void testCacheSyncSingleColumn() throws Exception {
    resetAll();
    String schema = "barcelona_mm";
    String mainTable = "notes";
    String mainCol = "assignee_id";
    String mainColValue = "1";
    String mapQuery = "SELECT firstname AS value FROM #.security_user WHERE security_user_id = ";
    String queryResult = "Support Operations";
    String key = mainTable + "." + mainCol;
    String[] lineA = new String[] { mainTable, mainCol, "security_user", "security_user_id", "firstname" };
    String[] lineB = new String[] { "" };

    FileReader fileMock = null;// EasyMock.createMock(FileReader.class);
    CSVReader mockReader = EasyMock.createMock(CSVReader.class);
    connectionFactoryMock = EasyMock.createMock(ConnectionFactory.class);
    cacheSignatureService = new CachedSignatureServiceImpl();
    SignatureDao signatureDaoMock = EasyMock.createMock(SignatureDao.class);

    SignatureReader signatureReaderMock = EasyMock.createMock(SignatureReader.class);

    setInternalState(cacheSignatureService, SignatureReader.class, signatureReaderMock);
    setInternalState(cacheSignatureService, SignatureDao.class, signatureDaoMock);
    PowerMockito.whenNew(CSVReader.class).withArguments(fileMock).thenReturn(mockReader);

    // ERROR AT THIS LINE
    expect(mockReader.readNext()).andReturn(lineA);
    expect(mockReader.readNext()).andReturn(lineB);
    mockReader.close();
    replayAll();
    JdbcTemplate template = createMock(JdbcTemplate.class);
    expect(template.query(EasyMock.<String> anyObject(), EasyMock.<PreparedStatementSetter> anyObject(),
        EasyMock.<ResultSetExtractor<String>> anyObject())).andReturn(queryResult).anyTimes();

    expect(signatureReaderMock.getQuery(EasyMock.anyString(), EasyMock.anyString())).andReturn(mapQuery);
    expect(signatureReaderMock.getRefKey(EasyMock.anyString(), EasyMock.anyString())).andReturn(key);
    expect(signatureReaderMock.getRefKey(EasyMock.anyString(), EasyMock.anyString())).andReturn(key);
    expect(signatureDaoMock.getValue(EasyMock.anyString(), EasyMock.anyString(), EasyMock.anyString(),
        EasyMock.anyString(), EasyMock.anyString())).andReturn(queryResult);

    replayAll(signatureReaderMock, signatureDaoMock);
    String result = cacheSignatureService.getValue("source", schema, mainTable, mainCol, mainColValue);
    Assert.assertTrue(result.equals(queryResult));

  }

如果我运行10次,那么在所有10次中都会经过一段时间,并抛出错误

java.lang.IllegalStateException: 0 matchers expected, 1 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)

0 个答案:

没有答案