Eclipse throws java.lang.NullPointerException when

2020-07-02 08:16发布

问题:

When I running my junit test, console print this 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

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:

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.

My Develop Environment is:
Mac OS X Yosetime 10.10.4
Eclipse Java EE IDE for Web Developers(Version: Mars Release (4.5.0))
Maven plugin(install from Eclipse->Install New Software)

I guess this is a conflict between eclipse junit and project junit config, but I'm not sure.

How to solve the problem, thanks.

回答1:

Came across the same NPE on Eclipse Mars when executing (single) JUnit tests.

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). With this library, I could execute all tests from within Mars.

In the other workspace/project where I had the problem before I am using an older version of JUnit.

I think this old JUnit version is the problem, will have to upgrade to a newer version.



回答2:

Yep. I raised a bug against Eclipse, and they advised changed my dependency to a newer version of JUnit.

I went from JUnit 4.4 to JUnit 4.9, and it started working again.



回答3:

Do this :

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