整合JUnit4
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-test</artifactId> |
| <version>5.3.0</version> |
| <scope>test</scope> |
| </dependency> |
| <dependency> |
| <groupId>junit</groupId> |
| <artifactId>junit</artifactId> |
| <version>4.12</version> |
| <scope>compile</scope> |
| </dependency> |
-测试类
| @RunWith(SpringJUnit4ClassRunner.class) |
| @ContextConfiguration("classpath:bean1.xml") |
| public class JTest4 { |
| |
| @Autowired |
| private UserService userService; |
| |
| @Test |
| public void test1() { |
| userService.accountMoney(); |
| } |
| } |
| <?xml version="1.0" encoding="UTF-8"?> |
| <beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xmlns:aop="http://www.springframework.org/schema/aop" |
| xmlns:tx="http://www.springframework.org/schema/tx" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd |
| http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> |
| |
| |
| <context:component-scan base-package="org.example"></context:component-scan> |
| |
| |
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" |
| destroy-method="close"> |
| <property name="url" value="jdbc:mysql://127.0.0.1:3306/jdbc" /> |
| <property name="username" value="root" /> |
| <property name="password" value="123456" /> |
| <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> |
| </bean> |
| |
| |
| <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> |
| |
| <property name="dataSource" ref="dataSource"></property> |
| </bean> |
| |
| |
| <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
| |
| <property name="dataSource" ref="dataSource"></property> |
| </bean> |
| |
| |
| <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> |
| </beans> |
| 123 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener] |
| 134 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@7193666c, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@20deea7f, org.springframework.test.context.support.DirtiesContextTestExecutionListener@3835c46, org.springframework.test.context.transaction.TransactionalTestExecutionListener@1dde4cb2, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@7714e963, org.springframework.test.context.event.EventPublishingTestExecutionListener@20ce78ec] |
| 1000 [main] INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited |
| |
| java.lang.ArithmeticException: / by zero |
整合JUnit5
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-test</artifactId> |
| <version>5.3.0</version> |
| <scope>compile</scope> |
| </dependency> |
| <dependency> |
| <groupId>org.junit.jupiter</groupId> |
| <artifactId>junit-jupiter</artifactId> |
| <version>RELEASE</version> |
| <scope>compile</scope> |
| </dependency> |
| @ExtendWith(SpringExtension.class) |
| @ContextConfiguration("classpath:bean1.xml") |
| public class JTest5 { |
| |
| @Autowired |
| private UserService userService; |
| |
| @Test |
| public void test1() { |
| userService.accountMoney(); |
| } |
| |
| } |
| @SpringJUnitConfig(locations = "classpath:bean1.xml") |
| public class JTest5 { |
| |
| @Autowired |
| private UserService userService; |
| |
| @Test |
| public void test1() { |
| userService.accountMoney(); |
| } |
| |
| } |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术