测试时提示空指针

来源:4-8 DAO层单元测试编码和问题排查(上)

zoeyqq

2018-04-02 11:15

package org.seckill.dao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.seckill.entity.Seckill;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Date;
import java.util.List;

import static org.junit.Assert.*;

/**
 * Created by zhouqian on 2018/4/2.
 */

/**
 * 配置spring和Junit整合,Junit启动时加载springIOC容器
 */
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring的配置文件位置
@ContextConfiguration({"classpath:spring/spring-dao.xml"})
public class SeckillDaoTest {

    //注入Dao依赖
    private SeckillDao seckillDao;

    @Test
    public void testQueryById() throws Exception {
        long id = 1000;
        Seckill seckill = seckillDao.queryById(id);
        System.out.println(seckill.getName());
        System.out.println(seckill);
    }


    @Test
    public void testReduceNumber() throws Exception {
        int count = seckillDao.reduceNumber(1000,new Date());
        System.out.println(count);

    }

    @Test
    public void testQueryAll() throws Exception {
        List<Seckill> seckills =seckillDao.queryAll(0,100);
        for(Seckill seckill : seckills){
            System.out.println(seckill);
        }
    }
}

/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:50386,suspend=y,server=n -ea -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA 15.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA 15.app/Contents/plugins/junit/lib/junit-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/tools.jar:/Users/zhouqian/IdeaProjects/seckill/target/test-classes:/Users/zhouqian/IdeaProjects/seckill/target/classes:/Library/maven/repository/junit/junit/4.11/junit-4.11.jar:/Library/maven/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Library/maven/repository/org/slf4j/slf4j-api/1.7.12/slf4j-api-1.7.12.jar:/Library/maven/repository/ch/qos/logback/logback-core/1.1.1/logback-core-1.1.1.jar:/Library/maven/repository/ch/qos/logback/logback-classic/1.1.1/logback-classic-1.1.1.jar:/Library/maven/repository/mysql/mysql-connector-java/5.1.35/mysql-connector-java-5.1.35.jar:/Library/maven/repository/c3p0/c3p0/0.9.1.2/c3p0-0.9.1.2.jar:/Library/maven/repository/org/mybatis/mybatis/3.3.0/mybatis-3.3.0.jar:/Library/maven/repository/org/mybatis/mybatis-spring/1.2.3/mybatis-spring-1.2.3.jar:/Library/maven/repository/taglibs/standard/1.1.2/standard-1.1.2.jar:/Library/maven/repository/jstl/jstl/1.2/jstl-1.2.jar:/Library/maven/repository/com/fasterxml/jackson/core/jackson-databind/2.5.4/jackson-databind-2.5.4.jar:/Library/maven/repository/com/fasterxml/jackson/core/jackson-annotations/2.5.0/jackson-annotations-2.5.0.jar:/Library/maven/repository/com/fasterxml/jackson/core/jackson-core/2.5.4/jackson-core-2.5.4.jar:/Library/maven/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Library/maven/repository/org/springframework/spring-core/4.1.7.RELEASE/spring-core-4.1.7.RELEASE.jar:/Library/maven/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/Library/maven/repository/org/springframework/spring-beans/4.1.7.RELEASE/spring-beans-4.1.7.RELEASE.jar:/Library/maven/repository/org/springframework/spring-context/4.1.7.RELEASE/spring-context-4.1.7.RELEASE.jar:/Library/maven/repository/org/springframework/spring-aop/4.1.7.RELEASE/spring-aop-4.1.7.RELEASE.jar:/Library/maven/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar:/Library/maven/repository/org/springframework/spring-expression/4.1.7.RELEASE/spring-expression-4.1.7.RELEASE.jar:/Library/maven/repository/org/springframework/spring-jdbc/4.1.7.RELEASE/spring-jdbc-4.1.7.RELEASE.jar:/Library/maven/repository/org/springframework/spring-tx/4.1.7.RELEASE/spring-tx-4.1.7.RELEASE.jar:/Library/maven/repository/org/springframework/spring-web/4.1.7.RELEASE/spring-web-4.1.7.RELEASE.jar:/Library/maven/repository/org/springframework/spring-webmvc/4.1.7.RELEASE/spring-webmvc-4.1.7.RELEASE.jar:/Library/maven/repository/org/springframework/spring-test/4.1.7.RELEASE/spring-test-4.1.7.RELEASE.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 org.seckill.dao.SeckillDaoTest,testQueryById

Connected to the target VM, address: '127.0.0.1:50386', transport: 'socket'

四月 02, 2018 11:13:35 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames

信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]

