Você está na página 1de 46

Automação de Testes de Software com JUnit

Prof. Evandro César Freiberger

Instituto Federal de Mato Grosso


Departamento da Área de Informática
evandro.freiberger@ifmt.edu.br

2022

Prof. Evandro César Freiberger (IFMT) Testes 2022 1 / 46


Sumário

1 Introdução ao Framework JUnit

2 Detalhamento do JUnit

Prof. Evandro César Freiberger (IFMT) Testes 2022 2 / 46


Framework JUnit
O JUnit é um framework open-source, criado por Erich Gamma e Kent Beck, com suporte
à criação de testes automatizados na linguagem de programação Java.

Projetado inicialmente para a automação de Testes de Unidade.

Esse framework facilita a criação e manutenção do código para a automação de testes


com apresentação dos resultados.

Atualmente, com apoio de outras APIs e Framework, pode ser usado para outros níveis
de testes, como teste de integração e testes de sistema.

O JUnit permite a realização de testes de unidades, conhecidos como "caixa branca",


facilitando assim a correção de métodos e objetos.

Prof. Evandro César Freiberger (IFMT) Testes 2022 3 / 46


Introdução a Um Cenário de Teste
Crie um Projeto Maven (maven-archetype-quickstar), com os seguintes
dados:
groupId = ifmt.cba

artifactId = junit01

version = 1.0-SNAPSHOT

pasta do projeto: JUnit01

Prof. Evandro César Freiberger (IFMT) Testes 2022 4 / 46


Arquivo pom.xml do Projeto Maven (1)
1 <?xml v e r s i o n = " 1 . 0 " encoding= "UTF−8 " ?>
2
3 < p r o j e c t xmlns= " h t t p : / / maven . apache . org /POM/ 4 . 0 . 0 " x m l n s : x s i = " h t t p : / / www. w3 . org / 2 0 0 1 /XMLSchema− i n s t a n c e "
4 x s i : s c h e m a L o c a t i o n = " h t t p : / / maven . apache . org /POM/ 4 . 0 . 0 h t t p : / / maven . apache . org / xsd / maven − 4 . 0 . 0 . xsd " >
5 <modelVersion> 4 . 0 . 0 < / modelVersion>
6
7 < g r o u p I d > i f m t . cba< / g r o u p I d >
8 <artifactId >junit01</ artifactId >
9 < v e r s i o n >1.0 −SNAPSHOT< / v e r s i o n >
10
11 <name> j u n i t 0 1 < / name>
12 < ! −− FIXME change i t t o t h e p r o j e c t ’ s w e b s i t e −−>
13 < u r l > h t t p : / / www. example . com< / u r l >
14
15 <properties >
16 < p r o j e c t . b u i l d . sourceEncoding >UTF−8 </ p r o j e c t . b u i l d . sourceEncoding >
17 <maven . c o m p i l e r . source >1.7 </maven . c o m p i l e r . source >
18 <maven . c o m p i l e r . t a r g e t >1.7 </maven . c o m p i l e r . t a r g e t >
19 </ p r o p e r t i e s >
20
21 <dependencies >
22 <dependency>
23 <groupId > j u n i t < / groupId >
24 < a r t i f a c t I d > j u n i t </ a r t i f a c t I d >
25 < v e r s i o n >4.11 </ v e r s i o n >
26 <scope > t e s t < / scope >
27 </ dependency>
28 </ dependencies >
29
30 <build >
31 <pluginManagement ><!−− l o c k down p l u g i n s v e r s i o n s t o a v o i d u s i n g Maven d e f a u l t s ( may be moved t o p a r e n t pom) −−>

Prof. Evandro César Freiberger (IFMT) Testes 2022 5 / 46


Arquivo pom.xml do Projeto Maven (2)
32 <plugins >
33 <!−− c l e a n l i f e c y c l e , see h t t p s : / / maven . apache . org / r e f / c u r r e n t / maven−core / l i f e c y c l e s . h t m l # c l e a n _ L i f e c y c l e −−>
34 <plugin >
35 < a r t i f a c t I d >maven−clean − p l u g i n < / a r t i f a c t I d >
36 <version >3.1.0 </ version >
37 </ p l u g i n >
38 <!−− d e f a u l t l i f e c y c l e , j a r p a c k a g i n g : see h t t p s : / / maven . apache . org / r e f / c u r r e n t / maven−core / d e f a u l t − b i n d i n g s . h t m l #
P l u g i n _ b i n d i n g s _ f o r _ j a r _ p a c k a g i n g −−>
39 <plugin >
40 < a r t i f a c t I d >maven−resources − p l u g i n < / a r t i f a c t I d >
41 <version >3.0.2 </ version >
42 </ p l u g i n >
43 <plugin >
44 < a r t i f a c t I d >maven− c o m p i l e r − p l u g i n < / a r t i f a c t I d >
45 <version >3.8.0 </ version >
46 </ p l u g i n >
47 <plugin >
48 < a r t i f a c t I d >maven− s u r e f i r e − p l u g i n < / a r t i f a c t I d >
49 <version >2.22.1 </ version >
50 </ p l u g i n >
51 <plugin >
52 < a r t i f a c t I d >maven− j a r − p l u g i n < / a r t i f a c t I d >
53 <version >3.0.2 </ version >
54 </ p l u g i n >
55 <plugin >
56 < a r t i f a c t I d >maven− i n s t a l l − p l u g i n < / a r t i f a c t I d >
57 <version >2.5.2 </ version >
58 </ p l u g i n >
59 <plugin >
60 < a r t i f a c t I d >maven−deploy − p l u g i n < / a r t i f a c t I d >
61 <version >2.8.2 </ version >
62 </ p l u g i n >

