Java SpringJDBCTemplate

Java SpringJDBCTemplate,java,spring-mvc,jdbctemplate,Java,Spring Mvc,Jdbctemplate,我是SpringMVC和JDBCTemplate的新手,非常需要这方面的帮助。我在applicationContext.xml中声明了以下内容: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" />

我是SpringMVC和JDBCTemplate的新手,非常需要这方面的帮助。我在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>
测试等级:

  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));
    }

}
然后我使用jdbcTemplate使用PreparedStatementCreator执行insert语句。
现在,我正在尝试测试(没有模拟,我正在硬编码值只是为了测试…)这段代码是否有效?当我运行这个测试时,它告诉我NPE jdbcTemplate为null。我的代码有什么问题吗?或者我测试它的方式有什么问题吗?任何帮助都将不胜感激

新年快乐:)

PS-我已经用@RunWith(SpringJUnit4ClassRunner.class)注释了测试类
@ContextConfiguration(locations={“classpath:applicationContext.xml”})仅位于@Pradeep的注释之后。现在我遇到了另一个例外:“加载ApplicationContext失败。”

最明显的答案是,您的测试没有调用
setDataSource()
方法,也没有执行任何其他会导致创建JdbcTemplate的操作。但是,您没有显示足够的代码,以便有人指出问题所在

更新:在测试中,您说
abcDAOImpl=new abcDAOImpl(),仅此而已。您希望如何注入JdbcTemplate/DataSource?这不会是魔法造成的。要么您必须手动完成所有连接,要么让Spring将DAO注入到您的测试中。为此,只需添加如下字段:

@Autowired
private ABCDAO abcDao;

可能是在
测试applicationContext.xml
文件中缺少导入
applicationContext.xml
文件

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


您的
DAOImpl
类是否包含在
组件扫描中
?是的。1.试试2。尝试基于实例变量jdbcTemplate而不是DAOImpl上的settruse@Repository进行注入。java@swamy我已经做到了。谢谢你的回复,在你的帮助下,我取得了更大的进步。我没有创建一个单独的test-applicationContext.xml,只是用applicationContext注释了我的测试类(我已经将整个代码放在文章中)。我已经用@RunWith(SpringJunit4Runner.class)和@ContextConfiguration(locations={“classpath:applicationContext.xml})注释了我的类。现在,当我运行JUnit测试时,它说“加载应用程序上下文失败”。测试类无法找到applicationContext文件。不幸的是,您的代码非常不可读。使用IDE正确设置格式,然后将其粘贴到您的问题中,全部选中,然后单击编辑器中的“代码”按钮使其变得有用。听起来您最初的问题似乎是您甚至没有启动Spring进行测试,但您希望它为您自动布线。现在您正试图启动它,但您的配置存在一些其他问题。是的,您是对的。我尝试过使用“代码”按钮。如果我能做点别的,请告诉我。是的,那好多了。更新了我的答案。谢谢Ryan和其他人的帮助。
<import resource="classpath:applicationContext.xml" />