File tree Expand file tree Collapse file tree 4 files changed +91
-8
lines changed
main/java/com/coderolls/SampleProject
test/java/com/coderolls/SampleProject Expand file tree Collapse file tree 4 files changed +91
-8
lines changed Original file line number Diff line number Diff line change 1
- # junit-tutorial
2
- A repository for JUnit tutorial example
1
+ # JUnit Tutorial
2
+
3
+ A repository for JUnit Tutorial examples from the blog and videos
4
+
5
+ 1 . [ Why We Write JUnit Test Cases?] ( https://coderolls.com/why-unit-test-cases/ )
6
+ 2 . [ How To Write JUnit Test Case In Java? (With Example)] ( https://coderolls.com/junit-test-case-in-java/ )
Original file line number Diff line number Diff line change 15
15
</properties >
16
16
17
17
<dependencies >
18
- <dependency >
19
- <groupId >junit</groupId >
20
- <artifactId >junit</artifactId >
21
- <version >3.8.1</version >
22
- <scope >test</scope >
23
- </dependency >
18
+ <!-- https://mvnrepository.com/artifact/junit/junit -->
19
+ <dependency >
20
+ <groupId >junit</groupId >
21
+ <artifactId >junit</artifactId >
22
+ <version >4.0</version >
23
+ <scope >test</scope >
24
+ </dependency >
25
+
24
26
</dependencies >
25
27
</project >
Original file line number Diff line number Diff line change
1
+ package com .coderolls .SampleProject ;
2
+
3
+ public class PasswordGenerator {
4
+ /**
5
+ * A method generate password from name and year of birth
6
+ *
7
+ * Ex. For name 'Thomas' and Year of birth '1992', it will generate password as 'Thom1992'.
8
+ *
9
+ * If name has equal to or less than 4 character then it will return name+YearOfith as password.
10
+ *
11
+ * Ex. For name 'Mary' and year of birth '2003', it will return 'Mary2003' as password.
12
+ *
13
+ * @param name
14
+ * @param yearOfBirth
15
+ * @return
16
+ */
17
+ public String generatePassword (String name , int yearOfBirth ) {
18
+ String password = null ;
19
+
20
+ if (name ==null ) {
21
+ return password ;
22
+ }
23
+
24
+ if (name .length ()<=4 ) {
25
+ password = name +yearOfBirth ;
26
+ }else {
27
+ String str = name .substring (0 , 4 );
28
+ password =str +yearOfBirth ;
29
+ }
30
+ return password ;
31
+ }
32
+
33
+ }
Original file line number Diff line number Diff line change
1
+ package com .coderolls .SampleProject ;
2
+
3
+ import org .junit .Test ;
4
+
5
+ import junit .framework .TestCase ;
6
+
7
+ public class PasswordGeneratorTest extends TestCase {
8
+
9
+ @ Test
10
+ public void testGeneratePassword () {
11
+ String name = "Joseph" ;
12
+ int yearOFBirth = 1998 ;
13
+
14
+ String expected = "Jose1998" ;
15
+
16
+ PasswordGenerator passwordGenerator = new PasswordGenerator ();
17
+ String actual = passwordGenerator .generatePassword (name , yearOFBirth );
18
+ assertEquals (expected , actual );
19
+ }
20
+
21
+ @ Test
22
+ public void testGeneratePassword_nameLessThan4Charaters () {
23
+ String name = "Nic" ;
24
+ int yearOFBirth = 2002 ;
25
+
26
+ String expected = "Nic2002" ;
27
+
28
+ PasswordGenerator passwordGenerator = new PasswordGenerator ();
29
+ String actual = passwordGenerator .generatePassword (name , yearOFBirth );
30
+
31
+ assertEquals (expected , actual );
32
+ }
33
+
34
+ @ Test
35
+ public void testGeneratePassword_nameIsNull () {
36
+ String name = null ;
37
+ int yearOFBirth = 2002 ;
38
+
39
+ PasswordGenerator passwordGenerator = new PasswordGenerator ();
40
+ String actual = passwordGenerator .generatePassword (name , yearOFBirth );
41
+
42
+ assertNull (actual );
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments