Sunday, December 18, 2016

Mockito简介

Mockito是基于JUnit,可以先学习一下JUnit。原则是依赖什么,模拟什么
Mockito可以验证
1. 值是否相等(assertEquals)
2. 方法调用几次(verify)
3. 方法调用参数是否一致(eq/ArgumentCaptor)
4. 方法调用是否按顺序

Stub

比如一个FTP程序,要测试如果断开连接是否会log错误信息Disconnected from FTP。这里断开连接是测试前提(或叫条件)是Stub,会否log错误信息是测试对象(或叫目标)。when里面只能是mock对象,不能是真实对象。when(getName(anyString(), "abc")这是不对,因为参数如果一个是matcher,其他就一定要matcher(eq, any...), i.e. eq("ewf", any())

FTPClient mockedFTP = mock(FTPClient.class);
或者
@Mock
FTPClient mockedFTP

//Stub “无法连接”
when(mockedFTP.isConnected())).thenReturn(false);
when可以任意值如when(foo.add(lt(5), eq(8)))第一参数任意小于5的值及第二参数=8时返回。。
但thenReturn一定要具体指不能用any(Test.class)

//开始测试,真实调用
test.isFTPConnected();

//检查结果
assert(true, Logger.endWith("Disconnected from FTP"));

这样的测试看似显而易见,由代码一看就知道是正确,但为了确保后续维护或者refactor仍能保持这个逻辑(需求),有必要写这样的测试。

Mockito不支持连续mock,如when(mockedFTP.getConn().isConnected).thenReturn(true); 此情况可以逐个mock来做接力
@Mock Connect conn;
when(conn.isConnected).thenReturn(true);

