简体   繁体   中英

Junit tests passing individually, not in groups

Currently i'm creating tests to test my services. I've already tested the daos with no issue. The services use the daos for testing, but the daos had no issue. I'm pretty sure there is some decoupling that needs to be done, but i'm not able to trace it.

This is the test for the Dao

public class MockContactInfoDaoTest  {

    private MockContactInfoDao mCIDao;
    private ContactInfo relevantContactInfo;
    private ContactInfoType mobilePhone;

    @Before
    public void setUp() throws Exception {

        mCIDao = new MockContactInfoDao();
        mCIDao.initMockData();

        mobilePhone = new ContactInfoType((long) 1,"cellPhone","Contact user mobile device",0 );

        relevantContactInfo = new ContactInfo((long)6,mobilePhone.getId(),"Larry","Bird","Carl","1234 apple st","N/A","US",
                "Hyattsville","MD","20782","Medium Money Ent.","123-878-7890","email6@email.com","acceptsmarketing","Cell phone",
                "Male",(new Date(1985, 1, 1)),mobilePhone,0);
    }

    @After
    public void tearDown() throws Exception{

        mCIDao = null;
    }

    @Test
    public void testCreateContactInfo() throws Exception {

        mCIDao.createContactInfo(relevantContactInfo);
        assertEquals(6,mCIDao.contactInfos.size());
    }
    @Test
    public void testFindContactInfo() throws Exception{

        Long five = (long) 5;
        assertEquals(five,mCIDao.findContactInfo(five).getId());
    }

    @Test
    public void testFetchContactInfos() {

        assertEquals(5,mCIDao.fetchContactInfos().size());
    }

This is the test for the Service

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/core-service-context.xml"})
public class ContactInfoServiceImplTest {


    @Autowired
    private ContactInfoServiceImpl contactInfoService;
    private ContactInfo relevantContactInfo;
    private ContactInfoType mobilePhone;

    @Before
    public void setUp() throws Exception {

        //Initializing objects neccesary for contactinfo
        mobilePhone = new ContactInfoType((long) 1,"cellPhone","Contact user mobile device",0 );
        relevantContactInfo = new ContactInfo((long)6,mobilePhone.getId(),"Larry","Bird","Carl","1234 apple st","N/A","US",
                "Hyattsville","MD","20782","Medium Money Ent.","123-878-7890","email6@email.com","acceptsmarketing","Cell phone",
                "Male",(new Date(1985, 1, 1)),mobilePhone,0);
    }

    @Test
    public void testCreateContactInfo() throws Exception {
        ContactInfo savedContactInfo = null;
        savedContactInfo = contactInfoService.createContactInfo(relevantContactInfo);
        assertEquals(savedContactInfo.getId(), relevantContactInfo.getId());
}

    @Test
    public void testFindContactInfo() throws Exception{

        Long five = (long) 5;
        assertEquals(five,contactInfoService.findContactInfo(five).getId());
    }

    public void setContactInfoService(ContactInfoServiceImpl contactInfoService) {
        this.contactInfoService = contactInfoService;
    }



    @Test

    public void testFetchContactInfos() {

        assertEquals(5,contactInfoService.fetchContactInfos().size());
    }
}

The error i'm getting while running the service test, is with testFetchContactInfos. When ran by itself, it returns 5 as expected. When ran with the other tests, it returns 6. This must be because of the testCreateContactInfo method where an item is added to the list thereby returning 6 in that scenario. The service test closesly mirrors the dao test, yet the dao didn't give me this kinda of issue.

We can't see your production code or your mock code so we can't see exactly what is going on, but we can guess.

Any data that sticks around between tests will lead to the individual tests passing while the group fails.

Here is an example:

class A {
    static int x;
    void increment() {
        x++;
    }
}   
public class ATest {

    A a;

    @Before   
    public void setUp() {
        a = new A();
    }
    @Test
    public void test1() {
        a.increment();
        Assert.assertEquals(1,  a.x);
    }
    @Test
    public void test2() {
        a.increment();
        Assert.assertEquals(1,  a.x);
    }

}

See if you can find something like this in your code. Static data or persistent DB data could give you the same result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM