Basic JUnit related queries

optimystix

optimystix

@optimystix-4yMPKC Oct 27, 2024
Hi CEans,

I didn't learn JUnit well enough when I had the time and opportunity and now I am fumbling for answers to simple questions that have cropped up in my mind.

Suppose I want to run JUnit tests on 4 methods - addUser, deleteUser, viewUser, updateUser.
My doubts are-
1. When I run JUnit testcases on these, will these actually affect the data in the database (for both - with and without mock objects)?
  • if they do, won't it create a problem for other test cases which are dependent on the same data present in the db?
2. Manual intervention will be required every time I want to run viewUser or updateUser as the user that I am trying to view or update may not be present due to data changes in the db. Am I right? In this case, it would be extremely cumbersome in large number of test cases, to manually change the values of the dependent items.

for eg-
@Test
    public void testViewUser() {
        String userId = "2";
        DummyUser dummyUser = dummyUserDao.viewUser(userId);
        assertNotNull(dummyUser);
Here,say, I am running the test case after a month and user id 2 does not exist in the db anymore, so the test case will fail. To avoid that, I will manually have to check and update such fields every time I want to run such cases.

How can we avoid such scenarios?

I don't even know if my queries are correct. Please help and correct me if I am wrong.

Thanks in advance 😀

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Feb 13, 2014

    Tagging @#-Link-Snipped-# and @#-Link-Snipped-# .
  • Anoop Kumar

    Anoop Kumar

    @anoop-kumar-GDGRCn Feb 13, 2014

    Always load data from properties/resource file.
    Using test data directly in class files, you have compile again.
    I didn't worked on jUnit but, you probably know how to this.
    This #-Link-Snipped-#might help you.