[英]Why can I run JUnit tests for my Spring project, but not a main method?
I am using JDBC to connect to MySQL for a small application. 我正在使用JDBC连接到小型应用程序的MySQL。 In order to test without altering the real database, I'm using HSQL in memory for JUnit tests. 为了在不更改实际数据库的情况下进行测试,我在内存中使用HSQL进行JUnit测试。
I'm using Spring for DI and DAOs. 我将Spring用于DI和DAO。 Here is how I'm configuring my HSQL DataSource 这是我配置HSQL数据源的方式
<bean id="mockDataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:mockSeo"/>
<property name="username" value="sa"/>
</bean>
This works fine for my JUnit tests which use the mock DB. 对于使用模拟DB的JUnit测试,这工作正常。 But when I try to run a main method, I find the following error: 但是,当我尝试运行main方法时,发现以下错误:
Error creating bean with name 'mockDataSource' defined in class path resource [beans.xml]:
Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException:
Could not load JDBC driver class [org.hsqldb.jdbcDriver]
I'm running from Eclipse, and I'm using the Maven plugin. 我正在Eclipse上运行,并且正在使用Maven插件。 Is there a reason why this would work as a Test, but not as a main()
? 是否有理由将其作为Test而不是main()
起作用? I know that the main
method itself is not the problem, because it works if I remove all references to the HSQL DataSource from my Spring Configuration file. 我知道main
方法本身不是问题,因为如果我从Spring Configuration文件中删除对HSQL DataSource的所有引用,它将起作用。
m2eclipse won't add your test-scoped dependencies to the classpath when you run the application as 'Java Application'. 当您以“ Java应用程序”身份运行应用程序时,m2eclipse不会将您的测试范围的依赖项添加到类路径中。 You will need to change the scope of the hsql dependency to 'compile' (the default) for that to happen. 您需要将hsql依赖项的范围更改为“ compile”(默认)才能实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.