目录
jdbc内容和配置解耦合
jdbc.properties下
applicationContext1.xml下
返回测试,运行结果
使用Spring集成Junit完成JdbcTemplate修改和删除
pom.xml
修改操作
Junit4.11版本导致错误
运行结果
删除的操作
运行结果
抽取配置文件中的jdbc代码,使用单独的一个配置文件将次分开
jdbc.properties下
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
修改一下数据
public class JdbcTemplateTest {
@Test
//测试Spring产生jdbcTemplate对象
public void test2() throws PropertyVetoException {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext1.xml");
JdbcTemplate jdbcTemplate=app.getBean(JdbcTemplate.class);
int row = jdbcTemplate.update("insert into account value(?,?)", "ZH",66666);
System.out.println(row);
}
}
数据库中变化:
在pom.xml需要导入两个包,spring-test包和Juit包
junit
junit
4.12
org.springframework
spring-test
5.0.5.RELEASE
新建一个类
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//指定使用spring做为驱动内核进行测试
@RunWith(SpringJUnit4ClassRunner.class)
//配置文件的位置
@ContextConfiguration("classpath:applicationContext1.xml")
public class JdbcTemplateCRUDTest {
//被测试的用 @Autowired注入(测试谁就注入谁)
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testUpdate(){
int row = jdbcTemplate.update("update account set money=? where name=?", 222, "tom");
System.out.println(row);
}
}
一开始我使用了junit4.11的版本
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
java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:31)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
at org.springframework.util.Assert.state(Assert.java:73)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:104)
... 15 more
进程已结束,退出代码 -1
将其改为Junit4.12就可以正确执行了
运行结果
数据库中
@Test
public void testDelete(){
int row1 = jdbcTemplate.update("delete from account where name=?", "tom");
System.out.println(row1);
}
数据库中