-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
6291 lines (5190 loc) · 476 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Contrast Of Beauty]]></title>
<link href="http://blackat.github.io/atom.xml" rel="self"/>
<link href="http://blackat.github.io/"/>
<updated>2016-09-11T13:07:12+02:00</updated>
<id>http://blackat.github.io/</id>
<author>
<name><![CDATA[black@t]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Furure of Declaration Files]]></title>
<link href="http://blackat.github.io/blog/2016/09/11/furure-of-declaration-files/"/>
<updated>2016-09-11T11:15:37+02:00</updated>
<id>http://blackat.github.io/blog/2016/09/11/furure-of-declaration-files</id>
<content type="html"><![CDATA[<p>When I have started to use TypeScript I have looked at <a href="http://definitelytyped.org/">Definitely Typed</a> to see how many people where writing declaration files (<code>.d.ts</code>) and then <a href="https://typescript.codeplex.com/wikipage?title=Writing%20Definition%20%28.d.ts%29%20Files">guidelines from Microsoft</a> to see how to do it. There was then the utility <a href="https://github.com/DefinitelyTyped/tsd">tsd</a>, but now many stuff has changed.</p>
<!-- more -->
<h2>Evolution at Glance</h2>
<h3>TSD Package Manager</h3>
<blockquote><p>TSD is a package manager to search and install TypeScript definition files directly from the community driven DefinitelyTyped repository.</p><footer><strong>DefinitelyTyped</strong> <cite><a href='https://github.com/DefinitelyTyped/tsd'>TSD</a></cite></footer></blockquote>
<p>A tool has to be installed via <code>npm</code> to fetch the package and start to configure a file named <code>tsd.json</code> that holds all the declaration (or definition) files. Basically <code>tsd</code> is like <code>npm</code> with similar commands able to manage defintion dependencies used by TypeScript compiler to transpile <code>.ts</code> files and by type guard to perform runtime check.</p>
<blockquote><p>A type guard is some expression that performs a runtime check that guarantees the type in some scope.</p><footer><strong>TypeScript</strong> <cite><a href='https://www.typescriptlang.org/docs/handbook/advanced-types.html'>Documentation</a></cite></footer></blockquote>
<h3>Typings</h3>
<p>Some months go, looking at the <a href="https://github.com/DefinitelyTyped/tsd">GitHub repository</a>, I have discovered <a href="https://github.com/DefinitelyTyped/tsd/issues/269">the project has been deprecated</a> in favor of <a href="https://github.com/typings/typings">typings</a>. So <code>typings</code> is an evolution of <code>tsd</code> with some advantages explained <a href="https://github.com/typings/typings/issues/72">here</a> by <a href="https://github.com/blakeembrey">blakeembrey</a>.</p>
<p>Basically the previous tool has been improved into a more generic package manager that acts like <code>npm</code>, then it still requires to be installed. Old projects have to be updated to migrate from <code>tsd</code> if previously used. Slighter different syntax needs to be learnt.</p>
<h3>@Types</h3>
<p>Recently I was experimenting <a href="https://github.com/angular/angular-cli">Angular2 Command Line Interface</a> and I have noticed that generating a new project the folder <code>typings</code> was not created, something missing? Not at all, TypeScript 2.0 has improved once again and a new property <code>types</code> has been kicked in the <code>tsconfig.json</code>.</p>
<p>So looking at the <code>tsconfig.json</code> <a href="http://json.schemastore.org/tsconfig">schema definition</a> we can find</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="err">types:</span> <span class="p">{</span>
</span><span class='line'> <span class="nt">"description"</span><span class="p">:</span> <span class="s2">"Type declaration files to be included in compilation. Requires TypeScript version 2.0 or later."</span><span class="p">,</span>
</span><span class='line'> <span class="nt">"type"</span><span class="p">:</span> <span class="s2">"array"</span><span class="p">,</span>
</span><span class='line'> <span class="nt">"items"</span><span class="p">:</span> <span class="p">{</span>
</span><span class='line'> <span class="nt">"type"</span><span class="p">:</span> <span class="s2">"string"</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The evolution is explained in this post from <a href="https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/">Daniel Rosenwasser</a> Program Manager on TypeScript.</p>
<p>In a nutshell</p>
<blockquote><p>Getting type declarations in TypeScript 2.0 will require no tools apart from npm.</p><footer><strong>Daniel Rosenwasser</strong> <cite><a href='https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files'>The Future of Declaration Files</a></cite></footer></blockquote>
<p>Really a good news! we can rid off some extra entities in our project such as:</p>
<ul>
<li>another package managers then <code>npm</code>;</li>
<li><code>json</code> file for type declarations;</li>
<li>configure <code>tsconfig.json</code> to exclude files from <code>typings</code> folder;</li>
<li>the famous <code>///reference</code> at the beginning of each <code>.ts</code> file.</li>
</ul>
<h4>Usage</h4>
<p>Very simple:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>npm install --save @types/lodash
</span></code></pre></td></tr></table></div></figure>
<blockquote><p>From there you’ll be able to use lodash in your TypeScript code with no fuss. This works for both modules and global code.</p><footer><strong>Daniel Rosenwasser</strong> <cite><a href='https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files'>The Future of Declaration Files</a></cite></footer></blockquote>
<p>No more additional configuration or references at the beginning of each file.</p>
<h4>Type Search</h4>
<p>Please refer to <a href="http://microsoft.github.io/TypeSearch/">TypeSearch</a> search engine to find a type definition and how to install it via <code>npm</code>.</p>
<p>At first sight I could say the project is cleaner, less utilities and, may be, a more consistent way to manage <code>.d.ts</code> files. I hope a definitive path has been defined (at least for a couple of years) in order to have all the dependencies centralized in one place.</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[How to Test 2: TDD a Simple Example]]></title>
<link href="http://blackat.github.io/blog/2015/03/21/how-to-test-2-tdd-a-simple-example/"/>
<updated>2015-03-21T12:37:14+01:00</updated>
<id>http://blackat.github.io/blog/2015/03/21/how-to-test-2-tdd-a-simple-example</id>
<content type="html"><![CDATA[<p>Test Driven Development (TDD) is a software development process based on the <em>test-first</em> approach of <a href="http://en.wikipedia.org/wiki/Extreme_programming">extreme programming</a>. Roughly the idea is creating tests before any implementation and then start development guided by test, finally refactor. We will quickly explore the technique and its <em>rhythm</em> to highlight advantages and disadvantages. <strong>DRAFT 1</strong></p>
<!-- more -->
<h3>Components and material</h3>
<p>This tutorial is part of an experiment done to simply explain fundamental concepts of testing. In the <a href="http://blackat.github.io/blog/2015/03/07/how-to-test-1-find-errors-to-make-the-application-works/">previous one</a> you can find concepts and jargon of the testing world along with some exercises which can help you to get in confidence with unit testing and mock objects. The code experiment used in this tutorial is described in the previous article and can be found on <a href="https://github.com/blackat/tutorial-howtotest-1-collectors">GitHub</a>, so please clone it to experiment or just follow some code snippet on this page.</p>
<h2>A bit of theory</h2>
<h2>Learning by example</h2>
<p>Imagine to write a new collector unit which can be plugged into the Cloud Service. At the beginning not all the details are clear and well defined, but with some imagination we can define some functionalities. Just a sort of rough idea and then start to apply the TDD steps one by one.</p>
<h3>First test</h3>
<h5>RED</h5>
<p>So let’s create the <code>FacebookCollectorTddTest.class</code> and implement the first</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FacebookCollectorTddTest</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Test</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">testAcceptGoldenPath</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="n">FacebookCollector</span> <span class="n">collector</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">FacebookCollector</span><span class="o">();</span>
</span><span class='line'> <span class="n">assertTrue</span><span class="o">(</span><span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="k">new</span> <span class="nf">FacebookPost</span><span class="o">(</span><span class="s">"Just a foo post"</span><span class="o">),</span> <span class="mi">1L</span><span class="o">));</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>the code won’t compile because the classes still do not exist. You can ask your IDE to implement the classes for you. For instance in <a href="https://www.jetbrains.com/idea/">IntelliJ IDEA</a> you can exploit the class creation functionality as the screenshot below
<img class="center" src="http://blackat.github.io/images/posts/idea_class_creation.png"></p>
<p>The class we are going to implement should implement the <code>Collector.java</code> interface</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FacebookCollector</span> <span class="kd">implements</span> <span class="n">Collector</span> <span class="o">{</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The <code>Collector</code> interface is already the result of some rough idea about the service, but can be improved or extended in orde to support new features or requirements.</p>
<p>Once the code compile, run the test and make it fails, <strong>Red</strong>. But wait a minute! The assertion does not have any message and when the test fails does not tell us anything interesting.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">java</span><span class="o">.</span><span class="na">lang</span><span class="o">.</span><span class="na">AssertionError</span>
</span><span class='line'> <span class="n">at</span> <span class="n">org</span><span class="o">.</span><span class="na">junit</span><span class="o">.</span><span class="na">Assert</span><span class="o">.</span><span class="na">fail</span><span class="o">(</span><span class="n">Assert</span><span class="o">.</span><span class="na">java</span><span class="o">:</span><span class="mi">86</span><span class="o">)</span>
</span><span class='line'> <span class="n">at</span> <span class="n">org</span><span class="o">.</span><span class="na">junit</span><span class="o">.</span><span class="na">Assert</span><span class="o">.</span><span class="na">assertTrue</span><span class="o">(</span><span class="n">Assert</span><span class="o">.</span><span class="na">java</span><span class="o">:</span><span class="mi">41</span><span class="o">)</span>
</span><span class='line'> <span class="n">at</span> <span class="n">org</span><span class="o">.</span><span class="na">junit</span><span class="o">.</span><span class="na">Assert</span><span class="o">.</span><span class="na">assertTrue</span><span class="o">(</span><span class="n">Assert</span><span class="o">.</span><span class="na">java</span><span class="o">:</span><span class="mi">52</span><span class="o">)</span>
</span><span class='line'> <span class="n">at</span> <span class="n">com</span><span class="o">.</span><span class="na">contrastofbeauty</span><span class="o">.</span><span class="na">tutorial</span><span class="o">.</span><span class="na">collectors</span><span class="o">.</span><span class="na">FacebookCollectorTddTest</span><span class="o">.</span><span class="na">testAcceptGoldenPath</span><span class="o">(</span><span class="n">FacebookCollectorTddTest</span><span class="o">.</span><span class="na">java</span><span class="o">:</span><span class="mi">15</span><span class="o">)</span>
</span></code></pre></td></tr></table></div></figure>
<p>Let’s improve the assertion with a test message</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">testAcceptGoldenPath</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="n">FacebookCollector</span> <span class="n">collector</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">FacebookCollector</span><span class="o">();</span>
</span><span class='line'> <span class="n">FacebookPost</span> <span class="n">facebookPost</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">FacebookPost</span><span class="o">(</span><span class="s">"Just a foo post"</span><span class="o">);</span>
</span><span class='line'> <span class="n">assertTrue</span><span class="o">(</span><span class="s">"Object "</span> <span class="o">+</span> <span class="n">facebookPost</span> <span class="o">+</span> <span class="s">" for user "</span> <span class="o">+</span> <span class="n">USER_ID</span> <span class="o">+</span> <span class="s">" has not been accepted."</span><span class="o">,</span> <span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">facebookPost</span><span class="o">,</span> <span class="n">USER_ID</span><span class="o">));</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<h5>GREEN</h5>
<p>Make the test passes with the minimum and simplest amount of code. As result the <em>first version</em> of the constructor and the <code>accept</code> method could be</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FacebookCollector</span> <span class="kd">implements</span> <span class="n">Collector</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'><span class="kd">private</span> <span class="n">Map</span><span class="o"><</span><span class="n">Long</span><span class="o">,</span> <span class="n">List</span><span class="o"><</span><span class="n">FacebookPost</span><span class="o">>></span> <span class="n">facebookPostMap</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="nf">FacebookCollector</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="n">facebookPostMap</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o"><>();</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="nd">@Override</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">accept</span><span class="o">(</span><span class="n">Object</span> <span class="n">object</span><span class="o">,</span> <span class="kt">long</span> <span class="n">userId</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span><span class="o">(</span><span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">)</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">userId</span><span class="o">,</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">FacebookPost</span><span class="o">>());</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">add</span><span class="o">((</span><span class="n">FacebookPost</span><span class="o">)</span> <span class="n">object</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Now re-running the test it passes. Comparing this way of implementing with the one seen to correct the class by testing you can notice we have some quite a good amount of time because of <code>NullPointerException</code>, they have been spotted immediately and corrected without allowing them to propagate.</p>
<h5>REFACTOR</h5>
<p>Refactor both test and the class to make them more readable such as the <code>setUp()</code> method, the assertion message and the constant</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">FacebookCollectorTddTest</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">long</span> <span class="n">USER_ID</span> <span class="o">=</span> <span class="mi">1L</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">private</span> <span class="n">FacebookCollector</span> <span class="n">collector</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">private</span> <span class="n">FacebookPost</span> <span class="n">facebookPost</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Before</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="n">collector</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">FacebookCollector</span><span class="o">();</span>
</span><span class='line'> <span class="n">facebookPost</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">FacebookPost</span><span class="o">(</span><span class="s">"Just a foo post"</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Test</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">testAcceptGoldenPath</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="n">String</span> <span class="n">message</span> <span class="o">=</span> <span class="s">"Object "</span> <span class="o">+</span> <span class="n">facebookPost</span> <span class="o">+</span> <span class="s">" for user "</span> <span class="o">+</span> <span class="n">USER_ID</span> <span class="o">+</span> <span class="s">" has not been accepted."</span><span class="o">;</span>
</span><span class='line'> <span class="n">assertTrue</span><span class="o">(</span><span class="n">message</span><span class="o">,</span> <span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">facebookPost</span><span class="o">,</span> <span class="n">USER_ID</span><span class="o">));</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<hr />
<h3>Second test</h3>
<h5>RED</h5>
<p>The test is marked as <em>GoldenPath</em> in order to highlight it is the most common case, where everything should run smooth. But what happens at the borders? Our implementation always return true except when an exception happens. So let’s implement another test.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">testAcceptWrongObjectType</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="n">Integer</span> <span class="n">fooObject</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">Integer</span><span class="o">(</span><span class="mi">123</span><span class="o">);</span>
</span><span class='line'> <span class="n">String</span> <span class="n">message</span> <span class="o">=</span> <span class="s">"Object "</span> <span class="o">+</span> <span class="n">fooObject</span> <span class="o">+</span> <span class="s">" for user "</span> <span class="o">+</span> <span class="n">USER_ID</span> <span class="o">+</span> <span class="s">" must not be accepted."</span><span class="o">;</span>
</span><span class='line'> <span class="n">assertFalse</span><span class="o">(</span><span class="n">message</span><span class="o">,</span> <span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">fooObject</span><span class="o">,</span> <span class="n">USER_ID</span><span class="o">));</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The test will fail because of a <code>java.lang.ClassCastException</code>, the declaration of the list <code>List<FacebookPost></code> prevents some wrong addition to the list.</p>
<h5>GREEN</h5>
<p>It is enough doing a check on the type of object to make the test passes</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Override</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">accept</span><span class="o">(</span><span class="n">Object</span> <span class="n">object</span><span class="o">,</span> <span class="kt">long</span> <span class="n">userId</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">object</span> <span class="k">instanceof</span> <span class="n">FacebookPost</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">)</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">userId</span><span class="o">,</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">FacebookPost</span><span class="o">>());</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">add</span><span class="o">((</span><span class="n">FacebookPost</span><span class="o">)</span> <span class="n">object</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<h5>REFACTOR</h5>
<p>Test and class should be fine.</p>
<hr />
<h3>Third test</h3>
<p>Roughly a collector starts to collect posts from a user, when a certain number of posts is reached, the collector calls the method <code>flush()</code> to submit the job.</p>
<h5>RED</h5>
<p>Let’s write a failing test</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">testAcceptCallFlushMethod</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">setNewBufferSize</span><span class="o">(</span><span class="mi">2</span><span class="o">);</span>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">setCallbackFunction</span><span class="o">(</span><span class="n">callbackMock</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">facebookPost</span><span class="o">,</span> <span class="n">USER_ID</span><span class="o">);</span>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">facebookPost</span><span class="o">,</span> <span class="n">USER_ID</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">verify</span><span class="o">(</span><span class="n">callbackMock</span><span class="o">,</span> <span class="n">times</span><span class="o">(</span><span class="mi">1</span><span class="o">)).</span><span class="na">addTask</span><span class="o">(</span><span class="n">any</span><span class="o">(</span><span class="n">Callable</span><span class="o">.</span><span class="na">class</span><span class="o">),</span> <span class="n">anyInt</span><span class="o">());</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The test sets the <em>buffer size</em> equal to two so it is enough adding two posts to have the <code>flush</code> method called. By using Mockito it is possible to verify if a certain method has been called and how many times. This is <em>behavior verification</em> and happens on <em>indirect outputs</em>.</p>
<h5>GREEN</h5>
<p>A solution for the method could be</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Override</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">accept</span><span class="o">(</span><span class="n">Object</span> <span class="n">object</span><span class="o">,</span> <span class="kt">long</span> <span class="n">userId</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">object</span> <span class="k">instanceof</span> <span class="n">FacebookPost</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">)</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">userId</span><span class="o">,</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">FacebookPost</span><span class="o">>());</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">add</span><span class="o">((</span><span class="n">FacebookPost</span><span class="o">)</span> <span class="n">object</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">newBufferSize</span> <span class="o">!=</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="n">newBufferSize</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">flush</span><span class="o">(</span><span class="n">userId</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="n">BUFFER_SIZE</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">flush</span><span class="o">(</span><span class="n">userId</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p> To make the test work we have to implement other three methods such as <code>flush()</code>, <code>setNewBufferSize()</code> and setCallbackFunction(). SO it is possible to temporary move to other tests and then come back or just provide a fake implementation to produce the call of the method we want to verify. It depends a lot on experience, confidence with the task and how strict we want to adhere to TDD approach. Two methods are setters and they should not be tested, the third one could be temporary implemented as</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Override</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">flush</span><span class="o">(</span><span class="kt">long</span> <span class="n">userId</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="n">callbackFunction</span><span class="o">.</span><span class="na">addTask</span><span class="o">(</span><span class="k">new</span> <span class="nf">Callable</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="nd">@Override</span>
</span><span class='line'> <span class="kd">public</span> <span class="n">Object</span> <span class="nf">call</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">},</span> <span class="n">userId</span><span class="o">);</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The test passes.</p>
<h5>REFACTOR</h5>
<p>Improve a bit the <code>accept()</code> method to make it more readable and compact. One solution could be</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Override</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">accept</span><span class="o">(</span><span class="n">Object</span> <span class="n">object</span><span class="o">,</span> <span class="kt">long</span> <span class="n">userId</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">object</span> <span class="k">instanceof</span> <span class="n">FacebookPost</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">)</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">userId</span><span class="o">,</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">FacebookPost</span><span class="o">>());</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">add</span><span class="o">((</span><span class="n">FacebookPost</span><span class="o">)</span> <span class="n">object</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="kt">int</span> <span class="n">bufferSize</span> <span class="o">=</span> <span class="n">newBufferSize</span> <span class="o">!=</span> <span class="mi">0</span> <span class="o">?</span> <span class="n">newBufferSize</span> <span class="o">:</span> <span class="n">BUFFER_SIZE</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="n">bufferSize</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">flush</span><span class="o">(</span><span class="n">userId</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<hr />
<h3>Fourth test</h3>
<p>It’s time to test the <code>flush()</code> method. Write a unit test, make it fails and re-implement it in a more appropriate way than what done before.
When the <code>flush()</code> method is called, it creates a task and invokes a method against a callback function, the list for a given user is cleared.</p>
<h5>RED</h5>
<p>The failing test</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">testFlushGoldenPath</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">setCallbackFunction</span><span class="o">(</span><span class="n">callbackMock</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">facebookPost</span><span class="o">,</span> <span class="n">USER_ID</span><span class="o">);</span>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="n">facebookPost</span><span class="o">,</span> <span class="n">USER_ID</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">String</span> <span class="n">message1</span> <span class="o">=</span> <span class="s">"The list size for the user "</span> <span class="o">+</span> <span class="n">USER_ID</span> <span class="o">+</span> <span class="s">" must be equal to 2"</span><span class="o">;</span>
</span><span class='line'> <span class="n">assertEquals</span><span class="o">(</span><span class="n">message1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> <span class="n">collector</span><span class="o">.</span><span class="na">getListSizeByUserId</span><span class="o">(</span><span class="n">USER_ID</span><span class="o">));</span>
</span><span class='line'>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">flush</span><span class="o">(</span><span class="n">USER_ID</span><span class="o">);</span>
</span><span class='line'> <span class="n">String</span> <span class="n">message2</span> <span class="o">=</span> <span class="s">"The list size after a call to flush() must be 0"</span><span class="o">;</span>
</span><span class='line'> <span class="n">assertEquals</span><span class="o">(</span><span class="n">message2</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">collector</span><span class="o">.</span><span class="na">getListSizeByUserId</span><span class="o">(</span><span class="n">USER_ID</span><span class="o">));</span>
</span><span class='line'>
</span><span class='line'> <span class="n">verify</span><span class="o">(</span><span class="n">callbackMock</span><span class="o">,</span> <span class="n">times</span><span class="o">(</span><span class="mi">1</span><span class="o">)).</span><span class="na">addTask</span><span class="o">(</span><span class="n">any</span><span class="o">(</span><span class="n">Callable</span><span class="o">.</span><span class="na">class</span><span class="o">),</span> <span class="n">anyInt</span><span class="o">());</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>We test</p>
<pre><code>- posts have been correctly added
- after the call to the `flush()` method the list is empty
- the method `addTask()` from callback is invoked
</code></pre>
<h5>GREEN</h5>
<p>Solution</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Override</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">flush</span><span class="o">(</span><span class="kt">long</span> <span class="n">userId</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="n">List</span><span class="o"><</span><span class="n">FacebookPost</span><span class="o">></span> <span class="n">list</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><>(</span><span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">));</span>
</span><span class='line'> <span class="n">facebookPostMap</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">clear</span><span class="o">();</span>
</span><span class='line'>
</span><span class='line'> <span class="n">FacebookTask</span> <span class="n">facebookTask</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">FacebookTask</span><span class="o">(</span><span class="n">list</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">callbackFunction</span><span class="o">.</span><span class="na">addTask</span><span class="o">(</span><span class="n">facebookTask</span><span class="o">,</span> <span class="n">userId</span><span class="o">);</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The test run fine.</p>
<h5>REFACTOR</h5>
<p>The class seems fine.</p>
<hr />
<h3>Fifth test</h3>
<p>Add exception in case the callback function is not set. This is a borderline test</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[How to Test 1: Find Errors to Make the Application Works]]></title>
<link href="http://blackat.github.io/blog/2015/03/07/how-to-test-1-find-errors-to-make-the-application-works/"/>
<updated>2015-03-07T09:37:33+01:00</updated>
<id>http://blackat.github.io/blog/2015/03/07/how-to-test-1-find-errors-to-make-the-application-works</id>
<content type="html"><![CDATA[<p>This is an <em>experiment tutorial</em> to better learn some <em>101</em> practices and how testing can be a better replacement of developing by debugging.</p>
<p>The goal of the <em>experiment-project</em> is to find, to correct bugs and make the application works by writing tests.</p>
<!-- more -->
<h2>A bit of theory and jargon</h2>
<p><em>SUT:</em> the System Under Test, in a unit test it could be a <em>class</em>.</p>
<p><em>Test class:</em> the <em>client</em> of the interface exposed by the SUT.</p>
<p><em>test fixture:</em> the context under which a test is run, it is a baseline under which running tests. It is a set of operations to bring the <em>System Under Test (SUT)</em> in a given state, run the test and verify the <em>expected outcome</em>. Test fixture and the expected outcome are tightly coupled, the test fixture allows the test to be repeatable for ever and ever. Method <code>setUp()</code> or <code>initialization</code> is used to create fixture while <code>tearDown()</code> or <code>distruction</code> is invoked to restore the original state.</p>
<p>A test has four phases:</p>
<ol>
<li><strong>set up</strong> to <em>apply</em> the test fixture;</li>
<li><strong>exercise</strong> to <em>interact</em> with the API exposed by the SUT;</li>
<li><strong>verifiy</strong> to check if the expected outcome has been <em>matched</em>;</li>
<li><strong>tear down</strong> to return to the <em>original state</em> tearing down the test fixture.</li>
</ol>
<h2>Clone the project</h2>
<p>Clone the project from <a href="https://github.com/blackat/tutorial-howtotest-1-collectors">Github</a> and import it in you preferred IDE, <a href="https://www.jetbrains.com/idea/">IntelliJ IDEA</a> in my case.</p>
<h3>Project Structure</h3>
<pre><code>├── README.md
├── exercise-api
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── com
│ └── contrastofbeauty
│ └── tutorial
│ └── api
│ ├── collectors
│ │ └── Collector.java
│ ├── domain
│ │ ├── AcknoledgeService.java
│ │ └── Callback.java
│ └── services
│ └── Service.java
├── exercise-to-be-corrected
│ ├── pom.xml
│ └── src
│ ...
│
├── exercise-working
│ ├── pom.xml
│ └── src
│ ...
│
└── pom.xml
</code></pre>
<p>The maven project is composed by four modules:</p>
<ol>
<li><strong>exercise-api:</strong> just collection of interfaces used in the other two modules;</li>
<li><strong>exercise-working:</strong> working application with a test suite;</li>
<li><strong>exercise-to-be-corrected:</strong> application with bugs where the test suite has to be created in order to find, correct bugs and make it work;</li>
<li><strong>exercise-tdd:</strong> implementation of some classes by tdd whose description is in the next <a href="http://blackat.github.io/blog/2015/03/21/how-to-test-2-tdd-a-simple-example">tut</a>.</li>
</ol>
<p>In the module <code>exercise-to-be-corrected</code> run the class <code>RunMeToBeCorrected.java</code> and see that it fails some some reason, may be a <code>NullPointerException</code> is thrown. We do not care so much because we do not want to make it work by a common pattern <strong>running/debugging</strong> rather we want to <em>implement missing tests</em> demonstrating how they can help us to find and correct bug make our application more robust.</p>
<h2>Application Description</h2>
<p>The application is mainly composed by a fake <a href="https://github.com/blackat/tutorial-howtotest-1-collectors/blob/master/exercise-working/src/main/java/com/contrastofbeauty/tutorial/services/CloudService.java">Cloud Service</a> able to post user’s tweets in batch by means of a <a href="https://github.com/blackat/tutorial-howtotest-1-collectors/blob/master/exercise-working/src/main/java/com/contrastofbeauty/tutorial/collectors/TweetCollector.java">Tweet Collector</a>. <a href="http://yuml.me/edit/3555184c">Here</a> the UML source.</p>
<p><img class="center" src="http://blackat.github.io/images/posts/tutorial_1_how_to_test.png"></p>
<h3>The workflow</h3>
<ol>
<li>The <code>CloudService</code> <em>has a</em> certain numbers of different type collectors (hence the association in the UML diagram), in this case just with one able to collect tweets.</li>
<li>A user who wants to post tweet in batch has to open a connection invoking the method <code>service.openConnection()</code> of the service.</li>
<li>The user then starts to post tweets using the method <code>service.saveObject()</code>.</li>
<li>Once done <code>service.saveObjectCompleted()</code> method will be called to tell the service that the user session is finished.</li>
</ol>
<h4>The workflow in details</h4>
<ol>
<li>The <code>CloudService</code> <em>has a</em> <code>TweetCollector</code> and a <code>Callback</code> implementation instance (Another approach is to leave the service the initialization of the callback function and substitute the <em>association</em> with a <em>composition</em> in the UML diagram).</li>
<li>The <code>Callback</code> implementation instance will be set into each added collector by the service.</li>
<li>Every time a user wants to save a tweet through the service, the collector will stock it in a <em>list</em> as a sort of buffer.</li>
<li>Once the collector has stocked a certain amount of tweets, for instance 500, it will generates a <em>task</em>.</li>
<li>A task has a collection of tweets and the implementation of the method <code>call()</code> which will be called by one of the thread of the thread pool. The method specifies how the list of tweets should be treated, for instance: saved, destroyed, sent to Twitter, printed or something else.</li>
<li>A collector uses the <code>Callback</code> instance to add a task to a <em>processing list</em>.</li>
<li>The list of active tasks will be then processed by a thread pool.</li>
</ol>
<h3>How to initiliaze the service and run it</h3>
<figure class='code'><figcaption><span>RunMeWorking.java</span> — <a href='https://github.com/blackat/tutorial-howtotest-1-collectors/blob/master/exercise-working/src/main/java/com/contrastofbeauty/tutorial/RunMeWorking.java'>Link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">RunMeWorking</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="n">Callback</span> <span class="n">callback</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">CallbackImpl</span><span class="o">();</span>
</span><span class='line'> <span class="n">Service</span> <span class="n">service</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">CloudService</span><span class="o">(</span><span class="n">callback</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">service</span><span class="o">.</span><span class="na">addCollector</span><span class="o">(</span><span class="k">new</span> <span class="nf">TweetCollector</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'> <span class="n">service</span><span class="o">.</span><span class="na">openConnection</span><span class="o">(</span><span class="mi">1L</span><span class="o">);</span>
</span><span class='line'> <span class="n">service</span><span class="o">.</span><span class="na">saveObject</span><span class="o">(</span><span class="k">new</span> <span class="nf">Tweet</span><span class="o">(</span><span class="s">"I am Felix the awesome cat."</span><span class="o">),</span> <span class="mi">1L</span><span class="o">);</span>
</span><span class='line'> <span class="n">service</span><span class="o">.</span><span class="na">saveObjectCompleted</span><span class="o">(</span><span class="k">new</span> <span class="nf">AcknoledgeServiceImpl</span><span class="o">(),</span> <span class="mi">1L</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Actually</strong> the service does not post anything to Twitter, it is just a way to show a bit of java concurrency and how to write <em>unit tests</em> when dealing with concurrent structures like <code>Future</code>.</p>
<h2>Let’s correct the application</h2>
<p>Let’s create tests for the application starting from <code>TweetCollector.java</code> class, press <code>cmd + shift + T</code> on Mac or <code>crtl + shift + T</code> on Windows and choose methods you want to test, then Idea will create the test class in the right place</p>
<pre><code>src/test/java/com/contrastofbeauty/tutorial/collectors/TweetCollectorTest.java.
</code></pre>
<p><strong>Tip</strong> Make sure the path <code>src/test/java</code> exists, otherwise Idea will create the test class together with the source one.</p>
<p><img class="center" src="http://blackat.github.io/images/posts/idea-test-wizard.png"></p>
<h3>TweetCollector Class</h3>
<p><img class="center" src="http://blackat.github.io/images/posts/tweet-collector.png"></p>
<p><a href="https://github.com/blackat/tutorial-howtotest-1-collectors/blob/master/exercise-to-be-corrected/src/main/java/com/contrastofbeauty/tutorial/collectors/TweetCollector.java">TweetCollector</a> is specific implementation of <code>Collector</code> interface able to stores an object of a specific type in a <code>List</code> by <code>userId</code> when its method <code>accept()</code> is invoked (e.g. by the service cloud).</p>
<p>Once the tweet collector has collected a certain number of tweet objects, <code>flush()</code> method is called, a <code>TweetTask</code> object is created and passed an parameter to <code>callbackFunction.addTask()</code>. The task in then put in a queue to be processed. A task is a computation unit, it owns:</p>
<ul>
<li><code>call()</code> method: called by a thread when the task is available in the task queue;</li>
<li>processing list: a set of object on which the aforementioned method will work.</li>
</ul>
<p>Once the task is added to the queue the list is emptied, ready to accept new object to be processed.</p>
<h3>TweeterCollectorTest</h3>
<p>Populate the <code>setUp()</code> method creating a new <code>TweetCollector</code> and annotate it with <code>@Before</code> in order to get it run automatically before any method annotated with <code>@Test</code> as specfied by <a href="http://junit.sourceforge.net/javadoc/org/junit/Before.html">JUnit doc</a>. Evrytime a test method is run, the <code>setUp()</code> method is invoked to have a fresh and clean collector object.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TweetCollectorTest</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">private</span> <span class="n">Collector</span> <span class="n">collector</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="nd">@Before</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUp</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="n">collector</span> <span class="o">=</span> <span class="k">new</span> <span class="nf">TweetCollector</span><span class="o">();</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<h4>Method accept() - 3 errors</h4>
<p>Let’s start working on the method <code>accept(Object object, long userId)</code>. Create, or rename the method if already created by the IDE, <code>testAcceptGoldenPath()</code> and add the following assertion:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">testAcceptGoldenPath</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="n">assertTrue</span><span class="o">(</span><span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="k">new</span> <span class="nf">Tweet</span><span class="o">(</span><span class="s">"foo tweet"</span><span class="o">),</span> <span class="mi">1L</span><span class="o">));</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>In the first test we want to test the <em>standard scenario</em> when everything run smooth, no exceptional situations. So for this reason it has been added the suffix <em>GoldenPath</em>, an alternative to <a href="http://en.wikipedia.org/wiki/Happy_path">Happy Path</a>. After that some borderline scenario tests will be added. <em>So run the test!</em></p>
<h5>Error 1</h5>
<p><strong>Issue:</strong> a <code>NullPointerException</code> is thrown.</p>
<p><strong>Solution:</strong> the object <code>processingList</code> has not been initialized. Add the initialization in the constructor for instance and <em>rerun the test</em>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="nf">TweetCollector</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="n">processingList</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o"><>();</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<h5>Error 2</h5>
<p><strong>Issue:</strong> another <code>NullPointerException</code> is thrown because the data structure is accessed but not initialized for a given user:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">processingList</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">add</span><span class="o">((</span><span class="n">Tweet</span><span class="o">)</span> <span class="n">object</span><span class="o">);</span>
</span></code></pre></td></tr></table></div></figure>
<p><strong>Solution:</strong> check if a given <code>userId</code> already exists in the map, if not create and add to the map the pair and <em>rerun the test</em>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="k">if</span> <span class="o">(</span><span class="n">processingList</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">)</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">processingList</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">userId</span><span class="o">,</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">Tweet</span><span class="o">>());</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<h5>Error 3</h5>
<p><strong>Issue:</strong> an <code>AssertionError</code> is thrown. All the <code>NPE</code> have been fixed but it seems the collector has not accepted the <code>Tweet</code> object.</p>
<p><strong>Solution:</strong> a missing <code>return true</code> prevent the method to behave correctly. Add the aforementioned statement and <em>rerun the test</em>. Now the test passes and this is the complete code for the method <code>accept</code>:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Override</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">accept</span><span class="o">(</span><span class="n">Object</span> <span class="n">object</span><span class="o">,</span> <span class="kt">long</span> <span class="n">userId</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">object</span> <span class="k">instanceof</span> <span class="n">Tweet</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">processingList</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">)</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">processingList</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">userId</span><span class="o">,</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">Tweet</span><span class="o">>());</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="n">processingList</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">add</span><span class="o">((</span><span class="n">Tweet</span><span class="o">)</span> <span class="n">object</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">customBufferSize</span> <span class="o">!=</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">if</span> <span class="o">(</span><span class="n">processingList</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="n">customBufferSize</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">flush</span><span class="o">(</span><span class="n">userId</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="o">}</span> <span class="k">else</span> <span class="nf">if</span> <span class="o">(</span><span class="n">processingList</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">).</span><span class="na">size</span><span class="o">()</span> <span class="o">==</span> <span class="n">PROCESSING_LIST_BUFFER_SIZE</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="n">flush</span><span class="o">(</span><span class="n">userId</span><span class="o">);</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<h5>Additional tests</h5>
<p>The method now seems correct but has been tested in case of a different obejcts? Add a new test method which will be part of the <em>automatic test suite</em> we are going to create. This automatic test suite will help us during phases such as refactoring, improving code readability and method evolution.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">testAcceptObjectNotAcceptedBecauseDifferentType</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="n">assertFalse</span><span class="o">(</span><span class="n">collector</span><span class="o">.</span><span class="na">accept</span><span class="o">(</span><span class="k">new</span> <span class="nf">Object</span><span class="o">(),</span> <span class="mi">1L</span><span class="o">));</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>Our automatic net of tests starts to take shape.</p>
<h4>Method flush() - 1 error</h4>
<p>The method <code>flush()</code> does not have a return type, so how can we test the <em>correct behavior</em>?</p>
<p><strong>Idea:</strong> indeed, we want to test the correct behavior! we need to find a way to check if the behavior of the method is the expected one so if it follows the right path. When <code>flush()</code> is invoke we expect the creation of a <code>TweetTask</code> object and the invocation of <code>addTask()</code> method.</p>
<p>Let’s try step by step. If not done yet by the IDE, create method <code>testFlushGoldenPath()</code> and invoke the method <code>flush()</code>, then <em>run the test!</em></p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="nd">@Test</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">testFlushGoldenPath</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'> <span class="n">collector</span><span class="o">.</span><span class="na">flush</span><span class="o">(</span><span class="n">USER_ID</span><span class="o">);</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<h5>Error 1</h5>
<p><strong>Issue:</strong> a <code>NullPointerException</code> is thrown. This is not a good behavior, the map has not been initialized if the method will be directly called.</p>
<p><strong>Pre-solution:</strong> check if in the map exist a <code>List</code> for the given <code>userId</code>, if not simply exit from the method execution (other solutions are acceptable, depends on requirements), <em>rerun the method</em>.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="k">if</span> <span class="o">(</span><span class="n">processingList</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">userId</span><span class="o">)</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span><span class="o">;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>The test passes, but <strong>no verification</strong> is done, so the test is pretty <em>useless</em>. We want to verify that if there is an item, a new <code>TweetTask</code> is created and the method <code>callbackFunction.addTask()</code> is invoked.</p>
<p><strong>Idea:</strong> the <code>callbackFunction</code> has not been set yet so a possible <code>NPE</code> could arise. May be in the future it will be inject by <em>DI</em>. So we can use <a href="https://code.google.com/p/mockito/">Mockito</a> to create a mock object and verify if the method <code>addTask()</code> has been called.</p>
<p><strong>Solution:</strong> mock a <code>Callback.class</code> class in order to make a verification on an <em>expected behavior</em> and change a bit the <code>setUp()</code> method to initialize mocks via annotations. Moreover set the callback and add at least one tweet object just to reproduce a small common scenario.</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>