FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/junit-exception/src/test/java/com/hmkcode/AppTest.java at master · user-git/Java · GitHub
user-git
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
junit-exception
/
src
/
test
/
java
/
com
/
hmkcode
/
AppTest.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
59 lines (41 loc) · 1.48 KB
Breadcrumbs
Java
/
junit-exception
/
src
/
test
/
java
/
com
/
hmkcode
/
AppTest.java
Copy path
File metadata and controls
59 lines (41 loc) · 1.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
com
.
hmkcode
;
import
java
.
util
.
LinkedList
;
import
org
.
junit
.
Rule
;
import
org
.
junit
.
Test
;
import
org
.
junit
.
rules
.
ExpectedException
;
import
org
.
junit
.
runner
.
RunWith
;
import
org
.
junit
.
runners
.
JUnit4
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
@
RunWith
(
JUnit4
.
class
)
public
class
AppTest
{
// ( 1 ) Simple way to test exception
@
Test
(
expected
=
NullPointerException
.
class
)
public
void
testNull
() {
String
str
=
null
;
str
.
toUpperCase
();
}
// ( 2 ) Test by tyr/catch (test exception message)
@
Test
public
void
testExceptionMessage
(){
try
{
new
LinkedList
<
Object
>().
get
(
0
);
//if no exception thrown the test will fail with the below message.
fail
(
"Expected an IndexOutOfBoundsException to be thrown"
);
}
catch
(
IndexOutOfBoundsException
anIndexOutOfBoundsException
) {
//if no exception message is not the same the test will fail with the below message.
assertThat
(
anIndexOutOfBoundsException
.
getMessage
(),
is
(
"Index: 0, Size: 0"
));
}
}
//( 3 ) Test by @Rule & ExpectedException.expect() | ExpectedException.expectMessage()
@
Rule
public
ExpectedException
thrown
=
ExpectedException
.
none
();
@
Test
public
void
testExceptionByRule
()
throws
ArithmeticException
{
thrown
.
expect
(
ArithmeticException
.
class
);
thrown
.
expectMessage
(
"/ by zero"
);
double
d
=
1
/
0
;
}
}
Back
|
FazBrowse Home
|
New Git URL