-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathslides.tex
2490 lines (1809 loc) · 72.2 KB
/
slides.tex
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
\documentclass[11pt, xcolor=svgnames]{beamer}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{times}
\usepackage{sans}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\usepackage{color}
\usepackage{verbatim}
\usepackage{url}
\usepackage{listings}
\lstset{
basicstyle=\footnotesize\tt, % the size of the fonts that are used for the code
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
captionpos=b, % sets the caption-position to bottom
extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
frame=single, % adds a frame around the code
language=Java, % the language of the code
keywordstyle=\bf,
showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
showstringspaces=false, % underline spaces within strings only
showtabs=false, % show tabs within strings adding particular underscores
tabsize=2, % sets default tabsize to 2 spaces
showstringspaces=false,
keywordstyle=\color{blue},
commentstyle=\color{green},
stringstyle=\color{mauve}
}
%
% \lstset
% {
% language=SQL,
% basicstyle=\footnotesize,
% columns=fullflexible,
% keepspaces=true,
% breaklines=true,
% breakatwhitespace=true,
% tabsize=3,
% extendedchars=true,
% showstringspaces=false,
% keywordstyle=\color{blue},
% commentstyle=\color{dkgreen},
% stringstyle=\color{mauve}
% }
\definecolor{mauve}{rgb}{0.58,0,0.82}
\def\Tiny
{
\fontsize{5pt}{5pt}
\selectfont
}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzstyle{block}=[draw opacity=0.7,line width=1.4cm]
\mode<presentation>
{
\usetheme{Warsaw}
\usefonttheme{serif}
\usecolortheme{whale}
\usefonttheme{structurebold}
\useoutertheme{infolines}
\setbeamercovered{transparent}
\setbeameroption{show notes}
\setbeamertemplate{blocks}[rounded][shadow=true]
\setbeamertemplate{navigation symbols}{}
\beamertemplateballitem
}
\tolerance=1000
\providecommand{\alert}[1]{\textbf{#1}}
\def\fig#1#2#3{
\begin{figure}[H]
\center
\includegraphics[#1]{#2}
\caption{#3\label{fig:#2}}
\end{figure}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title {Introduction to Unit Testing}
\author[Daniel Rodriguez]{
\textcolor{green!50!black}{Daniel Rodriguez}%\inst{1}
}
\institute[UAH]{University of Alcala}
\date{}
\setcounter{tocdepth}{1}
\begin{document}
\maketitle
\begin{frame}[fragile]{Outline}
\tableofcontents
\end{frame}
\AtBeginSection
{
\begin{frame}
\tableofcontents[currentsection]
\end{frame}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Definitions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Testing}
\begin{itemize}
\item \alert{Error}: A human mistake.
\item \alert{Fault}: The result of a mistake, evidenced in some development or maintenance product.
\item \alert{Failure}: A departure from the system's required behaviour.
\end{itemize}
\begin{itemize}
\item \alert{Fault identification (testing)}: Testing is the process of showing the presence of a software error. What fault caused the failure?
\item \alert{Fault correction or removal (debugging)}: Debugging is the process of discovering the location of an error and its subsequent modification, i.e., changing the system to remove the fault.
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Verification vs. Validation - IEEE Std610.12-1990}
\begin{itemize}
\item \alert{Requirements Validation}. Process of checking that a document or piece of software accurately reflects the customer requirements - i.e. \\
ARE WE BUILDING THE RIGHT SYSTEM?
\item \alert{Software Verification}. Process of ensuring that the product of one phase satisfies the product of a previous phase. sChecking that the software produced is a faithful implementation of the requirements specifiedAssessment of the quality of the implementation \\
ARE WE BUILDING IT RIGHT?
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Testing Process}
\begin{figure}
\includegraphics[width=300pt]{./figs/testing}
%\caption{}
\end{figure}
\tiny{(Source: Plfeeger)}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Test Driven Development}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Fail, Pass, Refactor: TDD Circle of life}
\begin{figure}
\includegraphics[width=200pt]{./figs/tdd-circle-of-life.png}
%\caption{}
\end{figure}
\tiny{Source:~\url{https://leantesting-wp.s3.amazonaws.com/resources/wp-content/uploads/2015/02/tdd-circle-of-life.png}}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Fail, Pass, Refactor: TDD Circle of life}
\begin{block}{TDD Life-cycle}
\begin{enumerate}
\item Identify functionality to include
\item Write failing test
\item Make it compile as quickly as possible
\item Make it pass a quickly as possible
\item Refactor: remove duplication while maintaining 100\% pass rate
\item Repeat as required
\end{enumerate}
\end{block}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Assertions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{What are Assertions?}
An \alert{assertion} specifies a constraint over some state of computation. If it evaluates to false, it implies there exists an incorrect state in the program.
Assertions:
\begin{itemize}
\item can test whether the execution state is consistent at the point of examination.
\item are predicates involving the state variables of a program in execution.
\item are embedded in the program text.
\end{itemize}
Assertions are part of most programming languages.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Assertions: History}
Hoare and Floyd started in the 60s (Hoare logic) the formal verification of programs. They introduced the concept of \emph{preconditions}, \emph{postconditions} and \emph{invariants}.
A triple describes how the execution of a piece of code changes the state of the computation:
$[\{P\}C\{Q\}]$
\noindent where $P$ and $Q$ are assertions and $C$ is code.
\begin{itemize}
\item $P$ precondition
\item $Q$ postcondition
\end{itemize}
In other words, a program behaviour can be verified using preconditions, postconditions and invariants.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Assertions in Java}
Assertions in Java are written as:
\begin{lstlisting}[language=JAVA,basicstyle=\small]
assert expression;
\end{lstlisting}
\texttt{assert} is a Java keyword and \texttt{expression} is any Boolean expression. When evaluated to \texttt{false}, an exception, \texttt{AssertionError}, will be raised.
They can also provide information for developers:
\begin{lstlisting}[language=JAVA,basicstyle=\small]
assert expression1 : expression2;
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{\texttt{Assert} Example}
\begin{lstlisting}[language=JAVA,basicstyle=\scriptsize]
public double avgMarks(double[] marks){
// Pre-condition
assert marks.length > 0 : "Array is empty!";
double avg = 0;
for (int i = 0 ; i < marks.length; i++){
//Invariant
assert marks[i] >= 0 && marks[i] <= 10 : "Mark is not valid!";
avg += marks[i];
}
return avg/marks.length;
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{\texttt{Assert} example}
\begin{lstlisting}[language=JAVA,basicstyle=\scriptsize]
public class AssertionExample {
public double avgMarks(double[] marks){
assert marks.length > 0 : "Array is empty!";
double avg = 0;
for (int i = 0; i < marks.length; i++) {
assert marks[i] >= 0 && marks[i] <= 10 : "Mark is not valid!";
avg += marks[i];
}
return avg/marks.length;
}
public static void main(String args[]){
AssertionExample e = new AssertionExample();
double[] marks = {10, 5, 11, 3, 2, 7.5};
System.out.println(e.avgMarks(marks));
marks = new double[0];
System.out.println(e.avgMarks(marks));
}
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{\texttt{Assert} example}
When assertions are evaluated when enabled, i.e., with the VM options \texttt{-enableassertions} or \texttt{-ea}:
\begin{lstlisting}[language=JAVA,basicstyle=\scriptsize]
$java -ea AssertionExample
\end{lstlisting}
\begin{center}
\includegraphics[width=\columnwidth]{./figs/assertionsExeption}
\end{center}
%assertionsExeption
Otherwise, it will run normally as assertions are designed to be run during the testing phase or debugging.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Assertions vs Exceptions}
Assertions are designed to check the program's correctness, i.e, as \emph{preconditions}, \emph{postconditions} or \emph{invariants}.
Assertions can be used to verify the state of a program but should NOT be used to:
\begin{itemize}
\item Validate arguments of public methods
\item Substitute the \textbf{exception handling} mechanims of a programming language
\item Modify values of attributes or objects
\item Be used in production code (assertions abruptly stop the execution of a program)
\end{itemize}
Exception handling is the process of responding to the occurrence of exceptions (usually in a graceful manner so that the execution is not interrupted). In Java, this is carried out with \texttt{try() ... catch() ... finally} statements.
Although most programming languages provide assertions, unit testing is usually carried out with frameworks such as jUnit (Java) or pyest (Python).
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% JUnit %%%
\section{JUnit}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{What is JUnit}
\begin{block}{}
\centering \url{https://www.junit.org/}
\end{block}
\begin{itemize}
\item JUnit is the most popular open source unit testing framework in Java.
\item JUnit is a regression testing framework.
\item Created in 1997 by Erich Gamma and Kent Beck.
\item Simple framework with a small learning curve.
\item Tests can be automated (facilitates regression testing). It allow us to aggregate tests via test suites.
\item \textit{De facto} standard, integrated with Ant, Maven, Gradle, etc. Typically run with continuous integration testing tools.
%\item \texttt{\url{https://www.junit.org/}}
\end{itemize}
\begin{block}{}
''Never in the field of software development have so many owed so much to so few lines of code'' (Martin Fowler)
\end{block}
\end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{frame}{JUnit 3 - (Obsolete)}
% There are still a lot of test cases and frameworks that use JUnit 3 or 4. We briefly describe how to do test cases with JUnit 3 and 4 to understand them (But we will only work with JUnit 5).
% JUnit 3 was based on inheritance and name conventions (it is common to still use them). With JUnit 3, we needed to create test classes as follows:
% \begin{itemize}
% \item Add \texttt{JUnit3<version>.jar} to the \texttt{classpath}
% \item Create a subclass extending the \texttt{TestCase} class provided by JUnit
% \item Define one or more public \texttt{testXXX()} methods in the subclass.
% \begin{itemize}
% \item Write assert methods inside the \texttt{testXXX()} methods to compare actual results (obtained from the CUT) with the expected results (the oracle)
% \end{itemize}
% \item Optionally, define \texttt{main()} to run the TestCase in batch mode (test runner)
% \end{itemize}
% \end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{frame}[fragile]{JUnit 3 (Obsolete) - Architecture }
% Main classes:
% \begin{center}
% \includegraphics[width=300pt]{./figs/UMLJUnit3}
% \end{center}
% \end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \begin{frame}[fragile]{JUnit3 (Obsolete) Minimal example}
% \begin{lstlisting}[language=Java,basicstyle=\scriptsize]
% import junit.framework.TestCase;
% public class MinimalTest extends TestCase {
% public MinimalTest(String name) {
% super(name);
% }
% // Test method
% public void testCUT () {
% int expected = 2; //expected result
% int actual = new CUT(); //get the actual result from CUT
% assertTrue(actual, expected);
% }
% // Not needed, it's possible to use Test runner
% public static void main(String[] args){
% junit.textui.TestRunner.run(MinimalTest.class);
% }
% }
% \end{lstlisting}
% \end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % \begin{frame}[fragile]{JUnit 3 (Obsolete) - Characteristics}
% % Fixtures are used to initialize and release common test data, this is done with \texttt{setUp()} and \texttt{tearDown()}.
% % \begin{itemize}
% % \item \texttt{setUp()} is used for setting things up before \textbf{each} test
% % \item \texttt{tearDown()} for tearing things down after \textbf{each} test
% % \end{itemize}
% % Suites can contain multiple test cases: \texttt{addTest(Test test)}
% % \begin{lstlisting}[language=JAVA,basicstyle=\scriptsize]
% % public static void main (String [] args){
% % junit.textui.TestRunner.run (suite ());
% % }
% % // TestSuite implements Test interface
% % public static Test suite() {
% % TestSuite suite = new TestSuite("AllTests");
% % suite.addTest(new TestSuite(FloatTest.class));
% % suite.addTest(new TestSuite(BoolTest.class));
% % return suite;
% % }
% % public void testAllTests () throws Exception{
% % assertTrue (suite != null);
% % }
% % \end{lstlisting}
% % \end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{JUnit 4}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{JUnit 4}
JUnit 4 took advantage of imports and annotations included in JDK 5+.
\begin{itemize}
\item Static imports. It allow us to make use of static variables and methods from other classes without qualifying them (name the class).
\item Annotations
\begin{itemize}
\item Annotations are metadata that provide information to the compiler (or other tools)
\item JUnit 4 works with annotations instead of extending classes. There is no need for naming conventions as in previous versions but those naming patterns are still used.
\end{itemize}
\item JUnit4 also improved the functionality with annotations
\begin{itemize}
\item Timeouts: \texttt{@Test(timeout=1000)}...
\item Testing exceptions: \texttt{@Test(expected=DBConnectException.class)}
\item Ignore a test: \texttt{@Ignore("Ignore the following test for now")}
\end{itemize}
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Junit 4 -- Annotations}
\begin{itemize}
\item \texttt{@Before} A method marked with \texttt{@Before} will be run before every test method. It is used to prepare the tests environment.
\item \texttt{@After}
\item \texttt{@Test} Identifies a test method.
\item \texttt{@Ignore} The method under test will be ignored (to disable some tests which we do not want to repeat)
\item \texttt{@AfterClass} Run only once after all test methods
\item \texttt{@BeforeClass}
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{JUnit 5}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Junit 5 -- Annotations}
\begin{itemize}
\item \texttt{@BeforeEach} A method marked with \texttt{@BeforeEach} will be run before every test method. It is used to prepare the tests environment.
\item \texttt{@AfterEach} It will be run after every test method.
\item \texttt{@Test} Identifies a test method.
\item \texttt{@Ignore} The method under test will be ignored (to disable some tests which we do not want to repeat)
\item \texttt{@AfterAll} Run only once after all test methods
\item \texttt{@BeforeAll} Run only once before all test methods
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Junit 5 -- Assert methods}
Assert methods are used to comparison between an expected value and an actual value to determine the success or failure of a test.
\begin{itemize}
\item \texttt{assertCONDITION(...)}
\item \texttt{assertCONDITION(String message,...)} - The message is displayed when the assert method fails
\end{itemize}
For example:
\begin{itemize}
\item \texttt{assertEquals(<> expected, <> actual)}
\item \texttt{assertSame(...)}
\item \texttt{assertFalse(boolean condition)}
\item \texttt{assertTrue(boolean condition)}
%\item \texttt{assertNull(...)}
%\item \texttt{assertNotNull(...)}
\item Etc.
\end{itemize}
Further information in the API: \texttt{org.junit.jupiter.api.Assertions}
%\url{https://junit.org/junit5/}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Junit 5 -- New functionality I}
\begin{itemize}
\item Custom display names - The \texttt{@DisplayName} annotation allows us to provide custom display names for test classes and methods, which can include spaces, special characters, and emojis.
\item Nested test classes - Non-static nested classes (i.e., inner classes) can serve as \texttt{@Nested} test classes for logical grouping of test cases.
\item Tagging and filtering - It is possible to tag test classes and test methods with your identifiers using the \texttt{@Tag} annotation.
\item Meta-annotations and composed annotations - This allows us to create a custom composed annotation that will inherit the semantics of its meta-annotations.
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}{Junit 5 -- New functionality II}
\begin{itemize}
\item Dynamic tests - JUnit Jupiter introduces dynamic tests which are generated at runtime by a factory method annotated with the \texttt{@TestFactory} annotation.
\item Repeated tests - Tests can be repeated a specified number of times by annotating the test method with the \texttt{@RepeatedTest} annotation.
\item Parameterized tests - A test can be run multiple times with different arguments by annotating the test method with the \texttt{@ParameterizedTest} annotation.
% and declaring at least one source to provide the arguments.
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{First Example}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{\texttt{ComplexNumber} Class -- Class Under Test (CUT)}
We will use the \texttt{ComplexNumber} class as example, which will later be used to calculate quadratic equations.
The structure of the \texttt{ComplexNumber} class is as follows:
\begin{center}
\includegraphics[width=75pt]{./figs/ComplexNumber}
\end{center}
The operations are as follows:
\begin{itemize}
\item Addition: $(a,b)+(c,d)=(a+c)+(b+d)i$
\item Multiplication: $(a,b)\cdot(c,d)=(ac-bd) + (ad+cb)i$
\item Equality: $(a,b)=(c,d) \Leftrightarrow a=c \wedge b=d$
\end{itemize}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{\texttt{ComplexNumber} class -- CUT I}
\begin{lstlisting}[language=Java,basicstyle=\scriptsize]
public class ComplexNumber {
private double fReal;
private double fImaginary;
public ComplexNumber(double re, double im) {
fReal = re;
fImaginary = im;
}
public double getReal() {
return fReal;
}
public double getImaginary() {
return fImaginary;
}
}
public ComplexNumber add(ComplexNumber c) {
return new ComplexNumber(getReal() + c.getReal(),
getImaginary() + c.getImaginary());
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{\texttt{ComplexNumber} class -- CUT II}
\begin{lstlisting}[language=Java,basicstyle=\scriptsize]
public ComplexNumber multiply(ComplexNumber c) {
double re = getReal() * c.getReal() - getImaginary() * c.getImaginary();
double im = getImaginary() * c.getReal() + getReal() * c.getImaginary();
return new ComplexNumber(re, im);
}
@Override
public boolean equals(Object anObject) {
if (anObject instanceof ComplexNumber) {
ComplexNumber c = (ComplexNumber) anObject;
return ((c.getReal() == getReal()) &&
(c.getImaginary() == getImaginary()));
} else
return false;
}
}
\end{lstlisting}
\end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % \subsection{JUnit in Eclipse}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % \begin{frame}{MoreUnit}
% %
% %MoreUnit makes unit testing in Eclipse a bit easier.
% %
% %To install MoreUnit in Eclipse, select Help \textgreater Eclipse Marketplace
% %
% % \begin{center}
% % \includegraphics[width=100pt]{./figs/market}
% % \end{center}
% %
% % \texttt{Next} and then \texttt{Finish} (restart Eclipse)
% %
% % More about MoreUnit: \texttt{https://moreunit.github.io/MoreUnit-Eclipse/ }
% %\end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %\begin{frame}
% %
% %Configure MoreUnit Source folder
% %
% % Import the JUnit library and create a source folder for the tests in each project which you want to have unit tests. Click \texttt{Window} \textgreater \texttt{Preferences}:
% %
% % \begin{center}
% % \includegraphics[width=205pt]{./figs/config}
% % \end{center}
% %
% % Set \texttt{Test} as Source Folder.
% %
% %\end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %\begin{frame}
% % Starting with MoreUnit. With a new Java Project:
% % \begin{enumerate}
% % \item Open the project properties
% % \item Click on \emph{Java Build Path}
% % \item Select the \emph{Libraries} tab
% % \item Click the \emph{Add Library} button
% % \item Choose JUnit
% % \end{enumerate}
% %\end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %\begin{frame}
% % In your Java Project, create a new source folder called 'test', in your folder right click and select \texttt{New} \textgreater \texttt{Source Folder}.
% %
% % \begin{center}
% % \includegraphics[width=150pt]{./figs/folder}
% % \end{center}
% %
% %\end{frame}
% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %\begin{frame}
% %In the class with the code you have to do right click and select MoreUnit menu \textgreater Jump to test/Member under test.
% %\begin{center}
% % \includegraphics[width=150pt]{./figs/Unitmenu}
% %\end{center}
% %
% %\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Fixtures}
Now you can view your test class in the test source folder. Extract from test class:
\begin{center}
\begin{lstlisting}[language=Java,basicstyle=\scriptsize]
public class ComplexNumberTest {
@BeforeClass
public static void setUpBeforeClass()
@AfterClass
public static void tearDownAfterClass()
@Before
public void setUp()
@After
public void tearDown()
...
}
\end{lstlisting}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile]
%We can add some attributes to test the methods:
%\begin{center}
%\begin{lstlisting}[language=Java,basicstyle=\tiny]
%public class ComplexNumberTest {
% private ComplexNumber cOneZero;
% private ComplexNumber cZeroOne;
% private ComplexNumber cOneOne;
%
% @BeforeClass
% public static void setUpBeforeClass() throws Exception {
% }
% @AfterClass
% public static void tearDownAfterClass() throws Exception {
% }
% @Before
% public void setUp() throws Exception {
% }
% @After
% public void tearDown() throws Exception {
% }
%}
%\end{lstlisting}
%\end{center}
%\end{frame}
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
We can write some tests that will start with the annotation \texttt{@Before} for the \texttt{setUp()} method as follows.
\begin{lstlisting}[language=Java,basicstyle=\tiny]
@Before
public void setUp() throws Exception {
ComplexNumber cOneZero = new ComplexNumber(1, 0);
ComplexNumber cZeroOne = new ComplexNumber(0, 1);
ComplexNumber cOneOne = new ComplexNumber(1, 1);
}
\end{lstlisting}
This method is executed before each test case is run. It is typically used to create class instances.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
We can destroy all previously created classes with the annotation \texttt{@After} before a method, usually named \texttt{tearDown()}.
\begin{lstlisting}[language=Java,basicstyle=\tiny]
@After
public void tearDown() throws Exception {
cOneZero = null;
cZeroOne = null;
cOneOne = null;
}
\end{lstlisting}
This method destroys the classes before each test is executed.
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
Now we can write unit tests:
~
\begin{lstlisting}[language=Java,basicstyle=\tiny]
@Test
public void testGetReal() {
System.out.println("getReal");
double expResult = 0.0;
double result = cZeroOne.getReal();
assertEquals(expResult, result);
}
@Test
public void testAdd() {
System.out.println("add");
ComplexNumber result = cZeroOne.add(cOneZero);
assertEquals(cOneOne, result);
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]
\begin{lstlisting}[language=Java,basicstyle=\tiny]
@Test
public void testMultiply() {
System.out.println("multiply");
ComplexNumber result = cZeroOne.multiply(cOneZero);
assertEquals(cZeroOne, result);
}
@Test
public void testEquals() {
System.out.println("equals");
boolean expResult = false;
boolean result = cZeroOne.equals(cOneZero);
assertEquals(expResult, result);
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}
We usually run the test with IDEs and we will see something similar to the following:
\begin{center}
\includegraphics[width=100pt]{./figs/Results}
\end{center}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Advanced Topics}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Testing Suites}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Suites}
Run different sets of unit tests
Using \texttt{@Suite} annotation
\begin{lstlisting}[language=Java,basicstyle=\scriptsize]
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
@SelectPackages({"uah.packageA"
,"uah.packageB"})
@IncludeTags("production")
@Suite
public class JUnit5TestSuiteExample {
}
\end{lstlisting}
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Testing Exceptions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile]{Exceptions}
The coefficient $a$ must not be zero. Therefore, the following code:
\texttt{instance = new QuadraticEquation(0,1,1);}
will throw an exception when executing:
\texttt{boolean result = instance.checkCoefficientsAreOk();}
This is tested with the annotation \texttt{@Test(expected=CoefficientsException.class)}.
\begin{lstlisting}[language=JAVA,basicstyle=\scriptsize]
@Test(expected=CoefficientsException.class)
public void testCheckCoefficientsAreOkException() throws CoefficientsException {
System.out.println("checkCoefficientsAreOk with Exception raised");