user123
user123

Reputation: 281

Spring jdbctemplate

I am new to Spring MVC and JDBCTemplate and badly need some help on this. I've declared the following in applicationContext.xml:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://" />
    <property name="username" value="user" />
    <property name="password" value="pwd" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

In my DAOImpl class, I've the following code:

@Repository
public class ABCDAOImpl implements ABCDAO
{

    private String INSERT_SQL = null;
    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource)
    {
        this.jdbcTemplate = new JdbcTemplate( dataSource );
    }

    @Override
    public boolean insertDataInDataBase(final Object obj)
    {
        boolean insertSuccessful = false;

        INSERT_SQL = "INSERT INTO XXX " +
                "(AA, BB, CC, DD, EE, " +
                "FF, GG, HH, II) VALUES (?,?,?,?,?,?,?,?,?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();
    jdbcTemplate.update(new PreparedStatementCreator() 
            {public PreparedStatement createPreparedStatement(Connection connection) 
                           throws SQLException
                {
                  PreparedStatement ps = null;
                  ps = connection.prepareStatement(INSERT_SQL);
                  ps.setString(1, xx);
                  ps.setString(2, xx);
                  ps.setString(3, xx);
                      ps.setString(4, xx);
                  ps.setString(5, xx);
                  ps.setString(6, xx;
                  ps.setString(7, xx);
                  ps.setString(8, xx);
                  ps.setString(9, xx);
                  return ps;
                  }}, keyHolder);

        return insertSuccessful;    
    }
}

Test class:

  import static org.junit.Assert.assertTrue;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:applicationContext.xml"})
    public class ABCDAOImplTest
    {
    private Object object = new Object();
    private ABCDAOImpl abcDAOImpl;

    @Before
    public void setup()
    {
        object.setAllVaribles();
        abcDAOImpl = new ABCDAOImpl();
    }

    @Test
    public void testRepositoryInsert()
    {
        System.out.println(abcDAOImpl.insertDataInDataBase(object));
        assertTrue(abcDAOImpl.insertDataInDataBase(object));
    }

}

Then I use jdbcTemplate to carry out an insert statement using PreparedStatementCreator.
Now, I am trying to test (without mocking, I am hardcoding the values just to test..) whether this code works or not? When I run this test, it gives me NPE saying jdbcTemplate is null. Is there something wrong with my code or is there something with the way I am testing it? Any help would be greatly appreciated.

Happy New Year :)

PS - I have annotated the Test class with @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"}) only after comment from @Pradeep. And now I get another exception: "Failed to load ApplicationContext."

Upvotes: 0

Views: 2385

Answers (2)

Ryan Stewart
Ryan Stewart

Reputation: 128799

The most obvious answer is that your test hasn't called the setDataSource() method or done anything else that would cause the JdbcTemplate to be created. You haven't shown enough code for someone to point out where the problem is, though.

Update: In your test, you say abcDAOImpl = new ABCDAOImpl();, and that's all. How do you expect the JdbcTemplate/DataSource to be injected? It won't happen by magic. Either you have to finish wiring it all up manually, or else let Spring inject the DAO into your test. To do that, just add a field like this:

@Autowired
private ABCDAO abcDao;

Upvotes: 1

NPKR
NPKR

Reputation: 5496

Maybe your missing Import of applicationContext.xml file in test-applicationContext.xml file

<import resource="classpath:applicationContext.xml" />

Upvotes: 0

Related Questions