• Spring集成Junit完成JdbcTemplate修改和删除,jdbc内容和配置解耦合


    目录

     jdbc内容和配置解耦合

     jdbc.properties下

    applicationContext1.xml下

    返回测试,运行结果

    使用Spring集成Junit完成JdbcTemplate修改和删除

    pom.xml

    修改操作 

     Junit4.11版本导致错误

    运行结果

    删除的操作

    运行结果 


     jdbc内容和配置解耦

    抽取配置文件中的jdbc代码,使用单独的一个配置文件将次分开

     jdbc.properties下

    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/test
    3. jdbc.username=root
    4. jdbc.password=123456

    applicationContext1.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xsi:schemaLocation="
    5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    7. ">
    8. <context:property-placeholder location="classpath:jdbc.properties"/>
    9. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    10. <property name="driverClass" value="${jdbc.driver}"/>
    11. <property name="jdbcUrl" value="${jdbc.url}"/>
    12. <property name="user" value="${jdbc.username}"/>
    13. <property name="password" value="${jdbc.password}"/>
    14. bean>
    15. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    16. <property name="dataSource" ref="dataSource"/>
    17. bean>
    18. beans>

     修改一下数据

    1. public class JdbcTemplateTest {
    2. @Test
    3. //测试Spring产生jdbcTemplate对象
    4. public void test2() throws PropertyVetoException {
    5. ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext1.xml");
    6. JdbcTemplate jdbcTemplate=app.getBean(JdbcTemplate.class);
    7. int row = jdbcTemplate.update("insert into account value(?,?)", "ZH",66666);
    8. System.out.println(row);
    9. }
    10. }

    返回测试,运行结果

     数据库中变化:

    使用Spring集成Junit完成JdbcTemplate修改和删除

    在pom.xml需要导入两个包,spring-test包和Juit包

    pom.xml

    1. <dependency>
    2. <groupId>junitgroupId>
    3. <artifactId>junitartifactId>
    4. <version>4.12version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframeworkgroupId>
    8. <artifactId>spring-testartifactId>
    9. <version>5.0.5.RELEASEversion>
    10. dependency>

    修改操作 

    新建一个类

    1. package test;
    2. import org.junit.Test;
    3. import org.junit.runner.RunWith;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.jdbc.core.JdbcTemplate;
    6. import org.springframework.test.context.ContextConfiguration;
    7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    8. //指定使用spring做为驱动内核进行测试
    9. @RunWith(SpringJUnit4ClassRunner.class)
    10. //配置文件的位置
    11. @ContextConfiguration("classpath:applicationContext1.xml")
    12. public class JdbcTemplateCRUDTest {
    13. //被测试的用 @Autowired注入(测试谁就注入谁)
    14. @Autowired
    15. private JdbcTemplate jdbcTemplate;
    16. @Test
    17. public void testUpdate(){
    18. int row = jdbcTemplate.update("update account set money=? where name=?", 222, "tom");
    19. System.out.println(row);
    20. }
    21. }

     Junit4.11版本导致错误

    一开始我使用了junit4.11的版本

    1. C:\java64\jdk1.8.0_131\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=4613:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit5-rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit-rt.jar;C:\java64\jdk1.8.0_131\jre\lib\charsets.jar;C:\java64\jdk1.8.0_131\jre\lib\deploy.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\access-bridge-64.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\cldrdata.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\dnsns.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\jaccess.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\jfxrt.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\localedata.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\nashorn.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\sunec.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\sunjce_provider.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\sunmscapi.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\sunpkcs11.jar;C:\java64\jdk1.8.0_131\jre\lib\ext\zipfs.jar;C:\java64\jdk1.8.0_131\jre\lib\javaws.jar;C:\java64\jdk1.8.0_131\jre\lib\jce.jar;C:\java64\jdk1.8.0_131\jre\lib\jfr.jar;C:\java64\jdk1.8.0_131\jre\lib\jfxswt.jar;C:\java64\jdk1.8.0_131\jre\lib\jsse.jar;C:\java64\jdk1.8.0_131\jre\lib\management-agent.jar;C:\java64\jdk1.8.0_131\jre\lib\plugin.jar;C:\java64\jdk1.8.0_131\jre\lib\resources.jar;C:\java64\jdk1.8.0_131\jre\lib\rt.jar;D:\小项目\webTest\target\test-classes;D:\小项目\webTest\target\classes;E:\Maven_project\lib\javax.transaction.jar;E:\Maven_project\lib\javax.annotation.jar;E:\Maven_project\lib\javax.jms.jar;E:\Maven_project\lib\javax.resource.jar;E:\Maven_project\lib\javax.ejb.jar;E:\Maven_project\lib\javax.persistence.jar;E:\Maven_project\lib\javax.servlet.jar;E:\Maven_project\lib\javax.servlet.jsp.jstl.jar;E:\Maven_project\lib\javax.servlet.jsp.jar;C:\Users\执久\.m2\repository\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar;C:\Users\执久\.m2\repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;C:\Users\执久\.m2\repository\com\alibaba\druid\1.1.10\druid-1.1.10.jar;C:\Users\执久\.m2\repository\junit\junit\4.11\junit-4.11.jar;C:\Users\执久\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\执久\.m2\repository\org\springframework\spring-test\5.0.5.RELEASE\spring-test-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-core\5.0.5.RELEASE\spring-core-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-jcl\5.0.5.RELEASE\spring-jcl-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-context\5.0.5.RELEASE\spring-context-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-aop\5.0.5.RELEASE\spring-aop-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-beans\5.0.5.RELEASE\spring-beans-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-expression\5.0.5.RELEASE\spring-expression-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-web\5.0.5.RELEASE\spring-web-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-webmvc\5.0.5.RELEASE\spring-webmvc-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.9.0\jackson-core-2.9.0.jar;C:\Users\执久\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.9.0\jackson-databind-2.9.0.jar;C:\Users\执久\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\执久\.m2\repository\org\springframework\spring-jdbc\5.0.5.RELEASE\spring-jdbc-5.0.5.RELEASE.jar;C:\Users\执久\.m2\repository\org\springframework\spring-tx\5.0.5.RELEASE\spring-tx-5.0.5.RELEASE.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 test.JdbcTemplateCRUDTest,testUpdate
    2. java.lang.ExceptionInInitializerError
    3. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    4. at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    5. at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    6. at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    7. at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
    8. at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
    9. at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    10. at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    11. at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    12. at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    13. at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:31)
    14. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    15. at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    16. at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    17. at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
    18. Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
    19. at org.springframework.util.Assert.state(Assert.java:73)
    20. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:104)
    21. ... 15 more
    22. 进程已结束,退出代码 -1

     将其改为Junit4.12就可以正确执行了

    运行结果

     数据库中

    删除的操作

    1. @Test
    2. public void testDelete(){
    3. int row1 = jdbcTemplate.update("delete from account where name=?", "tom");
    4. System.out.println(row1);
    5. }

    运行结果 

     数据库中

  • 相关阅读:
    LevelDB源码解读-SkipList
    跨域(CORS)和JWT 详解
    OpenCV自学笔记十二:形态操作(二)
    CC0 是什么,为什么它会改变 NFT 市场?
    re 正则从文本中提取全球电话和邮箱
    【Java】继承练习
    哪些人真的不适合学习数据分析,你中招了吗?
    C语言:生成校验码
    源码编译安装与yum和rpm软件安装详解
    AI推荐算法的演进之路
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/126191641