JUnit
JUnit is the easiest and the most preferred testing framework for Java and Spring applications. By writing JUnit test cases for our application, we can improve the quality of our application and also avoid buggy situations.
Here, we will discuss a simple JUnit test case, which is calling the getAllUsers
method in userService
. We can check the following code:
@RunWith(SpringRunner.class) @SpringBootTest public class UserTests { @Autowired UserService userSevice; @Test public void testAllUsers(){ List<User> users = userSevice.getAllUsers(); assertEquals(3, users.size()); } }
In the preceding code, we have called getAllUsers
and verified the total count. Let's test the single-user method in another test case:
// other methods @Test public void testSingleUser(){ User user = userSevice.getUser(100); assertTrue(user.getUsername().contains("David")); }
In the preceding code snippets, we just tested our service layer and verified the business logic. However,...