2013-08-24 42 views
1

我得到這個錯誤 - 但正則表達式應該恕我直言匹配字符串 - 很想知道什麼是這裏的問題奇怪AssertError使用巨星時,正則表達式斷言

java.lang.AssertionError: <'0 I: bar 
1 I: foo 
'> should match the regular expression:<'.*bar.*'> 
    at org.fest.assertions.Fail.failure(Fail.java:228) 
    at org.fest.assertions.Assert.failure(Assert.java:149) 
    at org.fest.assertions.StringAssert.matches(StringAssert.java:129) 
    at org.ligi.tracedroid.test.TheLog.should_rotate_proper(TheLog.java:19) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:241) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:80) 
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47) 
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69) 
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) 
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) 
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32) 
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93) 
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source) 
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35) 
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24) 
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355) 
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:724) 

這是測試引起的問題(煮下):

package org.ligi.tracedroid.test; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.ligi.tracedroid.logging.Log; 

import static org.fest.assertions.Assertions.assertThat; 
@RunWith(Runner.class) 
public class TheLog { 

    @Test 
    public void should_rotate_proper() { 
     Log.setLogCacheSize(2); 
     Log.i("forget me"); 
     Log.i("foo"); 
     Log.i("bar"); 

     // TODO investigate why this is not working 
     assertThat(Log.getCachedLog()).matches(".*bar.*"); 
    } 

} 
+0

,你能否告訴我們,造成異常的代碼,你試圖匹配的字符串,請提供更多信息? –

+0

剛剛添加了測試 – ligi

回答

3

.,除非你指定的格局內Pattern.DOTALL標誌或(?s)不匹配換行符。

變化如下:

assertThat(Log.getCachedLog()).matches("(?s).*bar.*"); 

但如果你想只檢查bar被包含在日誌中,contains似乎更爲合適:

assertThat(Log.getCachedLog()).contains("bar"); 
+0

謝謝 - 這是缺失的一塊 - 正則表達式更復雜 - 剛剛煮沸這個問題 – ligi