NutzCN Logo
问答 项目运行正常,junit时候却报错
发布于 2768天前 作者 qq_5245a4a9 2054 次浏览 复制 上一个帖子 下一个帖子
标签:

项目运行,数据库是正常的。
写了个junit测试,却抱下面这个错:
java.sql.SQLException: com.mysql.jdbc.Driver
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:550)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:649)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:72)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:75)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:26)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
at org.nutz.mock.NutTestRunner.createTest(NutTestRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:33)
at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:14)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:548)
... 41 more
2017-05-24 22:54:51,329 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) DEBUG - Remove object 'dataSource' from [app]
2017-05-24 22:54:51,330 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) DEBUG - Remove object 'dao' from [app]
2017-05-24 22:54:51,330 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84)

public class MyNutTestRunner extends NutTestRunner {

    public MyNutTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    public Class<?> getMainModule() {
        return MainModule.class;
    }
}

@RunWith(MyNutTestRunner.class)
@IocBean
public class DaoTest extends Assert {

    @Inject("refer:$ioc")
    protected Ioc ioc;

    @Inject
    protected Dao dao;

    @Test
    public void ceshiDao(){
        assertNotNull(dao);
    }
}
6 回复

报错没贴全

2017-06-05 23:59:41,182 com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:713) ERROR - {dataSource-1} init error
java.sql.SQLException: com.mysql.jdbc.Driver
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:550)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:649)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:72)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:75)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:26)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
at org.nutz.mock.NutTestRunner.createTest(NutTestRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:33)
at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:14)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:548)
... 41 more
2017-06-05 23:59:41,190 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) DEBUG - Remove object 'dataSource' from [app]
2017-06-05 23:59:41,192 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) DEBUG - Remove object 'dao' from [app]
2017-06-05 23:59:41,192 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) DEBUG - Remove object 'daoTest' from [app]

org.nutz.ioc.IocException: [daoTest, dao, dataSource] # FAIL to create Ioc Bean name=[dataSource]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:75)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:26)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
at org.nutz.mock.NutTestRunner.createTest(NutTestRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:33)
at org.nutz.mock.NutTestRunner.runChild(NutTestRunner.java:14)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.RuntimeException: java.sql.SQLException: com.mysql.jdbc.Driver
at org.nutz.lang.Lang.wrapThrow(Lang.java:182)
at org.nutz.lang.Lang.wrapThrow(Lang.java:181)
at org.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:21)
at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:72)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
... 33 more
Caused by: java.sql.SQLException: com.mysql.jdbc.Driver
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:550)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:649)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
... 35 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:548)
... 41 more

2017-06-05 23:59:41,205 org.nutz.ioc.impl.NutIoc.depose(NutIoc.java:255) INFO - org.nutz.ioc.impl.NutIoc@1772160903 is closing. startup date [17-06-05 23:59:40.971]

maven吗? pom.xml贴一下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4.0.0

<groupId>nutzDemo</groupId>
<artifactId>nutz</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <poi.version>3.14</poi.version>
</properties>

<!-- maven 中央仓库地址 -->
<repositories>
    <repository>
        <id>central</id>
        <!-- 阿里云仓库 -->
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <!-- 下载速度快 -->
        <!--<url>http://maven.antelink.com/content/repositories/central/</url>-->
        <!-- 下载速度一般 -->
        <!--<url>http://uk.maven.org/maven2</url>-->
    </repository>
</repositories>

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
        <resource>
            <directory>conf</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceDirectory>WebRoot</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <warName>${project.artifactId}</warName>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutz-plugins-mock</artifactId>
        <version>1.r.58</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutz</artifactId>
        <version>1.r.58</version>
    </dependency>


    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutz-plugins-daocache</artifactId>
        <version>1.r.58</version>
    </dependency>

    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutz-integration-quartz</artifactId>
        <version>1.r.58</version>
    </dependency>

    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutz-integration-shiro</artifactId>
        <version>1.r.58</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutz-plugins-mock</artifactId>
        <version>1.r.60</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!--&lt;!&ndash; 可选, 安利一下mockito,模拟mock对象很方便 &ndash;&gt;-->
    <!--<dependency>-->
        <!--<groupId>org.mockito</groupId>-->
        <!--<artifactId>mockito-core</artifactId>-->
        <!--<version>2.6.3</version>-->
        <!--<scope>test</scope>-->
    <!--</dependency>-->

</dependencies>

为啥pom.xml里没有mysql驱动的呢?? 你把mysql驱动放在tomcat的lib里面了??

加上就好了,感谢兽总

添加回复
请先登陆
回到顶部