跳到主要內容

發表文章

目前顯示的是有「JUnit」標籤的文章

How to verify the Log4j output?

Problem 當功能的測試案例寫完後,就是要開始針對較特殊的案例撰寫單元測試。log錯誤是一種錯誤回報的例外處理機制,而單元測試中,我們該如何捕捉到這些透過log回報的錯誤呢? Log4j可以透過ConsoleAppender將訊息給顯示到console上, 先前文章 也分享過驗證console內容的方法;然而我們不希望使用者看到這些內容,所以預設的log4j設定檔是不使用ConsoleAppender。 如果針對測試提供不同的log4j設定檔,也不是那麼的方便,因此我分享給大家另外一種方法,可以將Log4j訊息導到StringBuffer中以做驗證。 How to? 主要技巧是透過LogManager在測試之前加入WriterAppender,而WriterAppender會使用我給予的OutputStreamWriter: private StringBuffer mSB ; private StringBufferOutputStream mSBOutputStream ; private WriterAppender mTestAppender ;   @Before public void setUp ( ) throws Exception { mSB = new StringBuffer ( ) ; mSBOutputStream = new StringBufferOutputStream ( mSB ) ;   Writer w = new OutputStreamWriter ( mSBOutputStream ) ; mTestAppender = new WriterAppender ( ) ; mTestAppender. setLayout ( new PatternLayout ( PatternLayout. DEFAULT_CONVERSION_PATTERN ) ) ; mTestAppender. setName ( "test" ) ; mTestAppender. activateOptions ( ) ; mTestAppender. setWriter ( w ) ;   LogManager. getLog...

JUnit文章列表

PowerMock Mock System.exit Mock Constructor 當Powermock遇到Spring Static Block Resolve exceptions 不同的Expected Result 無法預期的輸入 Mock Class Field UnsupportedOperationException: Unsupported copy option Mockito / PowerMockito Mock partial static method with PowerMockito Frequently asked questions 常見問題 Verify the calling order Get Mock Class Type Answer Contains Multiple Value Code Coverage With PowerMock on static method Mockito with annotations JUnit How to verify the System.out.print? Conditionally ignore tests How to verify the Log4j output? JUnit4 : TemporaryFolder Rule

How to vertify the System.out.print?

Problem 假如我們有一隻main程式,它在執行完某些工作後會dump資訊到console上,我們要怎麼做verify? 假設程式如下,我們的測試程式所期望的輸出就是HelloWorld。 public class Example { public static void main ( String [ ] args ) { System . out . println ( "Hello World" ) ; } } How to verify? 是否能夠透過PowerMock達到目的呢? 答案是可以的,但非常麻煩。你必須先mock system.out物件,接著針對可能會呼叫到的println或print做去set expectResult。我提供另外一種方法,可以讓你直接針對結果做assertion,步驟如下: 建立mock的PrintStream類別且override write method,System.out的print method最後都會呼叫write method。 建立mock的OutputStream類別,用來設置給PrintStream使用。而此mock OutputStream物件需要override write method,將寫入的int轉放入StrinBuffer中,好讓最後我們可以直接透過此StringBuffer做驗證。 在測試案例中,將mock的PrintStream物件設至System.out中。 驗證mock PrintStream所儲存起來的資訊。 我們來看看實做。 Implement Mock PrintStream 在mock PrintStream建構子的地方,super讓它使用原本的System.out就好,這並不是很重要。重要的是我們要將client用來驗證的OutputStream給設成member,而write的地方要將傳入值給帶給OutputStream物件中,好讓它最後可以透過我們假的OutputStream做寫入動作。 import java.io.IOException ; import java.io.OutputStream ; import java.io.PrintStream ;   public class The...

Launch WireMock with mappings file of JUnit

Problem 在解決整合REST外部服務的腳本錄製問題後,接下來就是應用的問題。我想要解決的問題是「該如何在JUnit中啟用預錄好的腳本,讓它取代我所相依的外部服務,讓測試更穩更快減少假警報呢?」本篇分享我的做法。 How to? WireMockRule 這個方法是官網教的,直接使用JUnit的Rule讓它幫你啟動與關閉WireMockServer;如果要載入預錄腳本,就直接指定mappings的上一層即可。以我的範例來說,預錄腳本是放在./testdata/loadMappings/query/mappings中: public class TestWithWireMockRule { @Rule public WireMockRule wireMockRule = new WireMockRule ( WireMockConfiguration. options ( ) . port ( 80 ) . httpsPort ( 443 ) . usingFilesUnderDirectory ( "./testdata/loadMappings/query" ) ) ;   @Test public void getHttpCode200WhenQueryUser ( ) throws IOException { HttpGet post = new HttpGet ( "http://localhost/api/users/2" ) ; CloseableHttpClient httpClient = HttpClientFactory. createClient ( ) ; try { CloseableHttpResponse response = httpClient. execute ( post ) ; assertEquals ( 200 , response. getStatusLine ( ) . getStatusCode ( ) ) ; } finally { httpClient. close ( ) ; } } } WireMockServer 如果想要...