├── .gitignore ├── README.md ├── pom.xml └── src ├── main └── java │ └── dev │ └── camila │ └── unitTesting │ └── JUnit5 │ ├── App.java │ ├── entities │ ├── Aluno.java │ ├── Curso.java │ └── Matricula.java │ ├── enums │ └── Turno.java │ └── services │ ├── AlunoService.java │ └── MatriculaService.java └── test └── java └── dev └── camila └── unitTesting └── JUnit5 ├── AppTest.java ├── builders ├── AlunoBuilder.java └── CursoBuilder.java └── services ├── AlunoServiceTest.java └── MatriculaServiceTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/java,maven,eclipse,intellij+all,visualstudiocode 3 | # Edit at https://www.gitignore.io/?templates=java,maven,eclipse,intellij+all,visualstudiocode 4 | 5 | .metadata 6 | bin/ 7 | tmp/ 8 | *.tmp 9 | *.bak 10 | *.swp 11 | *~.nib 12 | local.properties 13 | .settings/ 14 | .loadpath 15 | .recommenders 16 | 17 | # External tool builders 18 | .externalToolBuilders/ 19 | 20 | # Locally stored "Eclipse launch configurations" 21 | *.launch 22 | 23 | # PyDev specific (Python IDE for Eclipse) 24 | *.pydevproject 25 | 26 | # CDT-specific (C/C++ Development Tooling) 27 | .cproject 28 | 29 | # CDT- autotools 30 | .autotools 31 | 32 | # Java annotation processor (APT) 33 | .factorypath 34 | 35 | # PDT-specific (PHP Development Tools) 36 | .buildpath 37 | 38 | # sbteclipse plugin 39 | .target 40 | 41 | # Tern plugin 42 | .tern-project 43 | 44 | # TeXlipse plugin 45 | .texlipse 46 | 47 | # STS (Spring Tool Suite) 48 | .springBeans 49 | 50 | # Code Recommenders 51 | .recommenders/ 52 | 53 | # Annotation Processing 54 | .apt_generated/ 55 | 56 | # Scala IDE specific (Scala & Java development for Eclipse) 57 | .cache-main 58 | .scala_dependencies 59 | .worksheet 60 | 61 | # Eclipse Core 62 | .project 63 | 64 | # JDT-specific (Eclipse Java Development Tools) 65 | .classpath 66 | 67 | # Annotation Processing 68 | .apt_generated 69 | 70 | .sts4-cache/ 71 | 72 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 73 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 74 | 75 | # User-specific stuff 76 | .idea/**/workspace.xml 77 | .idea/**/tasks.xml 78 | .idea/**/usage.statistics.xml 79 | .idea/**/dictionaries 80 | .idea/**/shelf 81 | 82 | # Generated files 83 | .idea/**/contentModel.xml 84 | 85 | # Sensitive or high-churn files 86 | .idea/**/dataSources/ 87 | .idea/**/dataSources.ids 88 | .idea/**/dataSources.local.xml 89 | .idea/**/sqlDataSources.xml 90 | .idea/**/dynamic.xml 91 | .idea/**/uiDesigner.xml 92 | .idea/**/dbnavigator.xml 93 | 94 | # Gradle 95 | .idea/**/gradle.xml 96 | .idea/**/libraries 97 | 98 | # Gradle and Maven with auto-import 99 | # When using Gradle or Maven with auto-import, you should exclude module files, 100 | # since they will be recreated, and may cause churn. Uncomment if using 101 | # auto-import. 102 | # .idea/modules.xml 103 | # .idea/*.iml 104 | # .idea/modules 105 | # *.iml 106 | # *.ipr 107 | 108 | # CMake 109 | cmake-build-*/ 110 | 111 | # Mongo Explorer plugin 112 | .idea/**/mongoSettings.xml 113 | 114 | # File-based project format 115 | *.iws 116 | 117 | # IntelliJ 118 | out/ 119 | 120 | # mpeltonen/sbt-idea plugin 121 | .idea_modules/ 122 | 123 | # JIRA plugin 124 | atlassian-ide-plugin.xml 125 | 126 | # Cursive Clojure plugin 127 | .idea/replstate.xml 128 | 129 | # Crashlytics plugin (for Android Studio and IntelliJ) 130 | com_crashlytics_export_strings.xml 131 | crashlytics.properties 132 | crashlytics-build.properties 133 | fabric.properties 134 | 135 | # Editor-based Rest Client 136 | .idea/httpRequests 137 | 138 | # Android studio 3.1+ serialized cache file 139 | .idea/caches/build_file_checksums.ser 140 | 141 | # Ignores the whole .idea folder and all .iml files 142 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 143 | 144 | .idea/ 145 | 146 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 147 | 148 | *.iml 149 | modules.xml 150 | .idea/misc.xml 151 | *.ipr 152 | 153 | # Sonarlint plugin 154 | .idea/sonarlint 155 | 156 | # Compiled class file 157 | *.class 158 | 159 | # Log file 160 | *.log 161 | 162 | # BlueJ files 163 | *.ctxt 164 | 165 | # Mobile Tools for Java (J2ME) 166 | .mtj.tmp/ 167 | 168 | # Package Files # 169 | *.jar 170 | *.war 171 | *.nar 172 | *.ear 173 | *.zip 174 | *.tar.gz 175 | *.rar 176 | 177 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 178 | hs_err_pid* 179 | 180 | target/ 181 | pom.xml.tag 182 | pom.xml.releaseBackup 183 | pom.xml.versionsBackup 184 | pom.xml.next 185 | release.properties 186 | dependency-reduced-pom.xml 187 | buildNumber.properties 188 | .mvn/timing.properties 189 | .mvn/wrapper/maven-wrapper.jar 190 | .flattened-pom.xml 191 | 192 | .vscode/* 193 | !.vscode/settings.json 194 | !.vscode/tasks.json 195 | !.vscode/launch.json 196 | !.vscode/extensions.json 197 | 198 | # Ignore all local history of files 199 | .history 200 | 201 | # End of https://www.gitignore.io/api/java,maven,eclipse,intellij+all,visualstudiocode -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Testes unitários em Java utilizando JUnit​