any用于假设,any(class)用于verify
when(mockClass.get(any())).thenReturn();
verify(dynamoDao).put(any(List.class)

API:
doReturn
doNothing
doThrow

Exception的测试:
  @Test(expected=ArithmeticException.class) public void testException2(){ doThrow(ArithmeticException.class).when(customer).getCustomerId(); // Act underTest.buildAnimal("tiger", 4); }

InOrder的测试:
声明mock的预期调用顺序,verify再验证
// Arrange
InOrder inOrder = inOrder(customer, myJsonHelper);

// Act
underTest.callInOrder();

// Assert
inOrder.verify(customer).setCustomerId(Mockito.anyInt());
inOrder.verify(myJsonHelper).toObject();

Verify

Mockito包括stub, verify, spy三种API。Verify是检查mock对象的某一个函数+参数这个组合(参数不同视为不同调用)的调用次数。使用场合:如果测试对象不返回结果或结果极为复杂就用此校验法。

//真实调用
mockedList.add("one");
//验证add("one")这个调用是否一次
verify(mockedList).add("one");

另一个例子:
request.timestamp=1483685164
device.timestamp=1483685164
//真实调用
updateTimestampActivity.update(request);
//验证add("one")这个调用是否一次
verify(dynamoDao).put(devices);
verify(dynamoDao, times(0)).put(any(Device.class));

Main.java:
update(request){
    request.timestamp=Date.now();
    Devices devices = convertToObject(request);
    dynamoDB.put(devices);
}
这个例子中verify对象时dynamoDao的put方法以及devices参数,由于devices在真实调用中被修改成当前时间与1483685164不一致,所以会报错:参数不一样。代码要更改成verify(dynamoDao).put(any(List.class));表示匹配任何List参数也就是不验证参数。
Matchers.any(Test.class)
Matchers.anyListOf(Test.class)

参数捕捉:

public void test(){
    //act
     underTest.exec();

    //assert
     verify(math).pow(baseCaptor.capture(), exponenetCaptor.capture());
     Integer base = baseCaptor.getValue();
     Float exponenet = exponenetCaptor.getValue();

     assertEqual(2, base);
   
}

@Captor
ArgumentCaptor<Integer> baseCaptor;

@Captor
ArgumentCaptor<Float> exponenetCaptor;

@Mock
Math math;

@InjectMocks
Test underTest;

捕捉参数还可以用eq来代替,如verify(math).pow(eq(2), eq(1.3));等价于verify(math).pow(2,1,3)当prmitive类型时候。如果参数为复合类型,eq会调用equals去做匹配,如eq(student),而不加eq时候会比较指针。

Spy

spy是修改部分测试对象的部分API以满足测试条件,此法慎用。
假设UnderTest有两个函数getProfile(), getName(),而getProfile()内部调用getName(), 此时getName若用when().thenReturn做mock会报错因为getName()也是UnderTest的一个函数。这是可以用spy来修改UnderTest的API来满足。@InjectMocks和@Spy可以连用。例如:

StudentTest:

public void test(){
    //因为student是spy,所以student.get(Name)可以假设
    doReturn("Gary").when(student).getName();
}

@InjectMocks
@Spy
Student student;


Mockito用于有dependency情况下测试,JUnit是无条件下测试,两者结合使用。

Annotation:

用annotation显得代码更简洁
annotation mock 功能
@RunWith(MockitoJUnitRunner.class) MockitoAnnotations.initMocks(this); 初始化
@Mock Mockito.mock(ArrayList.class) 自动产生实例,用于假设或叫依赖。如果mock类成员一定要与@InjectMocks连用,否则时null不自动产生实例
@Captor ArgumentCaptor.forClass(String.class) 参数捕捉
@InjectMocks MyDictionary dic; 自动产生实例。需要测试的obj也叫UnderTest,而它的域成员可以mock,用来测试每个函数

class Student {
     String name;
     public void exam(Dialog dialog){}
}

StudentTest:

@Mock
String name;

@InjectMocks
Student student;

@Mock
Dialog dialog;

这里dialog是独立的,即使没有InjectMocks也不影响。但name依赖于student的存在。

另一个例子:

@Test
public void test_BuildAnimal(){
// Arrange
when(customer.getCustomerId()).thenReturn(0);

// Act
Animal animal = underTest.buildAnimal("tiger", 4);

// Assert
assertEquals("0:tiger", animal.drivedName());

}

@InjectMocks
VideoSpeechlet underTest;

@Mock
Customer customer;

Mockito有一定局限性,解决方案是PowerMock。

mock contructor:
https://lkrnac.net/blog/2014/01/mock-constructor/

EqualsBuilder
排除某些fields如id
assertTrue(EqualsBuilder.reflectionEquals(student, student2, new String[]{"id"}))
assertTrue(EqualsBuilder.reflectionEquals(student, student2, "id"))

Ref:
理论:
http://blog.csdn.net/zhangxin09/article/details/42422643
API:
http://blog.csdn.net/onlyqi/article/details/6544989
实例:
http://blog.csdn.net/onlyqi/article/details/6546589
JUnit:
http://blog.csdn.net/zhangxin09/article/details/42418441



Wednesday, October 12, 2016

Hexo + GitHub Pages to build blog by Node.js

my github website: https://shineboy2013.github.io/

<code>O(_n_<sup>2</sup>)</code>
句末加两个空格表示换行
&ast;表示*
&times;表示乘号
<br>
---表示分隔线
<pre>有时不work,要改成div,重启,再改为pre即可


写新的blog,可以直接写,不用重启server(hexo s)但若要checkin时需要重新启动

安装:
这里https://xuanwo.org/2015/03/26/hexo-intor/解释很详细了,但有几点要补充:

1. download latest hexo,旧的版本安装失败
2. 要建GitHub page,github的subdomain要与Github的用户名一致。
3. 注意cd到hexo dir to run hexo g
4. git d有问题ssh解决不了,所以用TurquoiseGit check in

Theme:
我用的theme为https://hmybmny.com/hexo-theme-concise/
主题可以通过github下载(可以直接下载,解压,放入themes文件夹),然后修改_config.yml改theme名字即可,具体见上面link

Plugin:
npm install hexo-generator-feed --save
npm install hexo-generator-sitemap --save
npm install hexo-math --save
npm install hexo-generator-archive --save

=>atom.xml/sitemap.xml

write an article:
Software\Hexo\source\_posts写一个MD文件
HTML to markdown: https://www.browserling.com/tools/html-to-markdown
Online Markdown Editor: https://www.zybuluo.com/mdeditor

Generate files to localhost:
cd D:\MyFiles\Coding2\Hexo Server
hexo clean
hexo g
hexo s

http://localhost:4000/

Deploy to Prod:
网站文件产生在D:\MyFiles\Coding2\Hexo Server\public的文件夹,然后将其所有文件复制到public-git,这样做因为hexo clean会把.git的文件也去掉。
push public-git to https://github.com/shineboy2013/ by TurtoiseGit
同时备份到Coding\JS\Hexo\source\_posts


Markdown:
https://hexo.io/docs
http://syxiaqj.github.io/2014/02/23/write-blog-with-markdown/
how to use tags

Styling:
可以自己改主题的styling

比如更改table styling,在\Hexo\themes\hexo-theme-concise\source\css\_partial\article.styl
table{ border-collapse: collapse; width: 100%; } .highlight table{ border-collapse: collapse; width: 0% !important; }

th, td {
text-align: left;
padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2}

th {
background-color: #4CAF50;
color: white;
}


比如更改tag cloud styling,在\Hexo\themes\hexo-theme-concise\source\css\_partial\sidebar.styl
覆盖inline style
   .tagcloud a {
 font-size: 20px !important;
   }

Archiving:
然后在站点的 _config.yml 中设置,例如:
index_generator:
  per_page: 5

archive_generator:
  per_page: 0
  yearly: true
  monthly: true

tag_generator:
  per_page: 10

加广告栏:
[concise]
在theme上加入自己的script:
1. 在Hexo Server\themes\hexo-theme-concise\layout\donation加入donation folder
2. 加入sidebar.ejs文件
<div style="color:blue;text-align:center;margin-bottom:10px">
    <h4>Donote me at Venmo</h4>
<img alt="" border="0" src="/images/static/Venmo_QR_Code.png" width="131" height="160" />
</div>
3. 修改Hexo Server\themes\hexo-theme-concise\layout\layout.ejs加入步骤2的script
  <%- partial('donation/sidebar') %>

[next]
2. reward and reward_settings in _config.yml
3. themes\next\source\images\static\ad.jpg
    themes\next\layout\_layout.swig
  
  <a href="mailto:metajobreferral@gmail.com?subject=Mail from Github">
   <img alt="Free mock interview" border="0" src="/images/static/ad.jpg" width="270" />
  </a>

加网站数据:
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?</script>

加favicon:
[concise]
1. 在\themes\hexo-theme-concise加入favicon: /favicon.png
2. 在\themes\hexo-theme-concise\source文件夹中加入图片(路径相对于source)
[next]
themes\next\source\images\favicon-32x32-next.png (favicon in _config.yml)

How to add tags page
[next] https://theme-next.js.org/docs/theme-settings/custom-pages

加评论:
https://shineboy2013.disqus.com/admin/
hexo-theme-concise不支持disqus,所以换了nexT主题

theme_config:
  disqus:
    enable: true
    shortname: xxx
    count: true
    lazyload: false


加Search:
https://theme-next.js.org/docs/third-party-services/search-services

Bugnet, open source bug tracking system for .Net

Sunday, September 4, 2016

PowerShell

当文件很大时,可以用PowerShell来查看
看某一行(跳过N行看之后的M行):
get-content a.csv | select -first 1 -skip 90000

Select-String -Path a.csv -SimpleMatch "12345" | select filename, linenumber

Saturday, July 9, 2016

ASP MVC

Bootstrap: js/css framework
Razor: front-end framework like struts
@Html.DropDownListFor

Code First Data Annotations:
Model->database framework
key如果自定义的话不是identity的话就要用[DatabaseGenerated(DatabaseGenerationOption.None)]
其他validation

Identity:
[DatabaseGenerated(DatabaseGenerationOption.Identity)]
需要manually加入[Id] Int Identity(1,1)

NuGet push changes to database:
Update-Database -Verbose -Force

Route:
http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC








Controller:
Delete non-required fields here








Pirce price = db.Prices.ToList().Where(row=>rowId == priceid).First();
return RedirectToAction("Select", "Manufacturers", new {id=1});

Model:
Add caption







View:



@Url.Action既可用于view也可用于controller不要hardcode url尽量用这个。
@Html.ActionLink("Add another price","Select","Manufacturers", new {id=1},null);
window.location.href='@Url.Action("Create")'+'/'+'@ViewBag.id';

@using (Html.BeginForm(null,null,FormMethod.Post, new{id="myForm",@onSubmit="return validate();"}))
@Html.HiddenFor(model=>model.Id)

Publish website in Windows:
1. publish website to local(right click proj in VS2015)
2. copy all files to production iis dir
3. Restart Default web site


https://www.youtube.com/watch?v=zWFoZb6EiwU&list=PLkhqom1HfPQrtTsUzSZk2Xvs2_F-CGTDq&index=1


select2是一个很好的搜索型多选和单选插件。
datepicker也是日历选择器首选。
$('#radio').select2().val(['700','750']).trigger('change');