-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrote test cases for the methods defined
- Loading branch information
knalawade
committed
Mar 26, 2023
1 parent
9b99ab0
commit 29cac27
Showing
7 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import java.util.List; | ||
|
||
public class ApplicationDao { | ||
public User getUserById(String id) { | ||
if(id.equals("3412")) | ||
return null; | ||
// Make database query and return a list of users | ||
User user = new User(); | ||
user.setUsername("mark"); | ||
user.setPassword("name123"); | ||
user.initializePosts(); | ||
user.addPost("It's a beautiful day"); | ||
return user; | ||
} | ||
|
||
public void save(User user) throws Exception { | ||
// Save entry into db | ||
// throws exception if user obj is null | ||
if(user == null) | ||
throw new Exception("The user is null"); | ||
System.out.println("User saved successfully"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
public class Basics { | ||
public int compare(int n1, int n2) { | ||
if (n1 > n2) return 1; | ||
else if (n1 < n2) return -1; | ||
return 0; | ||
} | ||
|
||
public int max(int[] arr) { | ||
if(arr.length == 0) return -1; | ||
|
||
int max = arr[0]; | ||
for (int i = 1; i < arr.length; i++) { | ||
if (arr[i] > max) { | ||
max = arr[i]; | ||
} | ||
} | ||
return max; | ||
} | ||
|
||
public void sortArray(int[] array) { | ||
int n = array.length; | ||
for (int i = 0; i < n-1; i++) { | ||
for (int j = 0; j < n-i-1; j++) { | ||
if (array[j] > array[j+1]) { | ||
int temp = array[j]; | ||
array[j] = array[j+1]; | ||
array[j+1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public String reverseString(String s) { | ||
int length = s.length(); | ||
StringBuilder reversed = new StringBuilder(length); | ||
|
||
for (int i = length - 1; i >= 0; i--) { | ||
reversed.append(s.charAt(i)); | ||
} | ||
|
||
return reversed.toString(); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
public class Mocking { | ||
|
||
User user; | ||
|
||
|
||
public void setUser(User user) { | ||
this.user = getUser(user); | ||
} | ||
|
||
private User getUser(User user) { | ||
return user; | ||
} | ||
|
||
public int assignPermission() { | ||
if(user.getRole().equals("admin")) { | ||
String username = user.getUsername(); | ||
System.out.println("Assign special permissions for user " + username); | ||
return 1; | ||
} else { | ||
System.out.println("Cannot assign permission"); | ||
return -1; | ||
} | ||
} | ||
|
||
public int updateUsername(String id, String username) throws Exception{ | ||
ApplicationDao applicationDao = new ApplicationDao(); | ||
User user = applicationDao.getUserById(id); | ||
if(user!=null) | ||
user.setUsername(username); | ||
applicationDao.save(user); | ||
return 1; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class User { | ||
private String id; | ||
private String username; | ||
private String password; | ||
private String role; | ||
|
||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getRole() { | ||
return role; | ||
} | ||
|
||
private List<String> posts; | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public List<String> getPosts() { | ||
return posts; | ||
} | ||
|
||
public void initializePosts() { posts = new ArrayList<>(); } | ||
|
||
public void addPost(String post) { | ||
posts.add(post); | ||
} | ||
|
||
public List<String> getAllPostsContainingWord(String word) { | ||
List<String> filteredPosts = new ArrayList<>(); | ||
for(String post: posts) { | ||
if(post.contains(word)) | ||
filteredPosts.add(post); | ||
} | ||
return filteredPosts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class Welcome { | ||
public static void main(String[] args) { | ||
System.out.println("Welcome to Testing Tutorial"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BasicTests { | ||
|
||
@Test | ||
public void compare() { | ||
Basics basicTests = new Basics(); | ||
int value = basicTests.compare(2, 1); | ||
Assertions.assertEquals(1, value); | ||
} | ||
|
||
@Test | ||
@DisplayName("First number is less than the second") | ||
public void compare2() { | ||
Basics basicTests = new Basics(); | ||
int value = basicTests.compare(2, 3); | ||
Assertions.assertEquals(-1, value); | ||
} | ||
|
||
@Test | ||
@DisplayName("First number is equal to the second") | ||
public void compare3() { | ||
Basics basicTests = new Basics(); | ||
int value = basicTests.compare(2, 2); | ||
Assertions.assertEquals(0, value); | ||
} | ||
|
||
@Test | ||
@DisplayName("Array sorted") | ||
public void sortArray() { | ||
Basics basicTests = new Basics(); | ||
int[] array = {5, 8, 3, 9, 1, 6}; | ||
basicTests.sortArray(array); | ||
Assertions.assertArrayEquals(new int[]{1, 3, 5, 6, 8, 9}, array); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class MockingTests { | ||
|
||
Mocking mocking; | ||
|
||
@Mock | ||
ApplicationDao applicationDao; | ||
|
||
|
||
@BeforeEach | ||
public void setup() { | ||
// user = mock(User.class); | ||
mocking = new Mocking(); | ||
|
||
} | ||
@Test | ||
@DisplayName("Permission assigned successfully") | ||
public void assignPermissions() { | ||
User user = mock(User.class); | ||
mocking.setUser(user); | ||
when(user.getRole()).thenReturn("admin"); | ||
when(user.getUsername()).thenReturn("kunal"); | ||
Assertions.assertEquals(1, mocking.assignPermission()); | ||
|
||
// Test Method Stubbing for explanation | ||
// List<String> filteredPosts = new ArrayList<>(); | ||
// filteredPosts.add("Awesome Day"); | ||
// filteredPosts.add("This place is awesome"); | ||
// when(user.getAllPostsContainingWord("awesome")).thenReturn(filteredPosts); | ||
} | ||
|
||
@Test | ||
@DisplayName("User updated successfully") | ||
public void updateUsername() throws Exception { | ||
User user = new User(); | ||
user.setUsername("kunal"); | ||
lenient().when(applicationDao.getUserById(Mockito.anyString())).thenReturn(user); | ||
Assertions.assertEquals(1, mocking.updateUsername("3211", "allan")); | ||
} | ||
|
||
@Test | ||
@DisplayName("User could not be updated") | ||
public void updateUsernameError() throws Exception { | ||
lenient().when(applicationDao.getUserById(Mockito.anyString())).thenReturn(null); | ||
Assertions.assertThrows(Exception.class, () -> { | ||
mocking.updateUsername("3412","allan"); | ||
}); | ||
} | ||
} |