2 |

Este curso tem como objetivo habilitar o(a) aluno(a) a testar soluções desenvolvidas na linguagem Java, tornando-o apto a apoiar a implantação e utilização da Plataforma Digital do Poder Judiciário – PDPJ-Br no seu Tribunal.

3 | 4 |

Ao final do curso o participante deverá demonstrar ampla capacidade no uso dos conceitos de testes em Java conhecendo, entre outros: Criação de testes unitários em Java utilizando JUnit.

5 |
6 | 7 |

Configuração do ambiente de desenvolvimento

8 | 9 | https://github.com/cami-la/modulo_testes_automatizados_aula2_CNJ 10 | 11 |

Estruturação dos testes e dos seus artefatos

12 | 13 | 24 | 25 |

Criação dos Testes

26 | 43 | 44 |

Referências

45 |
    46 |
  1. 47 | frameworkdemoiselle.gov.br​ 48 |
  2. 49 |
  3. 50 | JUnit 5 User Guide 51 |
  4. 52 |
53 |

🤝 Contribuindo

54 | Este repositório foi criado para fins de estudo, então contribua com ele.
55 | 56 | Se possível:
57 | ⭐️ Star o projeto
58 | 🐛 Encontrar e relatar issues
59 |

60 | 61 | 62 | Disponibilizado com ♥ por [cami-la](https://www.linkedin.com/in/cami-la/ "cami-la"). 63 | ​ -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | dev.camila 7 | unit-testing-JUnit5 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | unit-testing-JUnit5 12 | http://www.example.com 13 | 14 | 15 | UTF-8 16 | 11 17 | ${java.version} 18 | ${java.version} 19 | ${java.version} 20 | 21 | 5.6.2 22 | 23 | 24 | 3.2.2 25 | 3.1.0 26 | 3.1.0 27 | 3.8.1 28 | 3.0.0-M5 29 | 3.2.0 30 | 3.0.0-M1 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.junit.jupiter 40 | junit-jupiter-api 41 | ${junit} 42 | test 43 | 44 | 45 | org.junit.jupiter 46 | junit-jupiter-engine 47 | ${junit} 48 | test 49 | 50 | 51 | org.junit.jupiter 52 | junit-jupiter-params 53 | ${junit} 54 | test 55 | 56 | 57 | 58 | 59 | 60 | 61 | maven-clean-plugin 62 | 3.1.0 63 | 64 | 65 | maven-resources-plugin 66 | 3.1.0 67 | 68 | 69 | maven-compiler-plugin 70 | 3.8.1 71 | 72 | 73 | maven-surefire-plugin 74 | 3.0.0-M4 75 | 76 | 77 | maven-jar-plugin 78 | 3.2.0 79 | 80 | 81 | maven-install-plugin 82 | 3.0.0-M1 83 | 84 | 85 | org.apache.maven.plugins 86 | maven-shade-plugin 87 | ${maven.shade} 88 | 89 | 90 | package 91 | 92 | shade 93 | 94 | 95 | 96 | 97 | dev.camila.unitTesting.JUnit5.App 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /src/main/java/dev/camila/unitTesting/JUnit5/App.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | System.getProperties().list(System.out); 7 | } 8 | 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/dev/camila/unitTesting/JUnit5/entities/Aluno.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.entities; 2 | 3 | public class Aluno { 4 | private Long id; 5 | private String nome; 6 | private String numeroContato; 7 | 8 | public Aluno(Long id, String nome, String numeroContato) { 9 | this.id = id; 10 | this.nome = nome; 11 | this.numeroContato = numeroContato; 12 | } 13 | 14 | public Aluno() { 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getNome() { 26 | return nome; 27 | } 28 | 29 | public void setNome(String nome) { 30 | this.nome = nome; 31 | } 32 | 33 | public String getNumeroContato() { 34 | return numeroContato; 35 | } 36 | 37 | public void setNumeroContato(String numeroContato) { 38 | this.numeroContato = numeroContato; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/dev/camila/unitTesting/JUnit5/entities/Curso.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.entities; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Curso { 7 | private Long id; 8 | private String nome; 9 | private Long idProfessor; 10 | private List matriculas = new ArrayList(); 11 | private Double valorCurso; 12 | private Boolean status; 13 | 14 | public Curso(Long id, String nome, Long idProfessor, List matriculas, Double valorCurso, 15 | Boolean status) { 16 | this.id = id; 17 | this.nome = nome; 18 | this.idProfessor = idProfessor; 19 | this.matriculas = matriculas; 20 | this.valorCurso = valorCurso; 21 | this.status = status; 22 | } 23 | 24 | public Curso() { 25 | } 26 | 27 | public Long getId() { 28 | return id; 29 | } 30 | 31 | public void setId(Long id) { 32 | this.id = id; 33 | } 34 | 35 | public String getNome() { 36 | return nome; 37 | } 38 | 39 | public void setNome(String nome) { 40 | this.nome = nome; 41 | } 42 | 43 | public Long getIdProfessor() { 44 | return idProfessor; 45 | } 46 | 47 | public void setIdProfessor(Long idProfessor) { 48 | this.idProfessor = idProfessor; 49 | } 50 | 51 | public List getMatriculas() { 52 | return matriculas; 53 | } 54 | 55 | public void setMatriculas(List matriculas) { 56 | this.matriculas = matriculas; 57 | } 58 | 59 | public Double getValorCurso() { 60 | return valorCurso; 61 | } 62 | 63 | public void setValorCurso(Double valorCurso) { 64 | this.valorCurso = valorCurso; 65 | } 66 | 67 | public Boolean getStatus() { 68 | return status; 69 | } 70 | 71 | public void setStatus(Boolean status) { 72 | this.status = status; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/dev/camila/unitTesting/JUnit5/entities/Matricula.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.entities; 2 | 3 | import dev.camila.unitTesting.JUnit5.enums.Turno; 4 | 5 | public class Matricula { 6 | private Long id; 7 | private Aluno aluno; 8 | private Curso curso; 9 | private Turno turno; 10 | private Double valorMatricula; 11 | 12 | public Matricula(Long id, Aluno aluno, Curso curso, Turno turno, Double valorMatricula) { 13 | this.id = id; 14 | this.aluno = aluno; 15 | this.curso = curso; 16 | this.turno = turno; 17 | this.valorMatricula = valorMatricula; 18 | } 19 | 20 | public Matricula() { 21 | } 22 | 23 | public Long getId() { 24 | return id; 25 | } 26 | 27 | public void setId(Long id) { 28 | this.id = id; 29 | } 30 | 31 | public Aluno getAluno() { 32 | return aluno; 33 | } 34 | 35 | public void setAluno(Aluno aluno) { 36 | this.aluno = aluno; 37 | } 38 | 39 | public Curso getCurso() { 40 | return curso; 41 | } 42 | 43 | public void setCurso(Curso curso) { 44 | this.curso = curso; 45 | } 46 | 47 | public Turno getTurno() { 48 | return turno; 49 | } 50 | 51 | public void setTurno(Turno turno) { 52 | this.turno = turno; 53 | } 54 | 55 | public Double getValorMatricula() { 56 | return valorMatricula; 57 | } 58 | 59 | public void setValorMatricula(Double valorMatricula) { 60 | this.valorMatricula = valorMatricula; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/dev/camila/unitTesting/JUnit5/enums/Turno.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.enums; 2 | 3 | public enum Turno { 4 | MANHA { 5 | @Override 6 | public double porcentagemDesconto() { 7 | return 0.90d; 8 | } 9 | }, TARDE { 10 | @Override 11 | public double porcentagemDesconto() { 12 | return 0.95d; 13 | } 14 | }, NOITE { 15 | @Override 16 | public double porcentagemDesconto() { 17 | return 1d; 18 | } 19 | }; 20 | 21 | public abstract double porcentagemDesconto(); 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/dev/camila/unitTesting/JUnit5/services/AlunoService.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.services; 2 | 3 | import java.util.Random; 4 | 5 | import dev.camila.unitTesting.JUnit5.entities.Aluno; 6 | 7 | public class AlunoService { 8 | 9 | public Aluno novoAluno(String nome, String numeroContato) { 10 | if(!validaNumero(numeroContato)) { 11 | throw new IllegalStateException(String.format("Número inválido [%s]", numeroContato)); 12 | } 13 | Aluno aluno = new Aluno(); 14 | aluno.setId(new Random().nextLong()); 15 | aluno.setNome(nome); 16 | aluno.setNumeroContato(numeroContato); 17 | return aluno; 18 | } 19 | 20 | public static boolean validaNumero(String numeroContato) { 21 | return numeroContato.startsWith("81") && 22 | numeroContato.indexOf("9") == 2 && 23 | numeroContato.length() == 11; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/dev/camila/unitTesting/JUnit5/services/MatriculaService.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.services; 2 | 3 | import java.util.Random; 4 | 5 | import dev.camila.unitTesting.JUnit5.entities.Aluno; 6 | import dev.camila.unitTesting.JUnit5.entities.Curso; 7 | import dev.camila.unitTesting.JUnit5.entities.Matricula; 8 | import dev.camila.unitTesting.JUnit5.enums.Turno; 9 | 10 | public class MatriculaService { 11 | 12 | public Matricula novaMatricula(Aluno aluno, Curso curso, Turno turno) { 13 | if (aluno == null || curso == null || turno == null) { 14 | throw new NullPointerException("Entre em contato com o suporte"); 15 | } 16 | if (!curso.getStatus()) { 17 | throw new IllegalStateException(String.format("O curso '%s' não está ativo", curso.getNome())); 18 | } 19 | Matricula novaMatricula = new Matricula(); 20 | novaMatricula.setId(new Random().nextLong()); 21 | novaMatricula.setAluno(aluno); 22 | novaMatricula.setCurso(curso); 23 | novaMatricula.setTurno(turno); 24 | novaMatricula.setValorMatricula(calculaValorMatricula(curso, turno)); 25 | adicionaMatriculaCurso(curso, novaMatricula); 26 | return novaMatricula; 27 | } 28 | 29 | public double calculaValorMatricula(Curso curso, Turno turno) { 30 | return curso.getValorCurso() * turno.porcentagemDesconto(); 31 | } 32 | 33 | public void adicionaMatriculaCurso(Curso curso, Matricula matricula) { 34 | curso.getMatriculas().add(matricula); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/dev/camila/unitTesting/JUnit5/AppTest.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertTrue; 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class AppTest { 7 | 8 | @Test 9 | public void shouldAnswerWithTrue() { 10 | assertTrue(true); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/dev/camila/unitTesting/JUnit5/builders/AlunoBuilder.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.builders; 2 | 3 | import java.util.Random; 4 | 5 | import dev.camila.unitTesting.JUnit5.entities.Aluno; 6 | 7 | public class AlunoBuilder { 8 | private Aluno aluno; 9 | 10 | public static AlunoBuilder builder() { 11 | AlunoBuilder builder = new AlunoBuilder(); 12 | Long idAluno = new Random().nextLong(); 13 | String nomeAluno = "Camila"; 14 | String contatoAluno = "00000000"; 15 | builder.aluno = new Aluno(idAluno, nomeAluno, contatoAluno); 16 | return builder; 17 | } 18 | 19 | public Aluno build() { 20 | return this.aluno; 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/dev/camila/unitTesting/JUnit5/builders/CursoBuilder.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.builders; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Random; 6 | 7 | import dev.camila.unitTesting.JUnit5.entities.Curso; 8 | import dev.camila.unitTesting.JUnit5.entities.Matricula; 9 | 10 | public class CursoBuilder { 11 | private Curso curso; 12 | 13 | public static CursoBuilder builder() { 14 | CursoBuilder builder = new CursoBuilder(); 15 | Long idCurso = new Random().nextLong(); 16 | String nomeCurso = "Unit Tests JUnit5"; 17 | Long idProfessor = new Random().nextLong(); 18 | List matriculas = new ArrayList(); 19 | Double valorCurso = 1000d; 20 | Boolean status = true; 21 | builder.curso = new Curso(idCurso, nomeCurso, idProfessor, matriculas, valorCurso, status); 22 | return builder; 23 | } 24 | 25 | public Curso build() { 26 | return this.curso; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/dev/camila/unitTesting/JUnit5/services/AlunoServiceTest.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.services; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | import org.junit.jupiter.params.ParameterizedTest; 6 | import org.junit.jupiter.params.provider.CsvSource; 7 | 8 | import dev.camila.unitTesting.JUnit5.entities.Aluno; 9 | 10 | class AlunoServiceTest { 11 | 12 | @Test 13 | void devera_CriarUmNovoAluno_RetornarAluno() { 14 | // given 15 | String nomeAluno = "Camila"; 16 | String contatoAluno = "81900000000"; 17 | 18 | // when 19 | AlunoService alunoService = new AlunoService(); 20 | Aluno actual = alunoService.novoAluno(nomeAluno, contatoAluno); 21 | 22 | // then 23 | Assertions.assertNotNull(actual); 24 | Assertions.assertEquals(nomeAluno, actual.getNome()); 25 | Assertions.assertEquals(contatoAluno, actual.getNumeroContato()); 26 | } 27 | 28 | @ParameterizedTest 29 | @CsvSource({ 30 | "81900000000, true", 31 | "82900000000, false", 32 | "81800000000, false", 33 | "81900, false" }) 34 | void devera_ValidarONumeroDeContatoDoAluno(String numeroContato, Boolean expected) { 35 | // when 36 | boolean actual = AlunoService.validaNumero(numeroContato); 37 | 38 | // then 39 | Assertions.assertEquals(expected, actual); 40 | 41 | } 42 | 43 | @Test 44 | void devera_LancarIllegalArgumentException_QuandoNumeroContatoInvalido() { 45 | //given 46 | String nomeAluno = "Camila"; 47 | String numeroContato = "87382"; 48 | 49 | //when 50 | //then 51 | AlunoService alunoService = new AlunoService(); 52 | IllegalStateException actualException = Assertions.assertThrows(IllegalStateException.class, 53 | () -> alunoService.novoAluno(nomeAluno, numeroContato)); 54 | 55 | String expectedMessage = String.format("Número inválido [%s]", numeroContato); 56 | 57 | Assertions.assertEquals(expectedMessage, actualException.getMessage()); 58 | } 59 | 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/dev/camila/unitTesting/JUnit5/services/MatriculaServiceTest.java: -------------------------------------------------------------------------------- 1 | package dev.camila.unitTesting.JUnit5.services; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Assumptions; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; 8 | import org.junit.jupiter.api.condition.EnabledForJreRange; 9 | import org.junit.jupiter.api.condition.EnabledIfSystemProperty; 10 | import org.junit.jupiter.api.condition.JRE; 11 | import org.junit.jupiter.params.ParameterizedTest; 12 | import org.junit.jupiter.params.provider.EnumSource; 13 | 14 | import dev.camila.unitTesting.JUnit5.builders.AlunoBuilder; 15 | import dev.camila.unitTesting.JUnit5.builders.CursoBuilder; 16 | import dev.camila.unitTesting.JUnit5.entities.Aluno; 17 | import dev.camila.unitTesting.JUnit5.entities.Curso; 18 | import dev.camila.unitTesting.JUnit5.entities.Matricula; 19 | import dev.camila.unitTesting.JUnit5.enums.Turno; 20 | 21 | class MatriculaServiceTest { 22 | 23 | private MatriculaService matriculaService; 24 | 25 | @BeforeEach 26 | void setup() { 27 | this.matriculaService = new MatriculaService(); 28 | } 29 | 30 | @ParameterizedTest 31 | @EnumSource(Turno.class) 32 | void devera_CriarUmaNovaMatricula_IndependenteDoTurno(Turno turno) { 33 | Assumptions.assumeTrue("camila".equals(System.getProperty("user.name"))); 34 | // given 35 | Aluno aluno = AlunoBuilder.builder().build(); 36 | Curso curso = CursoBuilder.builder().build(); 37 | 38 | // when 39 | Matricula actual = matriculaService.novaMatricula(aluno, curso, turno); 40 | 41 | // then 42 | Matricula expectedMatricula = curso.getMatriculas().get(0); 43 | double expectedValorMatricula = curso.getValorCurso() * turno.porcentagemDesconto(); 44 | 45 | Assertions.assertEquals(aluno, actual.getAluno()); 46 | Assertions.assertEquals(curso, actual.getCurso()); 47 | Assertions.assertEquals(turno, actual.getTurno()); 48 | Assertions.assertEquals(expectedValorMatricula, actual.getValorMatricula()); 49 | Assertions.assertEquals(expectedMatricula, actual); 50 | } 51 | 52 | 53 | @Test 54 | void devera_lancarNullPointerException_QuandoAlunoNull() { 55 | Assumptions.assumeFalse("Windows".equals(System.getProperty("os.name"))); 56 | // given 57 | Curso curso = CursoBuilder.builder().build(); 58 | Turno turno = Turno.NOITE; 59 | 60 | // when 61 | // then 62 | NullPointerException actualException = Assertions.assertThrows(NullPointerException.class, 63 | () -> matriculaService.novaMatricula(null, curso, turno)); 64 | 65 | String expectedMessage = "Entre em contato com o suporte"; 66 | 67 | Assertions.assertEquals(expectedMessage, actualException.getMessage()); 68 | } 69 | 70 | @Test 71 | @EnabledForJreRange(min=JRE.JAVA_8, max=JRE.JAVA_11) 72 | void devera_lancarNullPointerException_QuandoCursoNull() { 73 | // given 74 | Aluno aluno = AlunoBuilder.builder().build(); 75 | Turno turno = Turno.NOITE; 76 | 77 | // when 78 | // then 79 | NullPointerException actualException = Assertions.assertThrows(NullPointerException.class, 80 | () -> matriculaService.novaMatricula(aluno, null, turno)); 81 | 82 | String expectedMessage = "Entre em contato com o suporte"; 83 | 84 | Assertions.assertEquals(expectedMessage, actualException.getMessage()); 85 | } 86 | 87 | @Test 88 | @EnabledIfSystemProperty(named = "os.arch", matches = "amd64") 89 | void devera_lancarNullPointerException_QuandoTurnoNull() { 90 | // given 91 | Aluno aluno = AlunoBuilder.builder().build(); 92 | Curso curso = CursoBuilder.builder().build(); 93 | 94 | // when 95 | // then 96 | NullPointerException actualException = Assertions.assertThrows(NullPointerException.class, 97 | () -> matriculaService.novaMatricula(aluno, curso, null)); 98 | 99 | String expectedMessage = "Entre em contato com o suporte"; 100 | 101 | Assertions.assertEquals(expectedMessage, actualException.getMessage()); 102 | } 103 | 104 | @Test 105 | @DisabledIfEnvironmentVariable(named = "USER", matches = "camila") 106 | void devera_lancarIllegalStateException_QuandoCursoDesativado() { 107 | // given 108 | Aluno aluno = AlunoBuilder.builder().build(); 109 | Curso curso = CursoBuilder.builder().build(); 110 | Turno turno = Turno.MANHA; 111 | 112 | // when 113 | // then 114 | curso.setStatus(false); 115 | IllegalStateException actualException = Assertions.assertThrows(IllegalStateException.class, 116 | () -> matriculaService.novaMatricula(aluno, curso, turno)); 117 | 118 | String expectedMessage = String.format("O curso '%s' não está ativo", curso.getNome()); 119 | 120 | Assertions.assertEquals(expectedMessage, actualException.getMessage()); 121 | } 122 | } 123 | --------------------------------------------------------------------------------