Hi,

I am working on a java web application using Spring MVC framework. The application is running fine. I am writing some JUnit test cases for some of the pojo classes inside the webapp. I created a separate source folder 'test' which is in parallel to the src folder. I am writing a simple test case that gets a list of locations from the database using the DAO object. The problem is when i run the Testcase, it fails with a Null Pointer exception. And after some debugging i found that it does not even enter the method inside the DAO class. Any pointers as to where i could go wrong?

public class TestLocationService {
    LocationService locationService = new LocationService();
    @Resource(name="locationDAO")
    private ILocationDAO locationDAO;
    // Test to get Locations
    @Test
    public void testGetLocations() {
        List<Location> list = locationDAO.getList();
        Assert.assertNotNull(list);
    }
}

public class TestRunner {
    public static void main(String[] args) {
          Result result = JUnitCore.runClasses(TestLocationService.class);
          for (Failure failure : result.getFailures()) {
             System.out.println(failure.toString());
          }
          System.out.println(result.wasSuccessful());
       }
}

public class LocationService
{
    ...
    public List<LocationForm> getLocation()
    {
        List<Location> locations=locationDAO.getList();
        List<LocationForm> list = new ArrayList<LocationForm>();
        for(Location location:locations)
        {
        LocationForm locationForm = new LocationForm();
        locationForm.setLocID(location.getLocID());
        locationForm.setLocName(location.getLocName());
        locationForm.setCoords(location.getCoords());
        list.add(locationForm);
    }
    return list;
    }
    ...
}

Any suggestions would be of great help.

Thanks,
YogeshP

Recommended Answers

All 3 Replies

Hi,

Any ideas or pointers on this question? someone who has faced a similar issue? Thanks in advance.

Regards,
YogeshP

Hi tux4life,

Thanks a lot for the reply. I have put a component scan into my application-context file. Thing is that i have 2 source folders, one is src and the other is test. Both have the same package names. Now, i have the following lines in my application-context

<context:component-scan base-package="com.hpc.ong" />

What i am not sure about is whether this line includes both the source folders. Any ideas?

Regards,
YogeshP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.