|
38 | 38 | "\n",
|
39 | 39 | "## Testes da Hierarquia de Exceções\n",
|
40 | 40 | "\n",
|
41 |
| - "Monte um código que teste a sua hierarquia exceções (todas elas) conforme o exemplo a seguir." |
| 41 | + "Monte o código Java da sua hierarquia exceções (o máximo que conseguir até o momento) conforme o exemplo a seguir." |
42 | 42 | ]
|
43 | 43 | },
|
44 | 44 | {
|
|
49 | 49 | {
|
50 | 50 | "data": {
|
51 | 51 | "text/plain": [
|
52 |
| - "com.twosigma.beaker.javash.bkrdb57d720.DivisaoInvalida" |
| 52 | + "com.twosigma.beaker.javash.bkrd09ab654.DivisaoInvalida" |
53 | 53 | ]
|
54 | 54 | },
|
55 | 55 | "execution_count": 1,
|
|
77 | 77 | {
|
78 | 78 | "data": {
|
79 | 79 | "text/plain": [
|
80 |
| - "com.twosigma.beaker.javash.bkrdb57d720.DivisaoInutil" |
| 80 | + "com.twosigma.beaker.javash.bkrd09ab654.DivisaoInutil" |
81 | 81 | ]
|
82 | 82 | },
|
83 | 83 | "execution_count": 2,
|
|
105 | 105 | {
|
106 | 106 | "data": {
|
107 | 107 | "text/plain": [
|
108 |
| - "com.twosigma.beaker.javash.bkrdb57d720.DivisaoNaoInteira" |
| 108 | + "com.twosigma.beaker.javash.bkrd09ab654.DivisaoNaoInteira" |
109 | 109 | ]
|
110 | 110 | },
|
111 | 111 | "execution_count": 3,
|
|
133 | 133 | {
|
134 | 134 | "data": {
|
135 | 135 | "text/plain": [
|
136 |
| - "com.twosigma.beaker.javash.bkrdb57d720.Util" |
| 136 | + "com.twosigma.beaker.javash.bkrd09ab654.Util" |
137 | 137 | ]
|
138 | 138 | },
|
139 | 139 | "execution_count": 4,
|
|
147 | 147 | " int divisao;\n",
|
148 | 148 | " if (y == 1)\n",
|
149 | 149 | " throw new DivisaoInutil(\"Esta divisao eh inutil\");\n",
|
150 |
| - " if (x%y > 0)\n", |
| 150 | + " if (y > 0 && x%y > 0)\n", |
151 | 151 | " throw new DivisaoNaoInteira(\"Esta divisao nao eh inteira\");\n",
|
152 | 152 | " divisao = x / y;\n",
|
153 | 153 | " return divisao;\n",
|
154 | 154 | " }\n",
|
155 | 155 | "}"
|
156 | 156 | ]
|
157 | 157 | },
|
| 158 | + { |
| 159 | + "cell_type": "markdown", |
| 160 | + "metadata": {}, |
| 161 | + "source": [ |
| 162 | + "# Teste Sistemático\n", |
| 163 | + "\n", |
| 164 | + "Sistemas robustos têm rotinas que fazem teste sistemático do código na busca de falhas. Há frameworks como o [JUnit](https://junit.org/) que são especializados em testes. Nós faremos um teste sistemático altamente simplificado apenas das exceções. Para isso você pode adaptar ou estender a classe abaixo cuja função é testar o plano de exceções acima.\n", |
| 165 | + "\n", |
| 166 | + "A função de avaliação recebe os seguintes parâmetros:\n", |
| 167 | + "* `numerator` - numerador da divisão\n", |
| 168 | + "* `denominator` - denominador da divisão\n", |
| 169 | + "* `errorExpected` - se este teste devia produzir um erro ou não\n", |
| 170 | + "* `testSpecific` - se houver erro, se o teste deveria disparar uma exceção especializada\n", |
| 171 | + "\n", |
| 172 | + "Note que o teste é considerado correto se ele atender o que você informar (nos parâmetros) como esperado, ou seja, se você informar que um erro é esperado e o erro acontecer, ou se você informar que um erro não é esperado e o erro não acontecer. O mesmo vale para exceção especializada." |
| 173 | + ] |
| 174 | + }, |
158 | 175 | {
|
159 | 176 | "cell_type": "code",
|
160 | 177 | "execution_count": 5,
|
161 | 178 | "metadata": {},
|
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "data": { |
| 182 | + "text/plain": [ |
| 183 | + "com.twosigma.beaker.javash.bkrd09ab654.Test" |
| 184 | + ] |
| 185 | + }, |
| 186 | + "execution_count": 5, |
| 187 | + "metadata": {}, |
| 188 | + "output_type": "execute_result" |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "public class Test {\n", |
| 193 | + " private int round = 0,\n", |
| 194 | + " right = 0,\n", |
| 195 | + " wrong = 0;\n", |
| 196 | + "\n", |
| 197 | + " public void evaluate(int numerator, int denominator, boolean errorExpected, boolean testSpecific) {\n", |
| 198 | + " boolean error = true,\n", |
| 199 | + " errorCaptured = true,\n", |
| 200 | + " specific = false;\n", |
| 201 | + " round++;\n", |
| 202 | + " System.out.println(\"===== Test round \" + round + \" =====\");\n", |
| 203 | + " System.out.println(\"* error expected: \" + ((errorExpected) ? \"yes\" : \"no\"));\n", |
| 204 | + " System.out.println(\"* test specific exception: \" + ((testSpecific) ? \"yes\" : \"no\"));\n", |
| 205 | + " System.out.println(\"--- Testing...\");\n", |
| 206 | + " try {\n", |
| 207 | + " int division = Util.divide(numerator, denominator);\n", |
| 208 | + " System.out.println(\"Result of the division: \" + division);\n", |
| 209 | + " error = false;\n", |
| 210 | + " } catch (DivisaoInutil erro) {\n", |
| 211 | + " System.out.println(erro.getMessage());\n", |
| 212 | + " specific = true;\n", |
| 213 | + " } catch (DivisaoNaoInteira erro) {\n", |
| 214 | + " System.out.println(erro.getMessage());\n", |
| 215 | + " specific = true;\n", |
| 216 | + " } catch (DivisaoInvalida erro) {\n", |
| 217 | + " System.out.println(erro.getMessage());\n", |
| 218 | + " } catch (Exception erro) {\n", |
| 219 | + " System.out.println(\"Other error not captured: \" + erro.getMessage());\n", |
| 220 | + " errorCaptured = false;\n", |
| 221 | + " }\n", |
| 222 | + " \n", |
| 223 | + " System.out.println(\"--- Report\");\n", |
| 224 | + " System.out.println(\"* error found: \" + ((error) ? \"yes\" : \"no\"));\n", |
| 225 | + " System.out.println(\"* error captured: \" + ((errorCaptured) ? \"yes\" : \"no\"));\n", |
| 226 | + " System.out.println(\"* specific exception triggered: \" + ((specific) ? \"yes\" : \"no\"));\n", |
| 227 | + " boolean result = (((errorExpected && error && errorCaptured) || (!errorExpected && !error))\n", |
| 228 | + " && (testSpecific == specific));\n", |
| 229 | + " System.out.println(\"--- Final Result: \" + ((result) ? \"passed\" : \"not passed\"));\n", |
| 230 | + " if (result)\n", |
| 231 | + " right++;\n", |
| 232 | + " else\n", |
| 233 | + " wrong++;\n", |
| 234 | + " System.out.println();\n", |
| 235 | + " }\n", |
| 236 | + " \n", |
| 237 | + " public void summary() {\n", |
| 238 | + " System.out.println(\"===== Summary of Tests =====\");\n", |
| 239 | + " System.out.println(\"Tests passed: \" + right);\n", |
| 240 | + " System.out.println(\"Tests not passed: \" + wrong);\n", |
| 241 | + " }\n", |
| 242 | + "}" |
| 243 | + ] |
| 244 | + }, |
| 245 | + { |
| 246 | + "cell_type": "markdown", |
| 247 | + "metadata": {}, |
| 248 | + "source": [ |
| 249 | + "# Executando o Teste Sistemático\n", |
| 250 | + "\n", |
| 251 | + "Elabore para a sua hierarquia de exceções uma sequência de testes como o ilustrado a seguir que teste todas as suas classes de exceção. No exemplo a seguir, deixei de propósito um teste que não passou (divisão por zero) como ilustração." |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "code", |
| 256 | + "execution_count": 6, |
| 257 | + "metadata": {}, |
162 | 258 | "outputs": [
|
163 | 259 | {
|
164 | 260 | "name": "stdout",
|
165 | 261 | "output_type": "stream",
|
166 | 262 | "text": [
|
167 |
| - "=== Primeiro teste\n", |
168 |
| - "Resultado da divisao: 4\n", |
169 |
| - "=== Segundo teste\n", |
| 263 | + "===== Test round 1 =====\n", |
| 264 | + "* error expected: no\n", |
| 265 | + "* test specific exception: no\n", |
| 266 | + "--- Testing...\n", |
| 267 | + "Result of the division: 4\n", |
| 268 | + "--- Report\n", |
| 269 | + "* error found: no\n", |
| 270 | + "* error captured: yes\n", |
| 271 | + "* specific exception triggered: no\n", |
| 272 | + "--- Final Result: passed\n", |
| 273 | + "\n", |
| 274 | + "===== Test round 2 =====\n", |
| 275 | + "* error expected: yes\n", |
| 276 | + "* test specific exception: yes\n", |
| 277 | + "--- Testing...\n", |
170 | 278 | "Esta divisao eh inutil\n",
|
171 |
| - "=== Terceiro teste\n", |
| 279 | + "--- Report\n", |
| 280 | + "* error found: yes\n", |
| 281 | + "* error captured: yes\n", |
| 282 | + "* specific exception triggered: yes\n", |
| 283 | + "--- Final Result: passed\n", |
| 284 | + "\n", |
| 285 | + "===== Test round 3 =====\n", |
| 286 | + "* error expected: yes\n", |
| 287 | + "* test specific exception: yes\n", |
| 288 | + "--- Testing...\n", |
172 | 289 | "Esta divisao nao eh inteira\n",
|
173 |
| - "=== Quarto teste\n", |
174 |
| - "Ocorreu um erro nao esperado na divisao\n", |
175 |
| - "--> Esta divisao nao eh inteira\n" |
| 290 | + "--- Report\n", |
| 291 | + "* error found: yes\n", |
| 292 | + "* error captured: yes\n", |
| 293 | + "* specific exception triggered: yes\n", |
| 294 | + "--- Final Result: passed\n", |
| 295 | + "\n", |
| 296 | + "===== Test round 4 =====\n", |
| 297 | + "* error expected: yes\n", |
| 298 | + "* test specific exception: no\n", |
| 299 | + "--- Testing...\n", |
| 300 | + "Other error not captured: / by zero\n", |
| 301 | + "--- Report\n", |
| 302 | + "* error found: yes\n", |
| 303 | + "* error captured: no\n", |
| 304 | + "* specific exception triggered: no\n", |
| 305 | + "--- Final Result: not passed\n", |
| 306 | + "\n", |
| 307 | + "===== Summary of Tests =====\n", |
| 308 | + "Tests passed: 3\n", |
| 309 | + "Tests not passed: 1\n" |
176 | 310 | ]
|
177 | 311 | },
|
178 | 312 | {
|
|
181 | 315 | "null"
|
182 | 316 | ]
|
183 | 317 | },
|
184 |
| - "execution_count": 5, |
| 318 | + "execution_count": 6, |
185 | 319 | "metadata": {},
|
186 | 320 | "output_type": "execute_result"
|
187 | 321 | }
|
188 | 322 | ],
|
189 | 323 | "source": [
|
190 |
| - "// codigo testando a Excecao criada\n", |
191 |
| - "int numerador = 8;\n", |
192 |
| - "int denominador = 2;\n", |
193 |
| - "\n", |
194 |
| - "System.out.println(\"=== Primeiro teste\");\n", |
| 324 | + "Test testDivision = new Test();\n", |
195 | 325 | "\n",
|
196 | 326 | "// testando uma divisao valida\n",
|
197 |
| - "try {\n", |
198 |
| - " int divisao = Util.divide(numerador, denominador);\n", |
199 |
| - " System.out.println(\"Resultado da divisao: \" + divisao);\n", |
200 |
| - "} catch (DivisaoInvalida erro) {\n", |
201 |
| - " System.out.println(\"Ocorreu um erro nao esperado na divisao\");\n", |
202 |
| - " System.out.println(erro.getMessage());\n", |
203 |
| - "} catch (Exception erro) {\n", |
204 |
| - " System.out.println(\"Outro erro: \" + erro.getMessage());\n", |
205 |
| - "}\n", |
206 |
| - "\n", |
207 |
| - "System.out.println(\"=== Segundo teste\");\n", |
208 |
| - "\n", |
209 |
| - "denominador = 1;\n", |
| 327 | + "testDivision.evaluate(8, 2, false, false);\n", |
210 | 328 | "\n",
|
211 | 329 | "// testando a divisao inutil\n",
|
212 |
| - "try {\n", |
213 |
| - " int divisao = Util.divide(numerador, denominador);\n", |
214 |
| - " System.out.println(\"Resultado da divisao: \" + divisao);\n", |
215 |
| - "} catch (DivisaoInutil erro) {\n", |
216 |
| - " System.out.println(erro.getMessage());\n", |
217 |
| - "} catch (Exception erro) {\n", |
218 |
| - " System.out.println(\"Outro erro: \" + erro.getMessage());\n", |
219 |
| - "}\n", |
220 |
| - "\n", |
221 |
| - "System.out.println(\"=== Terceiro teste\");\n", |
222 |
| - "\n", |
223 |
| - "denominador = 3;\n", |
| 330 | + "testDivision.evaluate(8, 1, true, true);\n", |
224 | 331 | "\n",
|
225 | 332 | "// testando a divisao nao inteira\n",
|
226 |
| - "try {\n", |
227 |
| - " int divisao = Util.divide(numerador, denominador);\n", |
228 |
| - " System.out.println(\"Resultado da divisao: \" + divisao);\n", |
229 |
| - "} catch (DivisaoNaoInteira erro) {\n", |
230 |
| - " System.out.println(erro.getMessage());\n", |
231 |
| - "} catch (Exception erro) {\n", |
232 |
| - " System.out.println(\"Outro erro: \" + erro.getMessage());\n", |
233 |
| - "}\n", |
234 |
| - "\n", |
235 |
| - "System.out.println(\"=== Quarto teste\");\n", |
| 333 | + "testDivision.evaluate(8, 3, true, true);\n", |
236 | 334 | "\n",
|
237 | 335 | "// testando a super classe\n",
|
238 |
| - "try {\n", |
239 |
| - " int divisao = Util.divide(numerador, denominador);\n", |
240 |
| - " System.out.println(\"Resultado da divisao: \" + divisao);\n", |
241 |
| - "} catch (DivisaoInvalida erro) {\n", |
242 |
| - " System.out.println(\"Ocorreu um erro nao esperado na divisao\");\n", |
243 |
| - " System.out.println(\"--> \" + erro.getMessage());\n", |
244 |
| - "} catch (Exception erro) {\n", |
245 |
| - " System.out.println(\"Outro erro: \" + erro.getMessage());\n", |
246 |
| - "}" |
| 336 | + "testDivision.evaluate(8, 0, true, false);\n", |
| 337 | + "\n", |
| 338 | + "testDivision.summary();" |
247 | 339 | ]
|
248 | 340 | }
|
249 | 341 | ],
|
|
259 | 351 | "mimetype": "",
|
260 | 352 | "name": "Java",
|
261 | 353 | "nbconverter_exporter": "",
|
262 |
| - "version": "11.0.7" |
| 354 | + "version": "1.8.0_121" |
| 355 | + }, |
| 356 | + "toc": { |
| 357 | + "base_numbering": 1, |
| 358 | + "nav_menu": {}, |
| 359 | + "number_sections": false, |
| 360 | + "sideBar": false, |
| 361 | + "skip_h1_title": false, |
| 362 | + "title_cell": "Table of Contents", |
| 363 | + "title_sidebar": "Contents", |
| 364 | + "toc_cell": false, |
| 365 | + "toc_position": {}, |
| 366 | + "toc_section_display": false, |
| 367 | + "toc_window_display": false |
263 | 368 | }
|
264 | 369 | },
|
265 | 370 | "nbformat": 4,
|
|
0 commit comments