File tree Expand file tree Collapse file tree 8 files changed +175
-0
lines changed
Expand file tree Collapse file tree 8 files changed +175
-0
lines changed Original file line number Diff line number Diff line change 1717 <groupId >junit</groupId >
1818 <artifactId >junit</artifactId >
1919 </dependency >
20+ <dependency >
21+ <groupId >org.projectlombok</groupId >
22+ <artifactId >lombok</artifactId >
23+ </dependency >
2024 </dependencies >
2125
2226</project >
Original file line number Diff line number Diff line change 1+ package com .yiyun ;
2+
3+ import lombok .AllArgsConstructor ;
4+
5+ @ AllArgsConstructor
6+ public class MessageUtil {
7+ private String message ;
8+ // prints the message
9+ public String printMessage (){
10+ System .out .println (message );
11+ return message ;
12+ }
13+
14+ // add "Hi!" to the message
15+ public String salutationMessage (){
16+ message = "Hi!" + message ;
17+ System .out .println (message );
18+ return message ;
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .yiyun .junit ;
2+
3+
4+ import org .junit .Test ;
5+
6+ import static org .junit .Assert .*;
7+
8+ public class AssertTest {
9+ public static void main (String [] args ) {
10+ String name ="yi-yun" ;
11+ System .out .println ("name = " + name );
12+ }
13+ @ Test
14+ public void funX (){
15+
16+ }@ Test
17+ public void funData (){
18+ //test data
19+ int num = 5 ;
20+ String temp = null ;
21+ String str = "yi-yun" ;
22+ assertFalse (num >7 );
23+ assertNotNull (str );
24+ }
25+ @ Test
26+ public void funAdd (){
27+ String s ="yi-yun" ;
28+ assertEquals ("yi-yun" ,s );
29+ System .out .println ("s = " + s );
30+ }
31+ @ Test
32+ public void fun (){
33+ String name ="yi-yun" ;
34+ assert (name !=null );
35+ System .out .println ("name = " + name );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package com .yiyun .junit ;
2+
3+ import org .junit .*;
4+ import org .junit .runner .JUnitCore ;
5+ import org .junit .runner .Result ;
6+ import org .junit .runner .notification .Failure ;
7+
8+ public class JunitAnnotation {
9+ //execute before class
10+ @ BeforeClass
11+ public static void beforeClass () {
12+ System .out .println ("in before class" );
13+ }
14+
15+ //execute after class
16+ @ AfterClass
17+ public static void afterClass () {
18+ System .out .println ("in after class" );
19+ }
20+
21+ //execute before test
22+ @ Before
23+ public void before () {
24+ System .out .println ("in before" );
25+ }
26+
27+ //execute after test
28+ @ After
29+ public void after () {
30+ System .out .println ("in after" );
31+ }
32+
33+ //test case
34+ @ Test
35+ public void test () {
36+ System .out .println ("in test" );
37+ }
38+
39+ //test case ignore and will not execute
40+ @ Ignore
41+ public void ignoreTest () {
42+ System .out .println ("in ignore test" );
43+ }
44+ public static void main (String [] args ) {
45+ Result result = JUnitCore .runClasses (JunitAnnotation .class );
46+ for (Failure failure : result .getFailures ()) {
47+ System .out .println (failure .toString ());
48+ }
49+ System .out .println (result .wasSuccessful ());
50+ }
51+ }
Original file line number Diff line number Diff line change 1+ package com .yiyun .junit ;
2+
3+ import org .junit .runner .RunWith ;
4+ import org .junit .runners .Suite ;
5+
6+ @ RunWith (Suite .class )
7+ @ Suite .SuiteClasses ({
8+ TestJunit1 .class ,
9+ TestJunit2 .class
10+ })
11+ public class JunitTestSuite {
12+ }
Original file line number Diff line number Diff line change 1+ package com .yiyun .junit ;
2+
3+ import com .yiyun .MessageUtil ;
4+ import org .junit .Test ;
5+
6+ import static org .junit .Assert .assertEquals ;
7+ import static org .junit .Assert .assertNotEquals ;
8+
9+ public class TestJunit1 {
10+ String message = "yi-yun" ;
11+ MessageUtil messageUtil = new MessageUtil (message );
12+
13+ @ Test
14+ public void testPrintMessage () {
15+ System .out .println ("Inside yiyun-----testPrintMessage()" );
16+ assertNotEquals (message , messageUtil .printMessage ());
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package com .yiyun .junit ;
2+
3+ import com .yiyun .MessageUtil ;
4+ import org .junit .Test ;
5+
6+ import static org .junit .Assert .assertEquals ;
7+
8+ public class TestJunit2 {
9+ String message = "Robert" ;
10+ MessageUtil messageUtil = new MessageUtil (message );
11+
12+ @ Test
13+ public void testSalutationMessage () {
14+ System .out .println ("Inside testSalutationMessage()" );
15+ message = "Hi!" + "Robert" ;
16+ assertEquals (message ,messageUtil .salutationMessage ());
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package com .yiyun .junit ;
2+
3+ import org .junit .runner .JUnitCore ;
4+ import org .junit .runner .Result ;
5+ import org .junit .runner .notification .Failure ;
6+
7+ public class TestRunner {
8+ public static void main (String [] args ) {
9+ Result result = JUnitCore .runClasses (JunitTestSuite .class );
10+ for (Failure failure : result .getFailures ()) {
11+ System .out .println (failure .toString ());
12+ }
13+ System .out .println (result .wasSuccessful ());
14+ }
15+ }
You can’t perform that action at this time.
0 commit comments