简体   繁体   English

为什么我可以为Spring项目运行JUnit测试,但不能运行main方法呢?

[英]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.

相关问题 我的Maven项目如何在没有TestNG尝试运行JUnit测试的情况下同时运行JUnit和TestNG? - How can my Maven project run both JUnit and TestNG without TestNG trying to run JUnit tests? 为什么可以在不显式设置类路径的情况下运行JUnit测试? - Why can I run JUnit tests without explicitly setting the classpath? 为什么我不能从Eclipse运行Spring项目? - Why can't I run my Spring project from eclipse? 除了使用集成测试运行器在IntelliJ IDEA项目中以“IntegrationTest”结尾的所有JUnit单元测试之外,我该如何运行? - How can I run all JUnit unit tests except those ending in “IntegrationTest” in my IntelliJ IDEA project using the integrated test runner? 我可以在Spring JUnit测试中使用@Import吗? - Can I use @Import in Spring JUnit tests? 为什么我不能使用JUnit 4测试我的项目? - Why can't I test my project using JUnit 4? 如何改进我的 junit 测试 - How can I improve my junit tests 我的Eclipse无法再运行(或调试)我的JUnit测试 - My Eclipse can no longer run (or debug) my JUnit tests 如何让 JUnit 测试与 Gradle 项目一起运行? - How do I get JUnit tests to run with Gradle project? 如果使用Spring和Junit 4,如何编写内部测试类? 还是我还能如何构建测试? - How to write inner test classes if you use Spring and Junit 4? Or How else I can structure my tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM