简体   繁体   English

Eclipse在运行Spring JUnit时抛出java.lang.NullPointerException

[英]Eclipse throws java.lang.NullPointerException when running Spring JUnit

When I running my junit test, console print this Excpetion: 当我运行junit测试时,控制台打印此Excpetion:

java.lang.NullPointerException
at org.eclipse.jdt.internal.junit4.runner.SubForestFilter.shouldRun(SubForestFilter.java:81)
at org.junit.internal.runners.JUnit4ClassRunner.filter(JUnit4ClassRunner.java:110)
at org.junit.runner.manipulation.Filter.apply(Filter.java:47)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:34)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

And the Base test object like this 而Base测试对象就像这样

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
            "classpath*:config/spring/appcontext-*.xml",
            "classpath*:config/sqlmap/*.xml"
})
@TransactionConfiguration(defaultRollback = false)
@Transactional
public abstract class AbstractTestObject {
    static {
        //DOMConfigurator.configure("target/classes/log/log4j.xml");
    }
}

And the JUnit Method like this: 和JUnit方法是这样的:

public class StaffInfoServiceImplTest extends AbstractTestObject{
    @Autowired
    StaffInfoServiceImpl service;

    @Test
    public void insertTest() {
        StaffInfo bo = new StaffInfo();
        bo.setEmail("a@b.com");
        bo.setEntId(1);
        bo.setEntStaffNum("000XXXX");
        bo.setName("julia");
        bo.setPhone("00000000");
        bo.setSerialNum("SD12233KSRONDU189342ND");
        bo.setSuperior(2321);
        bo.setDepartment("Sale");
        bo.setSingleCredit(BigDecimal.valueOf(100));
        bo.setMonthlyCredit(BigDecimal.valueOf(10000));
        SLTContext c = new SLTContext();
        c.setOperator("0000001");
        service.insertStaffInfo(bo, c);
    }
}

My project is Java EE project, using maven to manage lib, using spring manage beans. 我的项目是Java EE项目,使用maven来管理lib,使用spring manage beans。

My Develop Environment is: 我的发展环境是:
Mac OS X Yosetime 10.10.4 Mac OS X Yosetime 10.10.4
Eclipse Java EE IDE for Web Developers(Version: Mars Release (4.5.0)) 面向Web开发人员的Eclipse Java EE IDE(版本:Mars Release(4.5.0))
Maven plugin(install from Eclipse->Install New Software) Maven插件(从Eclipse安装 - >安装新软件)

I guess this is a conflict between eclipse junit and project junit config, but I'm not sure. 我想这是eclipse junit和项目junit配置之间的冲突,但我不确定。

How to solve the problem, thanks. 如何解决问题,谢谢。

Came across the same NPE on Eclipse Mars when executing (single) JUnit tests. 执行(单个)JUnit测试时,在Eclipse Mars上遇到相同的NPE。

I tried to create a completely new workspace and added some test classes + test methods, but used the JUnit library shipped with Eclipse Mars (version 4.12). 我尝试创建一个全新的工作区并添加了一些测试类+测试方法,但使用了Eclipse Mars附带的JUnit库(版本4.12)。 With this library, I could execute all tests from within Mars. 有了这个库,我可以在Mars内部执行所有测试。

In the other workspace/project where I had the problem before I am using an older version of JUnit. 在我使用旧版JUnit之前遇到问题的其他工作区/项目中。

I think this old JUnit version is the problem, will have to upgrade to a newer version. 我认为这个旧的JUnit版本是问题,必须升级到更新的版本。

Yep. 是的。 I raised a bug against Eclipse, and they advised changed my dependency to a newer version of JUnit. 我提出了一个针对Eclipse的错误,他们建议将我的依赖项更改为更新版本的JUnit。

I went from JUnit 4.4 to JUnit 4.9, and it started working again. 我从JUnit 4.4转到JUnit 4.9,它又开始工作了。

Do this : 做这个 :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/applicationContext.xml")
@WebAppConfiguration
public class StaffInfoServiceImplTest { ..Your Code.. }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM