<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    云自無(wú)心水自閑

    天平山上白云泉,云自無(wú)心水自閑。何必奔沖山下去,更添波浪向人間!
    posts - 288, comments - 524, trackbacks - 0, articles - 6
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

    2015年6月16日

    1. java zip 多個(gè)文件時(shí),如果先添加了一個(gè)excel文件,然后再想添加其他的文件時(shí)會(huì)出現(xiàn) steam is closed的錯(cuò)誤。這是因?yàn)閣ork.write(outputSteam)后,出調(diào)用outputSteam.close(),關(guān)閉輸出流。
    解決方法:
    將原來(lái)的程序:
                ZipEntry entry = new ZipEntry( "file3.txt" );
                zos.putNextEntry( entry );
                workbook.write( zos );
                zos.closeEntry();
    改為:
                ZipEntry entry = new ZipEntry( "file3.txt" );
                zos.putNextEntry( entry );
                workbook.write( new NonCloseableOutputStream( zos ) );
                zos.closeEntry();

    其中 NonCloseableOutputStream 定義如下:
    public class NonCloseableOutputStream extends java.io.FilterOutputStream {
        public NonCloseableOutputStream(OutputStream out) {
            super(out);
        }
        @Override public void close() throws IOException {
            flush();
        }
    }



    2. 使用binary使得mysql區(qū)分大小寫
    select * from table1 where binary field1 = 'abc';

    posted @ 2017-08-09 19:52 云自無(wú)心水自閑 閱讀(428) | 評(píng)論 (0)編輯 收藏

    https://notepad-plus-plus.org/community/topic/13661/plugin-manager-x64-available-submit-your-plugins

    posted @ 2017-06-26 09:33 云自無(wú)心水自閑 閱讀(396) | 評(píng)論 (0)編輯 收藏

    move Git Server to a new IP/URL:

    you can just edit 
    .git/config and change the URLs there

    也可以在git視圖中,右鍵點(diǎn)擊項(xiàng)目,選擇屬性,然后修改url中的地址

    posted @ 2017-06-15 08:40 云自無(wú)心水自閑 閱讀(320) | 評(píng)論 (0)編輯 收藏

    autohotkey
    listary
    cmder可以split screen,在一個(gè)窗口中同時(shí)運(yùn)行數(shù)個(gè)cmd

    posted @ 2017-05-24 07:13 云自無(wú)心水自閑 閱讀(17853) | 評(píng)論 (0)編輯 收藏

    官網(wǎng)地址:autohotkey.com

    ; fill password
    ^Numpad2::
    Send, root{tab}root{enter}
    Return
    ^Numpad3::
    IfWinExist, ahk_exe OUTLOOK.EXE
    {
        WinActivate ahk_exe OUTLOOK.EXE ; Automatically uses the window found above.
        ; WinMaximize  ; same
        ;Send, Some text.{Enter}
    msgbox Outlook is running.
    }
    Return

    posted @ 2017-03-08 13:06 云自無(wú)心水自閑 閱讀(368) | 評(píng)論 (0)編輯 收藏

    <html>
    <head>
        <script src="https://unpkg.com/vue/dist/vue.js"></script>
        <script>
            window.onload = function () {
                var app = new Vue({
                    el: '#app',
                    data: {
                        message: 'Hello Vue!'
                    }
                });
            }    
        </script>
    </head>

    <body>
        <div id="app">
          {{ message }}
        </div>
    </body>
    </html>

    posted @ 2017-02-09 07:41 云自無(wú)心水自閑 閱讀(402) | 評(píng)論 (0)編輯 收藏


    String[] splits=someString.split("a,b,c,d", ",");
    logger.debug( "array: {}", (Object) splits );

    這里要注意的就是要把數(shù)組的數(shù)據(jù)類型強(qiáng)制轉(zhuǎn)換為Object 

    posted @ 2016-12-29 11:51 云自無(wú)心水自閑 閱讀(1619) | 評(píng)論 (0)編輯 收藏

    在windows環(huán)境中,可以用如下方法重置root密碼

    1、先停止mysql數(shù)據(jù)庫(kù)

    2、保存密碼重置sql文件
         5.7.6(包括)以后的版本:ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
         5.7.5(包括)以前的版本:SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
    假設(shè)保存到文件: c:\reset.txt

    3、以管理員身份打開(kāi)命令行窗口,運(yùn)行
    C:\> cd "C:\Program Files\MySQL\MySQL Server 5.5\bin"
    C:\> mysqld --init-file=C:\reset.txt

    4、啟動(dòng)后,還不能馬上用新密碼連接數(shù)據(jù)庫(kù),需要重啟mysql數(shù)據(jù)庫(kù)

    posted @ 2016-12-21 07:12 云自無(wú)心水自閑 閱讀(370) | 評(píng)論 (0)編輯 收藏

    This is a general step that happens when m2e/m2eclipse (Maven integration for Eclipse) is installed, whether projects are actively using it or not.
    這是因?yàn)閙2eclipse(maven插件)要在啟動(dòng)時(shí)需要進(jìn)行的一個(gè)步驟。

    This step can be disabled through the Eclipse preferences: Window / Preferences / Maven / "Download repository index updates on startup". This option is on the main "Maven" preference page (not a child page). Just uncheck the box to prevent this from happening.
    我們可以停止這個(gè)動(dòng)作。方法:Windows -> Preferences -> Maven 取消勾選 Download repository index updates on startup

    posted @ 2016-11-29 08:38 云自無(wú)心水自閑 閱讀(1310) | 評(píng)論 (0)編輯 收藏

    有好幾個(gè)java library都可以實(shí)現(xiàn)這個(gè)功能,但是從pdf提取文本的一個(gè)問(wèn)題是,提取出來(lái)的文本沒(méi)有固定的順序,不容易比較好的還原其格式。

    我的做法是使用pdfclown來(lái)進(jìn)行這項(xiàng)工作。官方網(wǎng)站是:https://pdfclown.org/ 先下載其最新版本。
    參考其示例代碼:https://pdfclown.org/2010/01/02/upcoming-0-0-8-whats-going-to-be-new/#more-30

    使用這段代碼,我們不僅可以得到文本的字符串,還能得到文本的頁(yè)數(shù)和相對(duì)坐標(biāo)。
    我的思路是先把所有文本的字符串和坐標(biāo)提取出來(lái)。然后排序,排序的順序是縱坐標(biāo),然后橫坐標(biāo)。
    這樣排序完畢后,就能比較好的解決文本格式問(wèn)題。

    posted @ 2016-11-28 11:03 云自無(wú)心水自閑 閱讀(385) | 評(píng)論 (0)編輯 收藏


    1, 先定義一個(gè)input, 做為datepicker的容器。
    <input type='text' class="form-control" id="dateTo" name="dateTo" required/>

    2, 在后面加上glyphicon, 注意關(guān)鍵是label 中的for的id需要是前面定義的容器的id, 這樣點(diǎn)擊glyphicon的時(shí)候就會(huì)觸發(fā)彈出日期選擇框。
    <label for="dateTo" class="input-group-addon"><span class="glyphicon glyphicon-time"></span></label>

    posted @ 2016-10-10 19:57 云自無(wú)心水自閑 閱讀(219) | 評(píng)論 (0)編輯 收藏

    在日志文件中看到這個(gè)錯(cuò)誤信息
    Cause: java.sql.SQLException: #HY000

    后來(lái)才知道這是因?yàn)閿?shù)據(jù)庫(kù)中有個(gè)別字段要求不能為空, 但是insert語(yǔ)句中沒(méi)有提供數(shù)據(jù),造成了這個(gè)錯(cuò)誤。

    關(guān)鍵是錯(cuò)誤信息不明確直觀,不容易知道是這個(gè)原因


    posted @ 2016-09-28 13:13 云自無(wú)心水自閑 閱讀(1067) | 評(píng)論 (0)編輯 收藏

        public void afterJFinalStart(){
            Configuration config = FreeMarkerRender.getConfiguration();
            config.setTemplateUpdateDelayMilliseconds( 2 );
            config.setAPIBuiltinEnabled( true );
        }

    posted @ 2016-09-21 14:02 云自無(wú)心水自閑 閱讀(239) | 評(píng)論 (0)編輯 收藏


    中文版地址  https://angular.cn/

    posted @ 2016-09-16 13:13 云自無(wú)心水自閑 閱讀(2108) | 評(píng)論 (0)編輯 收藏

    1, call ##002# to cancel "call diversion"

    2, call 121600, choose option "2" to cancel "Active call catcher"

    posted @ 2016-08-25 12:58 云自無(wú)心水自閑 閱讀(154) | 評(píng)論 (0)編輯 收藏

    1. 格式化XML的插件
    可以安裝“XML Tools", 安裝完畢后,選擇 插件->XML Tools->Pretty Print(XML Only - with line breaks)

    2. 格式化JSON的插件
    可以安裝”JSON Viewer", 安裝完畢后,選擇 插件->JSON Viewer->Format JSON

    3. 格式化SQL的插件
    可以安裝“Poor man's T-Sql Formatter", 選擇 插件->Poor man's T-Sql Formatter->Format T-Sql Code

    posted @ 2016-08-12 15:14 云自無(wú)心水自閑 閱讀(1052) | 評(píng)論 (0)編輯 收藏

     
    使用的工具

    1. Apache HttpClient
    2. Firefox + FireBug
    3. Burp Suite ( https://portswigger.net/burp ) + Firefox FoxyProxy

    Firefox + FireBug 主要用于查看渲染出的頁(yè)面中的信息(比如:表單項(xiàng)的名稱,節(jié)點(diǎn)ID等等)
    Burp Suite 主要用于動(dòng)態(tài)攔截頁(yè)面的交互,查看Ajax的調(diào)用。
    HttpClient 用于最后程序的編制。搞清楚了網(wǎng)頁(yè)交互的過(guò)程,就可以自主決定程序需要包含的內(nèi)容。
    在實(shí)際網(wǎng)頁(yè)中,可能需要點(diǎn)開(kāi)數(shù)級(jí)菜單,才能最后看到需要的內(nèi)容。
    但是在程序中,可以直接跳到最后一步。

    posted @ 2016-06-05 19:00 云自無(wú)心水自閑 閱讀(198) | 評(píng)論 (0)編輯 收藏

    1. 表格文字右對(duì)齊 
     <table>
    <tr>
        <td><p style="text-align:right;margin:0;padding:0">文字右對(duì)齊</p></td>
        <td>文字左對(duì)齊</td>
    </tr>
    </table>

    2. 表格邊緣的margin 需要在表格外再套一個(gè)div
    <div style="margin:10px">
        <table>
        ......
        </table>
    </div>

    3. btn-toolbar class can put a margin between 2 "pull-right" buttons
            <div class="row">
                <div class="col-md-2"></div>
                <div class="col-md-8 btn-toolbar">
                    <input type="submit" class="btn btn-warning pull-right" value="Submit">
                    <input type="button" id="profilePassBackBtn" class="btn btn-info pull-right" value="Back">
                </div>
                <div class="col-md-2">
                </div>
            </div>

    posted @ 2016-05-31 11:39 云自無(wú)心水自閑 閱讀(378) | 評(píng)論 (0)編輯 收藏

     AngularJS 2.0 已經(jīng)發(fā)布了Beta版本,相信正式版不久以后就會(huì)發(fā)布了。

    下面是官網(wǎng)上的新功能介紹:

    1. 更快更高效。AngularJS 2 將會(huì)比 AnuglarJS 1 快很多。因?yàn)樗鼤?huì)支持:從遠(yuǎn)程胳快速加載、離線編譯以便于更快啟動(dòng)、以及超快的變動(dòng)檢測(cè)和為使?jié)L動(dòng)更平滑的視圖緩存等等。

    2. 更加簡(jiǎn)單清晰。語(yǔ)法將會(huì)顯得更加自然,易于編寫

    3. 跨越平臺(tái)。無(wú)論是臺(tái)式機(jī)、手機(jī)瀏覽器、安卓、IOS平臺(tái),AngularJS都能提供相應(yīng)的支持。

    4. 無(wú)縫從 AngularJS 1 升級(jí)到 2

    5. 簡(jiǎn)便的開(kāi)發(fā)。支持各種開(kāi)發(fā)語(yǔ)言,ES5, TypeScript, Dart

    6. 全面完備的路由。 方便地映射URL到應(yīng)用組件,并提供多種高級(jí)功能,比如:嵌套和鄰接路由,支持卡片棧導(dǎo)航、動(dòng)畫過(guò)渡、手機(jī)用戶延遲加載等等

    7. 依賴注入。

    8. 舊瀏覽器的良好支持

    9. 動(dòng)畫效果 (仍在開(kāi)發(fā)中)

    10. 國(guó)際化支持(仍在開(kāi)發(fā)中)

    posted @ 2016-04-18 20:09 云自無(wú)心水自閑 閱讀(265) | 評(píng)論 (0)編輯 收藏

    1. Go to web project properties.
    2. Deployment Assembly (Left).
    3. Add > Select project > Select your lib project > Check "Assemble projects into the WEB-INF/lib folder of the web application" if not checked > Finish.

    posted @ 2016-04-13 10:35 云自無(wú)心水自閑 閱讀(171) | 評(píng)論 (0)編輯 收藏

     使用酷狗就可以轉(zhuǎn)換。
    右鍵點(diǎn)擊歌曲 ,工具,格式轉(zhuǎn)換。
    唯一要注意的是要先登錄。

    posted @ 2016-03-17 20:20 云自無(wú)心水自閑 閱讀(1595) | 評(píng)論 (0)編輯 收藏

    今天把commons dbcp 和 pool都升級(jí)到2.x, 結(jié)果發(fā)現(xiàn)不能正常的工作,卡在new BasicDataSource()上了.
    后來(lái)才發(fā)現(xiàn)原因是因?yàn)闆](méi)有加入commons-logging的jar文件

    幾個(gè)注意點(diǎn):
    1. commons dbcp2.x 和 commons pool需要同時(shí)升到2.x
    2. dbcp 2.x要運(yùn)行在java 7以上 
    3. mysql connector要5.1.11以上
    4. 需要有commons-logging的包,我使用的是slf4j, 就需要加一個(gè)jcl-over-slf4j

    posted @ 2016-02-09 11:44 云自無(wú)心水自閑 閱讀(615) | 評(píng)論 (0)編輯 收藏

    Error
    com.jcraft.jsch.JSchException: The cipher 'aes256-cbc' is required, but it is not available.
    or
    Caused by: java.security.InvalidKeyException: Illegal key size


    posted @ 2016-02-05 13:51 云自無(wú)心水自閑 閱讀(270) | 評(píng)論 (0)編輯 收藏


    我在網(wǎng)上搜索了一下如何使用Selenium下載文件,其中確實(shí)有幾篇文件介紹了實(shí)現(xiàn)的方法。
    但是其主要思想都是使用httpClient或者URL獲得InputStream, 然后保存到文件中。
    但是,其中的問(wèn)題是用戶登錄的Session不能維持。

    我發(fā)現(xiàn)了一個(gè)簡(jiǎn)單的方法。
    直接使用WebDriver.get, 示例如下:

    webDriver.get("https://website.com/login");
    WebElement element = driver.findElement( By.id( "userID" ) );
    element.sendKeys( "user01" );

    element = driver.findElement( By.id( "passwd" ) );
    element.sendKeys( "password" );

    element = driver.findElement( By.name( "Login" ) );
    element.submit();

    webDriver.get("https://website.cm/download.do?start=xx&end=yy");
    String source = webDriver.getPageSource();

    這個(gè)source就是我們想保存的要下載的內(nèi)容。
    只要把這個(gè)String寫到一個(gè)文件中,就實(shí)現(xiàn)了文件下載的目的

    posted @ 2016-01-28 18:06 云自無(wú)心水自閑 閱讀(470) | 評(píng)論 (0)編輯 收藏

         摘要: 在我的上一篇文章中介紹了如何進(jìn)行GPG加密解密。
    加密解密的基本操作流程是,用戶使用公鑰對(duì)明文進(jìn)行加密,解密方使用私鑰對(duì)密文進(jìn)行解密。

    在實(shí)際應(yīng)用中,除了加密保證文本內(nèi)容不泄露外,同時(shí)還要考慮能夠驗(yàn)證密文發(fā)送方的身份,比較普遍使用的方法就是簽名。
    本文主要對(duì)具體的方法進(jìn)行介紹并附上源代碼。  閱讀全文

    posted @ 2015-12-11 21:40 云自無(wú)心水自閑 閱讀(1268) | 評(píng)論 (0)編輯 收藏

    Java程序中訪問(wèn)擁有全部讀寫權(quán)限的目錄相對(duì)比較簡(jiǎn)單,和普通的目錄沒(méi)有什么差別。
    但是要訪問(wèn)一個(gè)需要用戶和密碼驗(yàn)證的目錄就需要一點(diǎn)點(diǎn)小技巧了。
    這里介紹一個(gè)開(kāi)源的庫(kù)能夠比較容易的實(shí)現(xiàn)這一需求。
    1。 下載庫(kù)文件:
     https://jcifs.samba.org/
    下載的zip文件中, 不僅包含了jar文件,還有文檔和示例。

    2。拷貝jcif-1.3.18.jar到類路徑中。

    3。代碼示例:
     1     String user = "your_user_name";
     2     String pass ="your_pass_word";
     3 
     4     String sharedFolder="shared";
     5     String path="smb://ip_address/"+sharedFolder+"/test.txt";
     6     NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
     7     SmbFile smbFile = new SmbFile(path,auth);
     8     SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
     9     smbfos.write("testing.and writing to a file".getBytes());
    10     System.out.println("completed nice !");
    說(shuō)明: 如果有一個(gè)共享目錄,比如: \\192.168.1.2\testdir\
    那么smb的路徑就是:smb://192.168.1.2/testdir/
    NtlmPasswordAuthentication需要三個(gè)參數(shù), 第一個(gè)是名,沒(méi)有的話,填null, 第二個(gè)是用戶名,第三個(gè)是密碼

    得到SmbFile之后,操作就和java.io.File基本一樣了。
    另外還有一些功能比如:
    SmbFile.copyTo
    SmbFile.renameTo
    等等

    posted @ 2015-11-20 14:03 云自無(wú)心水自閑 閱讀(12984) | 評(píng)論 (0)編輯 收藏

    先將my.default.ini改名為my.ini放到bin目錄
    命令行執(zhí)行: mysqld --initialize --user=mysql --console
    先執(zhí)行以上命令, 生成庫(kù). 注意有個(gè)臨時(shí)密碼, 要記下來(lái).

    安裝服務(wù):mysqld.exe --install MySql5.7 --defaults-file=c:\mysql\mysql5.7\my.ini

    然后啟動(dòng)服務(wù). 
    然后再命令行:
    mysql -uroot -p
    輸入密碼,
    再輸入: 
    set password = password('root')
    改密碼成功, 然后就可以操作了.

    posted @ 2015-11-09 15:25 云自無(wú)心水自閑 閱讀(736) | 評(píng)論 (0)編輯 收藏

    如果只是在beforeSubmit()中 調(diào)用$('#fieldname').val(2)是不能成功修改表單的值的。
    因?yàn)榇藭r(shí)ajaxForm已經(jīng)把表單中所有的內(nèi)容存儲(chǔ)在arr之中了。

        $('#form1').ajaxForm({
            beforeSubmit: function(arr){
                for ( var i = 0; i < arr.length; i ++ ) {
                    if ( arr[i].name == "fieldName1" ) {
                        arr[i].value = '新的值';
                    }
                }
            }
        });
    需要使用這種方式進(jìn)行修改。

    posted @ 2015-11-02 19:13 云自無(wú)心水自閑 閱讀(1218) | 評(píng)論 (0)編輯 收藏

    今天在運(yùn)行myeclipse的時(shí)候,突然報(bào)nullPointerException.

    具體的錯(cuò)誤信息如下:

    Message: Errors running builder ‘DeploymentBuilder’ on project XXX’.
    Exception Stack Trace
    java.lang.NullPointerException

    解決方法:

    1. Shut down the workspace.

    2. Delete the file com.genuitec.eclipse.ast.deploy.core.prefs which is located at <workspace dir>/.metadata/.plugins/org.eclipse.core.runtime/.settings/com.genuitec.eclipse.ast.deploy.core.prefs

    3. Start the IDE.

    posted @ 2015-10-21 09:21 云自無(wú)心水自閑 閱讀(364) | 評(píng)論 (0)編輯 收藏

     
    ipconfig /flushdns
    ipconfig /registerdns
    netsh winsock reset

    重新啟動(dòng)電腦。

    posted @ 2015-10-13 16:31 云自無(wú)心水自閑 閱讀(1787) | 評(píng)論 (0)編輯 收藏

    今天下載了Apache James 3.0 Beta 5, 文件名:james-server-app-3.0.0-beta5-20150627.102412-1076-app.zip
    解壓,運(yùn)行run.bat

    然后,注冊(cè)domain
    james-cli --host localhost adddomain example.com
    添加用戶
    james-cli.bat --host localhost adduser test@example.com password

    然后測(cè)試發(fā)送郵件,客戶端顯示發(fā)送成功,但是james服務(wù)器報(bào)錯(cuò),找不到MimeConfig的無(wú)參數(shù)構(gòu)造函數(shù)。
    解決方法:
    使用舊的mime4j的jar包替換james 3.0 beta5中自帶的最新包。
    beta5中自帶的是0.8.0版,apache網(wǎng)站中可以下載到0.7.2
    下載apache-mime4j-0.7.2-bin.zip, 將其中的apache-mime4j-core-0.7.2.jar, apache-mime4j-dom-0.7.2.jar復(fù)制到j(luò)ames\lib目錄,
    并將其更名覆蓋原有的
    apache-mime4j-core-0.8.0-20150617.024907-738.jar
    apache-mime4j-dom-0.8.0-20150617.024927-735.jar
    重新啟動(dòng)james, 發(fā)送郵件, 成功。

    posted @ 2015-10-08 08:45 云自無(wú)心水自閑 閱讀(3277) | 評(píng)論 (0)編輯 收藏

         摘要: 解壓/生成有密碼保護(hù)的壓縮文件, 研發(fā)過(guò)程中,作者研究了壓縮文件格式文檔: http://www.pkware.com/documents/casestudies/APPNOTE.TXT,并且參考了7-zip的實(shí)現(xiàn)。
      閱讀全文

    posted @ 2015-08-19 10:16 云自無(wú)心水自閑 閱讀(9954) | 評(píng)論 (0)編輯 收藏

         摘要: 花了兩天時(shí)間終于把windows10安裝好了,以下是我的一些個(gè)人的體會(huì)
      閱讀全文

    posted @ 2015-08-03 18:56 云自無(wú)心水自閑 閱讀(6251) | 評(píng)論 (0)編輯 收藏

    在JfinalConfig的繼承類中,
    configConstant() 需要設(shè)置me.setDevMode(true);

    1. 只有在DevMode下,才能禁止freeMarker的緩存。
    Configuration config = FreeMarkerRender.getConfiguration();
    config.setTemplateUpdateDelayMilliseconds(0);
    才會(huì)生效


    2. 這時(shí)才會(huì)有JFinal Action Report日志輸出

    posted @ 2015-07-24 19:58 云自無(wú)心水自閑 閱讀(416) | 評(píng)論 (0)編輯 收藏

    本文將簡(jiǎn)單介紹如何使用PowerMock和Mockito來(lái)mock
    1. 構(gòu)造函數(shù)
    2. 靜態(tài)函數(shù)
    3. 枚舉實(shí)現(xiàn)的單例
    4. 選擇參數(shù)值做為函數(shù)的返回值
    5. 在調(diào)用mock出來(lái)的方法中,改變方法參數(shù)的值

    一點(diǎn)簡(jiǎn)要說(shuō)明:Mockito其實(shí)已經(jīng)可以滿足大部分的需求,但是它的實(shí)現(xiàn)機(jī)制是使用cglib來(lái)動(dòng)態(tài)創(chuàng)建接口的類的實(shí)例。但是這種實(shí)現(xiàn)方式不能用于構(gòu)造函數(shù)和靜態(tài)函數(shù),因?yàn)槟切枰褂妙惖淖止?jié)碼(比如使用javassist). 所以我們才需要結(jié)合使用PowerMock.

    1. mock構(gòu)造函數(shù), 如果有代碼沒(méi)有使用DI注入依賴實(shí)例,在單元測(cè)試中可以使用PowerMock來(lái)模擬創(chuàng)建對(duì)象。
    注意的開(kāi)始兩行的2個(gè)注解 @RunWith 和 @PrepareForTest
    @RunWith比較簡(jiǎn)單,后面始終是PowerMockRunner.class
    @PrepareForText后面需要加的是調(diào)用構(gòu)造函數(shù)的類名,而不是有構(gòu)造函數(shù)的類本身。
    在下面的例子中,我們要測(cè)試的類是:Helper, 在Helper類中調(diào)用了Somthing類的構(gòu)造函數(shù)來(lái)創(chuàng)建實(shí)例。
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(Helper.
    class)
    public class HelperTest {
      @Mock
      
    private Something mockSomething;
          
      @InjectMocks
      
    private Helper helper;
          
      @Test
      
    public void doSomething() throws Exception {
          String argument 
    = "arg";
              
          PowerMockito.whenNew(Something.
    class).withArguments(argument).thenReturn(mockSomething);
             
          // 調(diào)用需要測(cè)試方法
          helper.doSomething(argument);
             
          // 進(jìn)行驗(yàn)證
          verify(mockSomething).doIt();
      }
    }


    public class Helper {
      public void doSomething(String arg) {
          Something something = new Something(arg);
          something.doit();
      }
    }


    2,mock 靜態(tài)函數(shù), 單例模式就是一個(gè)典型的會(huì)調(diào)用靜態(tài)函數(shù)的例子。 注意要點(diǎn)與mock構(gòu)造函數(shù)相同。
    class ClassWithStatics {
      
    public static String getString() {
        
    return "String";
      }

      
    public static int getInt() {
        
    return 1;
      }
    }

    @RunWith(PowerMockRunner.
    class)
    @PrepareForTest(ClassWithStatics.
    class)
    public class StubJustOneStatic {
      @Test
      
    public void test() {
        PowerMockito.mockStatic(ClassWithStatics.
    class);

        when(ClassWithStatics.getString()).thenReturn(
    "Hello!");

        System.out.println(
    "String: " + ClassWithStatics.getString());
        System.out.println(
    "Int: " + ClassWithStatics.getInt());
      }
    }

    3。mock枚舉實(shí)現(xiàn)的單例
    SingletonObject.java
    public enum SingletonObject { 
        INSTANCE
    ;
        private
    int num;
        protected
    void setNum(int num) {
            this.num = num;
        }
        public int getNum() {
            return
    num;
        }

    }
    SingletonConsumer.java

    public class SingletonConsumer {
        public String consumeSingletonObject() { 
            return
    String.valueOf(SingletonObject.INSTANCE.getNum());
        }
    }
    SingletonConsumerTest.java
    @RunWith(PowerMockRunner.class) 
    @PrepareForTest({SingletonObject.class})
    public class SingletonConsumerTest {
        @Test public void testConsumeSingletonObject() throws Exception {
            SingletonObject
    mockInstance = mock(SingletonObject.class);
            Whitebox
    .setInternalState(SingletonObject.class, "INSTANCE", mockInstance);
            when
    (mockInstance.getNum()).thenReturn(42);
            assertEquals
    ("42", new SingletonConsumer().consumeSingletonObject());
        }
    }
    4。返回參數(shù)值做為函數(shù)返回值。
    mockito 1.9.5之后,提供一個(gè)方便的方法來(lái)實(shí)現(xiàn)這個(gè)需要,在這之前可以使用一個(gè)匿名函數(shù)來(lái)返回一個(gè)answer來(lái)實(shí)現(xiàn)。
    when(myMock.myFunction(anyString())).then(returnsFirstArg());
    其中returnsFirstArg()是org.mockito.AdditionalAnswers中的一個(gè)靜態(tài)方法。
    在這個(gè)類中還有其他的一些類似方法
    returnsSecondArg()
    returnsLastArg()
    ReturnsArgumentAt(int position)

    5. 在調(diào)用mock出來(lái)的方法中,改變方法參數(shù)的值
    when( myMock.someMethod( any( List.class ) ) ).thenAnswer( ( new Answer<Void>() {
        @Override
        
    public Void answer( InvocationOnMock invocation )
                
    throws Throwable {
            Object[] args 
    = invocation.getArguments();
            List arg1 
    = (List)args[0];
            arg1.add(
    "12345");
            
    return null;
        }
    } ) );



    Verifying with generic parameters
    verify(someService).process(Matchers.<Collection<Person>>any());
    verify(adunoMasterBaseProcessor).processBinFiles( anyListOf(File.class) );

    posted @ 2015-06-16 21:27 云自無(wú)心水自閑 閱讀(18458) | 評(píng)論 (0)編輯 收藏

    主站蜘蛛池模板: 成人免费看片又大又黄| 免费大黄网站在线看| 亚洲无线码一区二区三区| 亚洲狠狠婷婷综合久久| 一本岛高清v不卡免费一三区| 亚洲国产a∨无码中文777| 免费看一级一级人妻片| 日韩一区二区在线免费观看 | 亚欧日韩毛片在线看免费网站| 亚洲综合精品香蕉久久网| www永久免费视频| 天堂亚洲免费视频| 亚洲AV无码XXX麻豆艾秋| 在线成人a毛片免费播放| 亚洲综合无码一区二区痴汉| 色天使亚洲综合一区二区| 四色在线精品免费观看| 亚洲中文无码卡通动漫野外| 美女视频黄的全免费视频| 亚洲成年人电影网站| 四虎精品视频在线永久免费观看| 18gay台湾男同亚洲男同| 2022久久国产精品免费热麻豆| 亚洲黄色免费观看| h视频在线观看免费完整版| 亚洲国产美女精品久久| 成人免费在线看片| 亚洲一区二区三区高清视频| 一本无码人妻在中文字幕免费| 亚洲午夜在线播放| 在线观看人成网站深夜免费| 亚洲经典千人经典日产| 又粗又硬免费毛片| www永久免费视频| 久久亚洲中文无码咪咪爱| 国产在线观看免费视频软件| 国产亚洲A∨片在线观看| 免费观看在线禁片| 亚洲色偷偷av男人的天堂| 4虎永免费最新永久免费地址| 亚洲一区二区三区亚瑟|