四月 02, 2018 11:13:35 上午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners

信息: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1b490621, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@57316e85, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6e4f0a7b, org.springframework.test.context.transaction.TransactionalTestExecutionListener@73949d1, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@421d3c0b]

四月 02, 2018 11:13:36 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [spring/spring-dao.xml]

四月 02, 2018 11:13:36 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh

信息: Refreshing org.springframework.context.support.GenericApplicationContext@361b90be: startup date [Mon Apr 02 11:13:36 CST 2018]; root of context hierarchy

四月 02, 2018 11:13:36 上午 org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties

信息: Loading properties file from class path resource [jdbc.properties]

四月 02, 2018 11:13:36 上午 com.mchange.v2.log.MLog <clinit>

信息: MLog clients using java 1.4+ standard logging.

四月 02, 2018 11:13:37 上午 com.mchange.v2.c3p0.C3P0Registry banner

信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]

11:13:37.657 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.

11:13:37.857 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Class not found: org.jboss.vfs.VFS

11:13:37.858 [main] DEBUG org.apache.ibatis.io.ResolverUtil - JBoss 6 VFS API is not available in this environment.

11:13:37.859 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Class not found: org.jboss.vfs.VirtualFile

11:13:37.860 [main] DEBUG org.apache.ibatis.io.ResolverUtil - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.

11:13:37.861 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Using VFS adapter org.apache.ibatis.io.DefaultVFS

11:13:37.862 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Find JAR URL: file:/Users/zhouqian/IdeaProjects/seckill/target/classes/org/seckill/entity

11:13:37.862 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Not a JAR: file:/Users/zhouqian/IdeaProjects/seckill/target/classes/org/seckill/entity

11:13:38.091 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Reader entry: Seckill.class

11:13:38.092 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Reader entry: SuccessKilled.class

11:13:38.093 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Listing file:/Users/zhouqian/IdeaProjects/seckill/target/classes/org/seckill/entity

11:13:38.093 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Find JAR URL: file:/Users/zhouqian/IdeaProjects/seckill/target/classes/org/seckill/entity/Seckill.class

11:13:38.093 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Not a JAR: file:/Users/zhouqian/IdeaProjects/seckill/target/classes/org/seckill/entity/Seckill.class

11:13:38.094 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Reader entry: ����1\

11:13:38.095 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Find JAR URL: file:/Users/zhouqian/IdeaProjects/seckill/target/classes/org/seckill/entity/SuccessKilled.class

11:13:38.095 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Not a JAR: file:/Users/zhouqian/IdeaProjects/seckill/target/classes/org/seckill/entity/SuccessKilled.class

11:13:38.096 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Reader entry: ����1T

11:13:38.098 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Checking to see if class org.seckill.entity.Seckill matches criteria [is assignable to Object]

11:13:38.099 [main] DEBUG org.apache.ibatis.io.ResolverUtil - Checking to see if class org.seckill.entity.SuccessKilled matches criteria [is assignable to Object]

11:13:38.101 [main] DEBUG o.m.spring.SqlSessionFactoryBean - Scanned package: 'org.seckill.entity' for aliases

11:13:38.207 [main] DEBUG o.m.spring.SqlSessionFactoryBean - Parsed configuration file: 'class path resource [mybatis-config.xml]'

11:13:38.418 [main] DEBUG o.m.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/Users/zhouqian/IdeaProjects/seckill/target/classes/mapper/SeckillDao.xml]'

11:13:38.446 [main] DEBUG o.m.spring.SqlSessionFactoryBean - Parsed mapper file: 'file [/Users/zhouqian/IdeaProjects/seckill/target/classes/mapper/SuccessKilledDao.xml]'

java.lang.NullPointerException

at org.seckill.dao.SeckillDaoTest.testQueryById(SeckillDaoTest.java:32)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)

at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)

at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)

at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)

at org.junit.runner.JUnitCore.run(JUnitCore.java:160)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)


Disconnected from the target VM, address: '127.0.0.1:50386', transport: 'socket'


写回答 关注

2回答

  • 石灬榴灬灬
    2018-04-02 16:30:59
    已采纳

    private SeckillDao seckillDao;上面的注解没写

    魄弹兽

    我写了@resource也是空指针异常

    2018-04-25 11:10:11

    共 2 条回复 >

  • 十一月一
    2018-04-02 16:35:15

    @Resource

    private SeckillDao seckillDao;

Java高并发秒杀API之业务分析与DAO层

Java实现高并发秒杀API的第一门课,还等什么,赶快来加入吧

87426 学习 · 496 问题

查看课程

相似问题