Prof. Evandro César Freiberger (IFMT) Testes 2022 6 / 46


Arquivo pom.xml do Projeto Maven (3)
63 <!−− s i t e l i f e c y c l e , see h t t p s : / / maven . apache . org / r e f / c u r r e n t / maven−core / l i f e c y c l e s . h t m l # s i t e _ L i f e c y c l e −−>
64 <plugin >
65 < a r t i f a c t I d >maven− s i t e − p l u g i n < / a r t i f a c t I d >
66 <version >3.7.1 </ version >
67 </ p l u g i n >
68 <plugin >
69 < a r t i f a c t I d >maven− p r o j e c t − i n f o − r e p o r t s − p l u g i n < / a r t i f a c t I d >
70 <version >3.0.0 </ version >
71 </ p l u g i n >
72 </ p l u g i n s >
73 </ pluginManagement >
74 </ b u i l d >
75 </ p r o j e c t >

Prof. Evandro César Freiberger (IFMT) Testes 2022 7 / 46


Classe NotaAluno - Versão 1 (1)
Codifique a classe NotaAluno com o nome NotaAluno1.java, conforme código fornecido.
1 package i f m t . cba ;
2
3 import java . u t i l . L i s t ;
4
5 p u b l i c c l a s s NotaAluno1 {
6 p r i v a t e List <Float > l i s t a N o t a ;
7 p r i v a t e f l o a t mediaNotas ;
8 p r i v a t e f l o a t maiorNota ;
9 p r i v a t e f l o a t menorNota ;
10
11 p u b l i c NotaAluno1 ( ) {
12 t h i s . mediaNotas = 0 ;
13 t h i s . maiorNota = F l o a t . MAX_VALUE;
14 t h i s . menorNota = F l o a t . MIN_VALUE ;
15 }
16 public List <Float > getListaNota ( ) {
17 return listaNota ;
18 }
19
20 p u b l i c v o i d addNota ( f l o a t nota ) {
21 t h i s . l i s t a N o t a . add ( nota ) ;
22 }
23
24 p u b l i c v o i d processarNotas ( ) {
25 t h i s . maiorNota = F l o a t . MIN_VALUE ;
26 t h i s . menorNota = F l o a t . MAX_VALUE;
27 f l o a t somaNotas = 0 ;
28
29 f o r ( f l o a t nota : t h i s . l i s t a N o t a ) {

Prof. Evandro César Freiberger (IFMT) Testes 2022 8 / 46


Classe NotaAluno - Versão 1 (2)
30 somaNotas += nota ;
31 i f ( nota > maiorNota ) {
32 t h i s . maiorNota = nota ;
33 } else {
34 t h i s . menorNota = nota ;
35 }
36 }
37 t h i s . mediaNotas = somaNotas / t h i s . l i s t a N o t a . s i z e ( ) ;
38 }
39
40 p u b l i c f l o a t getMediaNotas ( ) {
41 r e t u r n mediaNotas ;
42 }
43
44 p u b l i c f l o a t getMaiorNota ( ) {
45 r e t u r n maiorNota ;
46 }
47
48 p u b l i c f l o a t getMenorNota ( ) {
49 r e t u r n menorNota ;
50 }
51 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 9 / 46


Classe TesteNotas - Versão 1
Para testarmos se uma classe executa corretamente suas ações, podemos criar uma classe
auxiliar, que instancia e manipula objetos da classe a ser testada, com o objetivo de avaliar o
comportamento e resultados obtidos.

Codifique a classe com o nome TesteNotas1.java, conforme código fornecido.


1 package i f m t . cba ;
2
3 p u b l i c c l a s s TesteNotas1 {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
5
6 NotaAluno1 notas = new NotaAluno1 ( ) ;
7
8 notas . addNota ( 8 ) ;
9 notas . addNota ( 7 ) ;
10 notas . addNota ( 6 ) ;
11
12 notas . processarNotas ( ) ;
13
14 System . o u t . p r i n t l n ( " Media = " + notas . getMediaNotas ( ) ) ;
15 System . o u t . p r i n t l n ( " Maior nota = " + notas . getMaiorNota ( ) ) ;
16 System . o u t . p r i n t l n ( " Menor nota = " + notas . getMenorNota ( ) ) ;
17
18 }
19 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 10 / 46


Executando a Classe TesteNotas - Versão 1
Com a execução da classe TesteNotas1, observa-se uma exceção, cuja mensagem é:

Exception in thread "main"java.lang.NullPointerException: Cannot invoke


"java.util.List.add(Object)"because "this.listaNota"is null at
ifmt.cba.NotaAluno1.addNota(NotaAluno1.java:21) at
ifmt.cba.TesteNotas1.main(TesteNotas1.java:8)

NullPointerException causado com a execução da linha 21 da classe NotaAluno1.java e


linha 8 da classe TesteNotas1

Causa: faltou iniciar o objeto da lista de notas

Prof. Evandro César Freiberger (IFMT) Testes 2022 11 / 46


Classe NotaAluno - Versão 2 (1)
Codifique a classe NotaAluno com o nome NotaAluno2.java, conforme código fornecido.

Correção do erro de não inicialização do objeto listaNota.


1 package i f m t . cba ;
2
3 import java . u t i l . ArrayList ;
4 import java . u t i l . L i s t ;
5
6 p u b l i c c l a s s NotaAluno2 {
7 p r i v a t e List <Float > l i s t a N o t a ;
8 p r i v a t e f l o a t mediaNotas ;
9 p r i v a t e f l o a t maiorNota ;
10 p r i v a t e f l o a t menorNota ;
11
12 p u b l i c NotaAluno2 ( ) {
13 t h i s . l i s t a N o t a = new A r r a y L i s t < F l o a t > ( ) ;
14 t h i s . mediaNotas = 0 ;
15 t h i s . maiorNota = F l o a t . MAX_VALUE;
16 t h i s . menorNota = F l o a t . MIN_VALUE ;
17 }
18 public List <Float > getListaNota ( ) {
19 return listaNota ;
20 }
21
22 p u b l i c v o i d addNota ( f l o a t nota ) {
23 t h i s . l i s t a N o t a . add ( nota ) ;
24 }
25
26 p u b l i c v o i d processarNotas ( ) {

Prof. Evandro César Freiberger (IFMT) Testes 2022 12 / 46


Classe NotaAluno - Versão 2 (2)
27 t h i s . maiorNota = F l o a t . MIN_VALUE ;
28 t h i s . menorNota = F l o a t . MAX_VALUE;
29 f l o a t somaNotas = 0 ;
30
31 f o r ( f l o a t nota : t h i s . l i s t a N o t a ) {
32 somaNotas += nota ;
33 i f ( nota > maiorNota ) {
34 t h i s . maiorNota = nota ;
35 } else {
36 t h i s . menorNota = nota ;
37 }
38 }
39 t h i s . mediaNotas = somaNotas / t h i s . l i s t a N o t a . s i z e ( ) ;
40 }
41
42 p u b l i c f l o a t getMediaNotas ( ) {
43 r e t u r n mediaNotas ;
44 }
45
46 p u b l i c f l o a t getMaiorNota ( ) {
47 r e t u r n maiorNota ;
48 }
49
50 p u b l i c f l o a t getMenorNota ( ) {
51 r e t u r n menorNota ;
52 }
53 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 13 / 46


Classe TesteNotas - Versão 2
Codifique a classe com o nome TesteNotas2.java, conforme código fornecido.
1 package i f m t . cba ;
2
3 p u b l i c c l a s s TesteNotas2 {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
5
6 NotaAluno2 notas = new NotaAluno2 ( ) ;
7
8 notas . addNota ( 8 ) ;
9 notas . addNota ( 7 ) ;
10 notas . addNota ( 6 ) ;
11
12 notas . processarNotas ( ) ;
13
14 System . o u t . p r i n t l n ( " Media = " + notas . getMediaNotas ( ) ) ;
15 System . o u t . p r i n t l n ( " Maior nota = " + notas . getMaiorNota ( ) ) ;
16 System . o u t . p r i n t l n ( " Menor nota = " + notas . getMenorNota ( ) ) ;
17
18 }
19 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 14 / 46


Executando a Classe TesteNotas - Versão 2
Com a execução da classe TesteNotas2, observa-se o seguinte resultado:

Media = 7.0
Maior nota = 8.0
Menor nota = 6.0

Classe testada e aprovada!!!

Contudo, um teste nesse estilo exige que o Testador conheça os resultados corretos e
verifique todos os resultados de todas as classes testadas no sistema. Imagine 200 classes
sendo testadas?

Prof. Evandro César Freiberger (IFMT) Testes 2022 15 / 46


Classe NotaAluno - Versão 3 (1)
Codifique a classe NotaAluno com o nome NotaAluno3.java, conforme código fornecido.

Obs: mesma implementação da versão NotaAluno2.java.


1 package i f m t . cba ;
2
3 import java . u t i l . ArrayList ;
4 import java . u t i l . L i s t ;
5
6 p u b l i c c l a s s NotaAluno3 {
7 p r i v a t e List <Float > l i s t a N o t a ;
8 p r i v a t e f l o a t mediaNotas ;
9 p r i v a t e f l o a t maiorNota ;
10 p r i v a t e f l o a t menorNota ;
11
12 p u b l i c NotaAluno3 ( ) {
13 t h i s . l i s t a N o t a = new A r r a y L i s t < F l o a t > ( ) ;
14 t h i s . mediaNotas = 0 ;
15 t h i s . maiorNota = F l o a t . MAX_VALUE;
16 t h i s . menorNota = F l o a t . MIN_VALUE ;
17 }
18 public List <Float > getListaNota ( ) {
19 return listaNota ;
20 }
21
22 p u b l i c v o i d addNota ( f l o a t nota ) {
23 t h i s . l i s t a N o t a . add ( nota ) ;
24 }
25
26 p u b l i c v o i d processarNotas ( ) {

Prof. Evandro César Freiberger (IFMT) Testes 2022 16 / 46


Classe NotaAluno - Versão 3 (2)
27 t h i s . maiorNota = F l o a t . MIN_VALUE ;
28 t h i s . menorNota = F l o a t . MAX_VALUE;
29 f l o a t somaNotas = 0 ;
30
31 f o r ( f l o a t nota : t h i s . l i s t a N o t a ) {
32 somaNotas += nota ;
33 i f ( nota > maiorNota ) {
34 t h i s . maiorNota = nota ;
35 } else {
36 t h i s . menorNota = nota ;
37 }
38 }
39 t h i s . mediaNotas = somaNotas / t h i s . l i s t a N o t a . s i z e ( ) ;
40 }
41
42 p u b l i c f l o a t getMediaNotas ( ) {
43 r e t u r n mediaNotas ;
44 }
45
46 p u b l i c f l o a t getMaiorNota ( ) {
47 r e t u r n maiorNota ;
48 }
49
50 p u b l i c f l o a t getMenorNota ( ) {
51 r e t u r n menorNota ;
52 }
53 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 17 / 46


Classe TesteNotas - Versão 3
Codifique a classe com o nome TesteNotas3.java, conforme código fornecido.

O teste é criado com a compação dos valores esperados versus valores obtidos.

Isso não exige que o Testado conheça os resultados corretos.

Contudo, ainda precisa analisar o log/saída dos testes de forma individualizada para verificar
se algum dele não retornou o valor esperado.
1 package i f m t . cba ;
2
3 p u b l i c c l a s s TesteNotas3 {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] args ) {
5
6 NotaAluno3 notas = new NotaAluno3 ( ) ;
7
8 notas . addNota ( 8 ) ;
9 notas . addNota ( 7 ) ;
10 notas . addNota ( 6 ) ;
11
12 notas . processarNotas ( ) ;
13
14 System . o u t . p r i n t l n ( " Media e s t a c o r r e t a ? " + ( 7 . 0 f == notas . getMediaNotas ( ) ) ) ;
15 System . o u t . p r i n t l n ( " Maior nota e s t a c o r r e t a ? " + ( 8 . 0 f == notas . getMaiorNota ( ) ) ) ;
16 System . o u t . p r i n t l n ( " Menor nota e s t a c o r r e t a ? " + ( 6 . 0 f == notas . getMenorNota ( ) ) ) ;
17 }
18 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 18 / 46


Automatizando Testes com JUnit (1)
Abra a classe NotaAluno3, clique com o botão direito do mouse em qualquer parte do código
e escolha a opção Source Action.

Em seguida escolha a opção Generate Tests e complemente o nome da classe de testes


com: ifmt.cba.NotaAluno3AutomaticoTest

Por último, selecione todos os métodos na lista apresentada e clique em Ok ou Enter.


1 package i f m t . cba ;
2
3 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
4
5 import java . u t i l . L i s t ;
6
7 i m p o r t org . j u n i t . Test ;
8
9 p u b l i c c l a s s NotaAluno3AutomaticoTest {
10
11 @Test
12 p u b l i c v o i d testAddNota ( ) {
13
14 NotaAluno3 nota = new NotaAluno3 ( ) ;
15 a s s e r t E q u a l s ( 0 , nota . g e t L i s t a N o t a ( ) . s i z e ( ) ) ;
16 nota . addNota ( 9 ) ;
17 nota . addNota ( 8 ) ;
18 a s s e r t E q u a l s ( 2 , nota . g e t L i s t a N o t a ( ) . s i z e ( ) ) ;

Prof. Evandro César Freiberger (IFMT) Testes 2022 19 / 46


Automatizando Testes com JUnit (2)
19 }
20
21 @Test
22 public void testGetListaNota ( ) {
23 NotaAluno3 nota = new NotaAluno3 ( ) ;
24 nota . addNota ( 9 ) ;
25 nota . addNota ( 8 ) ;
26 L i s t < F l o a t > l i s t a = nota . g e t L i s t a N o t a ( ) ;
27
28 a s s e r t E q u a l s ( 9 , l i s t a . g e t ( 0 ) , 001) ;
29 a s s e r t E q u a l s ( 8 , l i s t a . g e t ( 1 ) , 001) ;
30 }
31
32 @Test
33 p u b l i c void testGetMaiorNota ( ) {
34 NotaAluno3 nota = new NotaAluno3 ( ) ;
35 nota . addNota ( 9 ) ;
36 nota . addNota ( 8 ) ;
37 nota . processarNotas ( ) ;
38 a s s e r t E q u a l s ( 9 , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
39 }
40
41 @Test
42 p u b l i c v o i d testGetMediaNotas ( ) {
43 NotaAluno3 nota = new NotaAluno3 ( ) ;
44 nota . addNota ( 9 ) ;
45 nota . addNota ( 8 ) ;
46 nota . processarNotas ( ) ;
47 a s s e r t E q u a l s ( 8 . 5 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
48 }
49
50 @Test

Prof. Evandro César Freiberger (IFMT) Testes 2022 20 / 46


Automatizando Testes com JUnit (3)
51 p u b l i c v o i d testGetMenorNota ( ) {
52 NotaAluno3 nota = new NotaAluno3 ( ) ;
53 nota . addNota ( 9 ) ;
54 nota . addNota ( 8 ) ;
55 nota . processarNotas ( ) ;
56 a s s e r t E q u a l s ( 8 , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
57 }
58
59 @Test
60 p ublic void testProcessarNotas ( ) {
61 NotaAluno3 nota = new NotaAluno3 ( ) ;
62 nota . addNota ( 9 ) ;
63 nota . addNota ( 8 ) ;
64 nota . processarNotas ( ) ;
65 a s s e r t E q u a l s ( 8 , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
66 a s s e r t E q u a l s ( 9 , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
67 a s s e r t E q u a l s ( 8 . 5 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
68 }
69 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 21 / 46


Automatizando Testes com JUnit (1)
Repita a operação de criar um Test para a classe NotaAluno3, mantenha o nome sugerido
para a classe de teste e não selecione nenhum método.

Codifique conforme código fornecido.


1 package i f m t . cba ;
2
3 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
4
5 i m p o r t org . j u n i t . Test ;
6
7 p u b l i c c l a s s NotaAluno3Test {
8
9 @Test
10 p u b l i c v o i d quandoNaoAdicionaNotas ( ) {
11 NotaAluno3 nota = new NotaAluno3 ( ) ;
12 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
13 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
14 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
15 }
16
17 @Test
18 p u b l i c v o i d processarSemNotas ( ) {
19 NotaAluno3 nota = new NotaAluno3 ( ) ;
20 nota . processarNotas ( ) ;
21 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
22 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
23 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
24 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 22 / 46


Automatizando Testes com JUnit (2)
25 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 23 / 46


Automatizando Testes com JUnit (1)
Codifique a classe NotaAluno4 conforme código fornecido.
1 package i f m t . cba ;
2
3 import java . u t i l . ArrayList ;
4 import java . u t i l . L i s t ;
5
6 p u b l i c c l a s s NotaAluno4 {
7 p r i v a t e List <Float > l i s t a N o t a ;
8 p r i v a t e f l o a t mediaNotas ;
9 p r i v a t e f l o a t maiorNota ;
10 p r i v a t e f l o a t menorNota ;
11
12 p u b l i c NotaAluno4 ( ) {
13 t h i s . l i s t a N o t a = new A r r a y L i s t < F l o a t > ( ) ;
14 t h i s . mediaNotas = 0 ;
15 t h i s . maiorNota = F l o a t . MAX_VALUE;
16 t h i s . menorNota = F l o a t . MIN_VALUE ;
17 }
18
19 public List <Float > getListaNota ( ) {
20 return listaNota ;
21 }
22
23 p u b l i c v o i d addNota ( f l o a t nota ) {
24 t h i s . l i s t a N o t a . add ( nota ) ;
25 }
26
27 p u b l i c v o i d processarNotas ( ) {
28 t h i s . maiorNota = F l o a t . MIN_VALUE ;
29 t h i s . menorNota = F l o a t . MAX_VALUE;

Prof. Evandro César Freiberger (IFMT) Testes 2022 24 / 46


Automatizando Testes com JUnit (2)
30 f l o a t somaNotas = 0 ;
31
32 f o r ( f l o a t nota : t h i s . l i s t a N o t a ) {
33 somaNotas += nota ;
34 i f ( nota > maiorNota ) {
35 t h i s . maiorNota = nota ;
36 } else {
37 t h i s . menorNota = nota ;
38 }
39 }
40 i f ( t h i s . l i s t a N o t a . size ( ) > 0) {
41 t h i s . mediaNotas = somaNotas / t h i s . l i s t a N o t a . s i z e ( ) ;
42 }
43 }
44
45 p u b l i c f l o a t getMediaNotas ( ) {
46 r e t u r n mediaNotas ;
47 }
48
49 p u b l i c f l o a t getMaiorNota ( ) {
50 r e t u r n maiorNota ;
51 }
52
53 p u b l i c f l o a t getMenorNota ( ) {
54 r e t u r n menorNota ;
55 }
56 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 25 / 46


Automatizando Testes com JUnit (1)
Produza um Test para a classe NotaAluno4, mantenha o nome sugerido para a classe de
teste e não selecione nenhum método.

Codifique conforme código fornecido.

O teste falha quando as notas são inseridas em ordem crescente na lista.


1 package i f m t . cba ;
2
3
4 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
5
6 i m p o r t org . j u n i t . Test ;
7
8 p u b l i c c l a s s NotaAluno4Test {
9 @Test
10 p u b l i c v o i d quandoNaoAdicionaNotas ( ) {
11 NotaAluno4 nota = new NotaAluno4 ( ) ;
12 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
13 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
14 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
15 }
16
17 @Test
18 p u b l i c v o i d processarSemNotas ( ) {
19 NotaAluno4 nota = new NotaAluno4 ( ) ;
20 nota . processarNotas ( ) ;

Prof. Evandro César Freiberger (IFMT) Testes 2022 26 / 46


Automatizando Testes com JUnit (2)
21 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
22 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
23 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
24 }
25
26 @Test
27 p u b l i c v o i d processarComNotasDecrescente ( ) {
28 NotaAluno4 nota = new NotaAluno4 ( ) ;
29 nota . addNota ( 8 . 0 f ) ;
30 nota . addNota ( 7 . 0 f ) ;
31 nota . addNota ( 6 . 0 f ) ;
32 nota . processarNotas ( ) ;
33 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
34 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
35 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
36 }
37
38 @Test
39 p u b l i c v o i d processarComNotasCrescente ( ) {
40 NotaAluno4 nota = new NotaAluno4 ( ) ;
41 nota . addNota ( 6 . 0 f ) ;
42 nota . addNota ( 7 . 0 f ) ;
43 nota . addNota ( 8 . 0 f ) ;
44 nota . processarNotas ( ) ;
45 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
46 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
47 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
48 }
49 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 27 / 46


Automatizando Testes com JUnit (1)
Codifique a classe NotaAluno5 conforme código fornecido.
1 package i f m t . cba ;
2
3 import java . u t i l . ArrayList ;
4 import java . u t i l . L i s t ;
5
6 p u b l i c c l a s s NotaAluno5 {
7 p r i v a t e List <Float > l i s t a N o t a ;
8 p r i v a t e f l o a t mediaNotas ;
9 p r i v a t e f l o a t maiorNota ;
10 p r i v a t e f l o a t menorNota ;
11
12 p u b l i c NotaAluno5 ( ) {
13 t h i s . l i s t a N o t a = new A r r a y L i s t < F l o a t > ( ) ;
14 t h i s . mediaNotas = 0 ;
15 t h i s . maiorNota = F l o a t . MAX_VALUE;
16 t h i s . menorNota = F l o a t . MIN_VALUE ;
17 }
18
19 public List <Float > getListaNota ( ) {
20 return listaNota ;
21 }
22
23 p u b l i c v o i d addNota ( f l o a t nota ) {
24 t h i s . l i s t a N o t a . add ( nota ) ;
25 }
26
27 p u b l i c v o i d processarNotas ( ) {
28 t h i s . maiorNota = F l o a t . MIN_VALUE ;
29 t h i s . menorNota = F l o a t . MAX_VALUE;

Prof. Evandro César Freiberger (IFMT) Testes 2022 28 / 46


Automatizando Testes com JUnit (2)
30 f l o a t somaNotas = 0 ;
31
32 f o r ( f l o a t nota : t h i s . l i s t a N o t a ) {
33 somaNotas += nota ;
34 i f ( nota > maiorNota ) {
35 t h i s . maiorNota = nota ;
36 }
37
38 i f ( nota < t h i s . menorNota ) {
39 t h i s . menorNota = nota ;
40 }
41 }
42 i f ( t h i s . l i s t a N o t a . size ( ) > 0) {
43 t h i s . mediaNotas = somaNotas / t h i s . l i s t a N o t a . s i z e ( ) ;
44 }
45 }
46
47 p u b l i c f l o a t getMediaNotas ( ) {
48 r e t u r n mediaNotas ;
49 }
50
51 p u b l i c f l o a t getMaiorNota ( ) {
52 r e t u r n maiorNota ;
53 }
54
55 p u b l i c f l o a t getMenorNota ( ) {
56 r e t u r n menorNota ;
57 }
58 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 29 / 46


Automatizando Testes com JUnit (1)
Produza um Test para a classe NotaAluno5, mantenha o nome sugerido para a classe de
teste e não selecione nenhum método.

Codifique conforme código fornecido.

O teste passa!
1 package i f m t . cba ;
2
3
4 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
5
6 i m p o r t org . j u n i t . Test ;
7
8 p u b l i c c l a s s NotaAluno5Test {
9 @Test
10 p u b l i c v o i d quandoNaoAdicionaNotas ( ) {
11 NotaAluno5 nota = new NotaAluno5 ( ) ;
12 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
13 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
14 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
15 }
16
17 @Test
18 p u b l i c v o i d processarSemNotas ( ) {
19 NotaAluno5 nota = new NotaAluno5 ( ) ;
20 nota . processarNotas ( ) ;

Prof. Evandro César Freiberger (IFMT) Testes 2022 30 / 46


Automatizando Testes com JUnit (2)
21 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
22 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
23 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
24 }
25
26 @Test
27 p u b l i c v o i d processarComNotasDecrescente ( ) {
28 NotaAluno5 nota = new NotaAluno5 ( ) ;
29 nota . addNota ( 8 . 0 f ) ;
30 nota . addNota ( 7 . 0 f ) ;
31 nota . addNota ( 6 . 0 f ) ;
32 nota . processarNotas ( ) ;
33 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
34 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
35 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
36 }
37
38 @Test
39 p u b l i c v o i d processarComNotasCrescente ( ) {
40 NotaAluno5 nota = new NotaAluno5 ( ) ;
41 nota . addNota ( 6 . 0 f ) ;
42 nota . addNota ( 7 . 0 f ) ;
43 nota . addNota ( 8 . 0 f ) ;
44 nota . processarNotas ( ) ;
45 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
46 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
47 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
48 }
49 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 31 / 46


Forma de Importação do Assert
Importação da classe Assert
1 package i f m t . cba ;
2
3 i m p o r t org . j u n i t . A s s e r t ;
4 i m p o r t org . j u n i t . Test ;
5
6 p u b l i c c l a s s ExemploImportacao1Test {
7
8 @Test
9 public void testeImportacaoClasseAssert ( ) {
10 Assert . assertTrue ( true ) ;
11 }
12 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 32 / 46


Forma de Importação do Assert
Importação estática de cada método estático da classe Assert
1 package i f m t . cba ;
2
3 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
4 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t T r u e ;
5
6 i m p o r t org . j u n i t . Test ;
7
8 p u b l i c c l a s s ExemploImportacao2Test {
9
10 @Test
11 public void testeImportacaoMetodoEstaticoAssert ( ) {
12 assertTrue ( true ) ;
13 }
14
15 @Test
16 public void testeImportacaoMetodoEstaticoAssert2 ( ) {
17 assertEquals (2 , 2) ;
18 }
19 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 33 / 46


Anotação @Before @After (1)
@Before - Um método anotado com essa anotação irá ser executado uma vez antes de cada
método de teste. É util quando um determinado recurso deve ser iniciado antes de cada teste.

@After - Um método anotado com essa anotação irá ser executado uma vez depois de cada
método de teste. É util para liberar recursos alocados antes dos testes.
1 package i f m t . cba ;
2
3 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
4
5 i m p o r t org . j u n i t . A f t e r ;
6 i m p o r t org . j u n i t . Before ;
7 i m p o r t org . j u n i t . Test ;
8
9 p u b l i c c l a s s Exe mpl oBef ore Aft erT est {
10
11 p r i v a t e i n t cont ;
12
13 @Before
14 p u b l i c v o i d beforeCont ( ) {
15 t h i s . cont = 1;
16 System . o u t . p r i n t l n ( " Before " + t h i s . c o n t ) ;
17 }
18
19 @After
20 public void afterCont ( ) {
21 System . o u t . p r i n t l n ( " A f t e r " + t h i s . c o n t ) ;
22 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 34 / 46


Anotação @Before @After (2)
23
24 @Test
25 p u b l i c v o i d caso1Test ( ) {
26 t h i s . c o n t += 1 ;
27 assertEquals (2 , t h i s . cont ) ;
28 }
29
30 @Test
31 p u b l i c v o i d caso2Test ( ) {
32 t h i s . c o n t += 2 ;
33 assertEquals (3 , t h i s . cont ) ;
34 }
35
36 @Test
37 p u b l i c v o i d caso3Test ( ) {
38 t h i s . c o n t += 3 ;
39 assertEquals (4 , t h i s . cont ) ;
40 }
41 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 35 / 46


Anotação @BeforeClass @AfterClass (1)
@BeforeClass - Um método anotado com essa anotação irá ser executado uma vez antes de
todos os testes de uma classe

@AfterClass - Um método anotado com essa anotação irá ser executado uma vez depois
que todos os testes de uma classe forem executados
1 package i f m t . cba ;
2
3 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
4
5 import org . junit . After ;
6 import org . junit . AfterClass ;
7 import org . junit . Before ;
8 import org . junit . BeforeClass ;
9 import org . junit . Test ;
10
11 p u b l i c c l a s s Exemp loBef oreC lassA fter Class Test {
12
13 p r i v a t e s t a t i c i n t cont ;
14
15 @BeforeClass
16 p u b l i c s t a t i c v o i d beforeClassCont ( ) {
17 cont = 1;
18 System . o u t . p r i n t l n ( " BeforeClass " + c o n t ) ;
19 }
20
21 @Before
22 p u b l i c v o i d beforeCont ( ) {

Prof. Evandro César Freiberger (IFMT) Testes 2022 36 / 46


Anotação @BeforeClass @AfterClass (2)
23 c o n t ++;
24 System . o u t . p r i n t l n ( " Before " + c o n t ) ;
25 }
26
27 @AfterClass
28 public s t a t i c void afterClassCont ( ) {
29 System . o u t . p r i n t l n ( " A f t e r C l a s s " + c o n t ) ;
30 }
31
32 @After
33 public void afterCont ( ) {
34 System . o u t . p r i n t l n ( " A f t e r " + c o n t ) ;
35 }
36
37 @Test
38 p u b l i c v o i d caso1Test ( ) {
39 c o n t += 1 ;
40 assertEquals (3 , cont ) ;
41 }
42
43 @Test
44 p u b l i c v o i d caso2Test ( ) {
45 c o n t += 2 ;
46 assertEquals (6 , cont ) ;
47 }
48
49 @Test
50 p u b l i c v o i d caso3Test ( ) {
51 c o n t += 3 ;
52 assertEquals (10 , cont ) ;
53 }
54 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 37 / 46


Anotação @BeforeClass @AfterClass (3)

Prof. Evandro César Freiberger (IFMT) Testes 2022 38 / 46


Quando uma Classe/Método Lança Exceção (1)
1 package i f m t . cba ;
2
3 import java . u t i l . ArrayList ;
4 import java . u t i l . L i s t ;
5
6 p u b l i c c l a s s NotaAluno6 {
7 p r i v a t e List <Float > l i s t a N o t a ;
8 p r i v a t e f l o a t mediaNotas ;
9 p r i v a t e f l o a t maiorNota ;
10 p r i v a t e f l o a t menorNota ;
11
12 p u b l i c NotaAluno6 ( ) {
13 t h i s . l i s t a N o t a = new A r r a y L i s t < F l o a t > ( ) ;
14 t h i s . mediaNotas = 0 ;
15 t h i s . maiorNota = F l o a t . MAX_VALUE;
16 t h i s . menorNota = F l o a t . MIN_VALUE ;
17 }
18
19 public List <Float > getListaNota ( ) {
20 return listaNota ;
21 }
22
23 p u b l i c v o i d addNota ( f l o a t nota ) {
24 t h i s . l i s t a N o t a . add ( nota ) ;
25 }
26
27 p u b l i c v o i d processarNotas ( ) throws RuntimeException {
28
29 i f ( t h i s . l i s t a N o t a . s i z e ( ) == 0 ) {
30 throw new RuntimeException ( ) ;
31 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 39 / 46


Quando uma Classe/Método Lança Exceção (2)
32
33 t h i s . maiorNota = F l o a t . MIN_VALUE ;
34 t h i s . menorNota = F l o a t . MAX_VALUE;
35 f l o a t somaNotas = 0 ;
36
37 f o r ( f l o a t nota : t h i s . l i s t a N o t a ) {
38 somaNotas += nota ;
39 i f ( nota > maiorNota ) {
40 t h i s . maiorNota = nota ;
41 }
42
43 i f ( nota < t h i s . menorNota ) {
44 t h i s . menorNota = nota ;
45 }
46 }
47 t h i s . mediaNotas = somaNotas / t h i s . l i s t a N o t a . s i z e ( ) ;
48 }
49
50 p u b l i c f l o a t getMediaNotas ( ) {
51 r e t u r n mediaNotas ;
52 }
53
54 p u b l i c f l o a t getMaiorNota ( ) {
55 r e t u r n maiorNota ;
56 }
57
58 p u b l i c f l o a t getMenorNota ( ) {
59 r e t u r n menorNota ;
60 }
61 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 40 / 46


Quando uma Classe/Método Lança Exceção (1)
Caso de testes anteriores irão falhar, visto que, uma exceção será lançada
1 package i f m t . cba ;
2
3 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
4
5 i m p o r t org . j u n i t . Test ;
6
7 p u b l i c c l a s s NotaAluno6_1Test {
8 @Test
9 p u b l i c v o i d quandoNaoAdicionaNotas ( ) {
10 NotaAluno6 nota = new NotaAluno6 ( ) ;
11 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
12 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
13 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
14 }
15
16 @Test
17 p u b l i c v o i d processarSemNotas ( ) {
18 NotaAluno6 nota = new NotaAluno6 ( ) ;
19 nota . processarNotas ( ) ;
20 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
21 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
22 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
23 }
24
25 @Test
26 p u b l i c v o i d processarComNotasDecrescente ( ) {
27 NotaAluno6 nota = new NotaAluno6 ( ) ;
28 nota . addNota ( 8 . 0 f ) ;
29 nota . addNota ( 7 . 0 f ) ;

Prof. Evandro César Freiberger (IFMT) Testes 2022 41 / 46


Quando uma Classe/Método Lança Exceção (2)
30 nota . addNota ( 6 . 0 f ) ;
31 nota . processarNotas ( ) ;
32 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
33 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
34 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
35 }
36
37 @Test
38 p u b l i c v o i d processarComNotasCrescente ( ) {
39 NotaAluno6 nota = new NotaAluno6 ( ) ;
40 nota . addNota ( 6 . 0 f ) ;
41 nota . addNota ( 7 . 0 f ) ;
42 nota . addNota ( 8 . 0 f ) ;
43 nota . processarNotas ( ) ;
44 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
45 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
46 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
47 }
48 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 42 / 46


Quando uma Classe/Método Lança Exceção (1)
Poder ser tratada com um try...catch e uso do método fail()
1 package i f m t . cba ;
2
3 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
4 i m p o r t s t a t i c org . j u n i t . A s s e r t . f a i l ;
5
6 i m p o r t org . j u n i t . Test ;
7
8 p u b l i c c l a s s NotaAluno6_2Test {
9 @Test
10 p u b l i c v o i d quandoNaoAdicionaNotas ( ) {
11 NotaAluno6 nota = new NotaAluno6 ( ) ;
12 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
13 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
14 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
15 }
16
17 @Test
18 p u b l i c v o i d processarSemNotas ( ) {
19 try {
20 NotaAluno6 nota = new NotaAluno6 ( ) ;
21 nota . processarNotas ( ) ;
22 f a i l ( ) ; / / se chegar aqui , e porque nao gerou a excecao que d e v e r i a
23 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
24 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
25 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
26 } c a t c h ( RuntimeException ex ) {
27 / / tudo c e r t o , deve g e r a r uma excecao
28 }
29 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 43 / 46


Quando uma Classe/Método Lança Exceção (2)
30
31 @Test
32 p u b l i c v o i d processarComNotasDecrescente ( ) {
33 NotaAluno6 nota = new NotaAluno6 ( ) ;
34 nota . addNota ( 8 . 0 f ) ;
35 nota . addNota ( 7 . 0 f ) ;
36 nota . addNota ( 6 . 0 f ) ;
37 nota . processarNotas ( ) ;
38 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
39 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
40 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
41 }
42
43 @Test
44 p u b l i c v o i d processarComNotasCrescente ( ) {
45 NotaAluno6 nota = new NotaAluno6 ( ) ;
46 nota . addNota ( 6 . 0 f ) ;
47 nota . addNota ( 7 . 0 f ) ;
48 nota . addNota ( 8 . 0 f ) ;
49 nota . processarNotas ( ) ;
50 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
51 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
52 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
53 }
54 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 44 / 46


Quando uma Classe/Método Lança Exceção (1)
Poder ser usada a propriedade @Test (expected = RuntimeException.class), que indica
que nesse caso de teste, espera-se receber uma exceção.
1 package i f m t . cba ;
2
3
4 i m p o r t s t a t i c org . j u n i t . A s s e r t . a s s e r t E q u a l s ;
5
6 i m p o r t org . j u n i t . Test ;
7
8 p u b l i c c l a s s NotaAluno6_3Test {
9 @Test
10 p u b l i c v o i d quandoNaoAdicionaNotas ( ) {
11 NotaAluno6 nota = new NotaAluno6 ( ) ;
12 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
13 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
14 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
15 }
16
17 @Test ( expected = RuntimeException . c l a s s )
18 p u b l i c v o i d processarSemNotas ( ) {
19 NotaAluno6 nota = new NotaAluno6 ( ) ;
20 nota . processarNotas ( ) ;
21 a s s e r t E q u a l s ( 0 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
22 a s s e r t E q u a l s ( F l o a t . MIN_VALUE , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
23 a s s e r t E q u a l s ( F l o a t . MAX_VALUE, nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
24 }
25
26 @Test
27 p u b l i c v o i d processarComNotasDecrescente ( ) {

Prof. Evandro César Freiberger (IFMT) Testes 2022 45 / 46


Quando uma Classe/Método Lança Exceção (2)
28 NotaAluno6 nota = new NotaAluno6 ( ) ;
29 nota . addNota ( 8 . 0 f ) ;
30 nota . addNota ( 7 . 0 f ) ;
31 nota . addNota ( 6 . 0 f ) ;
32 nota . processarNotas ( ) ;
33 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
34 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
35 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
36 }
37
38 @Test
39 p u b l i c v o i d processarComNotasCrescente ( ) {
40 NotaAluno6 nota = new NotaAluno6 ( ) ;
41 nota . addNota ( 6 . 0 f ) ;
42 nota . addNota ( 7 . 0 f ) ;
43 nota . addNota ( 8 . 0 f ) ;
44 nota . processarNotas ( ) ;
45 a s s e r t E q u a l s ( 7 . 0 f , nota . getMediaNotas ( ) , 0 . 0 0 1 ) ;
46 a s s e r t E q u a l s ( 8 . 0 f , nota . getMaiorNota ( ) , 0 . 0 0 1 ) ;
47 a s s e r t E q u a l s ( 6 . 0 f , nota . getMenorNota ( ) , 0 . 0 0 1 ) ;
48 }
49 }

Prof. Evandro César Freiberger (IFMT) Testes 2022 46 / 46

Você também pode gostar