尝试在junit测试中持久化对象时发生NullPointerException

时间:2016-03-08 16:32:18

标签: java spring junit persistence spring-data-neo4j

我有几个测试,其中持久化对象工作完全正常,但是,在这个特定的Junit测试中,当我尝试运行它时出现以下错误。有什么想法吗?

错误跟踪

    java.lang.NullPointerException
at org.springframework.data.neo4j.aspects.support.node.Neo4jNodeBacking.ajc$interMethod$org_springframework_data_neo4j_aspects_support_node_Neo4jNodeBacking$org_springframework_data_neo4j_aspects_core_NodeBacked$persist(Neo4jNodeBacking.aj:133)
at emlab.gen.domain.gis.Zone.persist(Zone.java:1)
at org.springframework.data.neo4j.aspects.support.node.Neo4jNodeBacking.ajc$interMethodDispatch1$org_springframework_data_neo4j_aspects_support_node_Neo4jNodeBacking$org_springframework_data_neo4j_aspects_core_NodeBacked$persist(Neo4jNodeBacking.aj)
at emlab.gen.role.RenewableSupportSchemeTenderTest.setUpBeforeClass(RenewableSupportSchemeTenderTest.java:83)
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:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
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)

Junit测试如下:

package emlab.gen.role;

import static org.junit.Assert.assertEquals;

import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import emlab.gen.domain.agent.EnergyProducer;
import emlab.gen.domain.agent.Regulator;
import emlab.gen.domain.gis.Zone;
import emlab.gen.domain.market.electricity.ElectricitySpotMarket;
import emlab.gen.domain.market.electricity.Segment;
import emlab.gen.domain.market.electricity.SegmentLoad;
import emlab.gen.domain.policy.renewablesupport.RenewableSupportSchemeTender;
import emlab.gen.domain.policy.renewablesupport.RenewableTarget;
import emlab.gen.domain.technology.PowerGeneratingTechnology;
import emlab.gen.domain.technology.PowerGridNode;
import emlab.gen.domain.technology.PowerPlant;
import emlab.gen.repository.MarketRepository;
import emlab.gen.repository.PowerGenerationTechnologyTargetRepository;
import emlab.gen.repository.PowerPlantRepository;
import emlab.gen.role.tender.CalculateRenewableTargetForTenderRole;
import emlab.gen.trend.TimeSeriesCSVReader;
import emlab.gen.trend.TriangularTrend;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/emlab-gen-test-context.xml" })
@Transactional
public class RenewableSupportSchemeTenderTest {   
   Logger logger = Logger.getLogger(RenewableSupportSchemeTenderTest.class);

   RenewableSupportSchemeTender schemeA = new RenewableSupportSchemeTender();

            @Autowired
            PowerPlantRepository powerPlantRepository;

            @Autowired
            PowerGenerationTechnologyTargetRepository powerGenerationTechnologyTargetRepository;

            @Autowired
            MarketRepository marketRepository;

            @Autowired
            CalculateRenewableTargetForTenderRole calculateRenewableTargetForTenderRole;

            private static final double DELTA = 1e-6;

            @BeforeClass
            @Transactional
            public static void setUpBeforeClass() throws Exception {
                Zone zoneA = new Zone();
                zoneA.setName("ZoneA");
                zoneA.persist();

                PowerGeneratingTechnology wind = new PowerGeneratingTechnology();

                wind.setCapacity(200);
                wind.setExpectedLeadtime(2);
                wind.setExpectedPermittime(1);
                wind.setIntermittent(true);
                wind.setExpectedLifetime(20);
                wind.setPeakSegmentDependentAvailability(0.4);
                wind.setBaseSegmentDependentAvailability(0.1);
                wind.setName("Wind");
                wind.persist();

                PowerGeneratingTechnology pv = new PowerGeneratingTechnology();
                pv.setName("PV");

                pv.setCapacity(150);

                pv.setFixedOperatingCostModifierAfterLifetime(0);
                pv.setExpectedLeadtime(1);
                pv.setExpectedPermittime(0);
                pv.setIntermittent(true);
                pv.setExpectedLifetime(15);
                pv.setPeakSegmentDependentAvailability(0.5);
                pv.setBaseSegmentDependentAvailability(0.1);
                pv.persist();

                Zone zoneB = new Zone();
                zoneB.setName("ZoneB");
                zoneB.persist();

                PowerGridNode powerGridNodeA = new PowerGridNode();
                powerGridNodeA.setZone(zoneA);
                powerGridNodeA.setCapacityMultiplicationFactor(1.0);
                powerGridNodeA.persist();
                PowerGridNode powerGridNodeB = new PowerGridNode();
                powerGridNodeB.setZone(zoneB);
                powerGridNodeB.setCapacityMultiplicationFactor(1.0);
                powerGridNodeB.persist();

                Segment S1 = new Segment();
                S1.setLengthInHours(20);
                S1.persist();

                Segment S2 = new Segment();
                S2.setLengthInHours(30);
                S2.persist();

                SegmentLoad SG1 = new SegmentLoad();
                SG1.setSegment(S2);
                SG1.setBaseLoad(2000);
                SG1.persist();

                SegmentLoad SG2 = new SegmentLoad();
                SG2.setSegment(S2);
                SG2.setBaseLoad(3000);

                // SegmentLoad SG3 = new SegmentLoad();
                // SG3.setSegment(S1);
                // SG3.setBaseLoad(3700);

                // SegmentLoad SG4 = new SegmentLoad();
                // SG4.setSegment(S1);
                // SG4.setBaseLoad(4000);

                SG2.persist();
                // SG3.persist();
                // SG4.persist();

                Set<SegmentLoad> segmentLoads1 = new HashSet<SegmentLoad>();
                segmentLoads1.add(SG1);
                segmentLoads1.add(SG2);

                TriangularTrend demandGrowthTrend = new TriangularTrend();
                demandGrowthTrend.setMax(2);
                demandGrowthTrend.setMin(1);
                demandGrowthTrend.setStart(1);
                demandGrowthTrend.setTop(1);
                demandGrowthTrend.persist();

                ElectricitySpotMarket marketA = new ElectricitySpotMarket();
                marketA.setName("marketA");
                marketA.setLoadDurationCurve(segmentLoads1);
                marketA.setDemandGrowthTrend(demandGrowthTrend);
                marketA.setZone(zoneA);
                marketA.persist();

                ElectricitySpotMarket marketB = new ElectricitySpotMarket();
                marketB.setName("marketB");
                marketB.setZone(zoneB);
                marketB.persist();

                EnergyProducer energyProducer1 = new EnergyProducer();
                EnergyProducer energyProducer2 = new EnergyProducer();

                PowerPlant windTurbineA1 = new PowerPlant();
                windTurbineA1.specifyAndPersist(-3, energyProducer1, powerGridNodeA, wind);

                PowerPlant windTurbineA2 = new PowerPlant();
                windTurbineA2.specifyAndPersist(-4, energyProducer2, powerGridNodeA, wind);

                PowerPlant windTurbineB1 = new PowerPlant();
                windTurbineB1.specifyAndPersist(5, energyProducer1, powerGridNodeB, wind);

                PowerPlant windTurbineB2 = new PowerPlant();
                windTurbineB2.specifyAndPersist(4, energyProducer2, powerGridNodeB, wind);

                PowerPlant pvA1 = new PowerPlant();
                pvA1.specifyAndPersist(-2, energyProducer1, powerGridNodeA, pv);

                PowerPlant pvA2 = new PowerPlant();
                pvA2.specifyAndPersist(-3, energyProducer2, powerGridNodeA, pv);

                PowerPlant pvB1 = new PowerPlant();
                pvB1.specifyAndPersist(5, energyProducer1, powerGridNodeB, pv);

                PowerPlant pvB2 = new PowerPlant();
                pvB2.specifyAndPersist(5, energyProducer2, powerGridNodeB, pv);

                Regulator regulatorA = new Regulator();
                regulatorA.setZone(zoneA);
                regulatorA.setNumberOfYearsLookingBackToForecastDemand(3);
                regulatorA.persist();

                Set<PowerGeneratingTechnology> setPV = new HashSet<PowerGeneratingTechnology>();
                setPV.add(pv);

                RenewableSupportSchemeTender schemeA = new RenewableSupportSchemeTender();
                schemeA.setRegulator(regulatorA);
                schemeA.setFutureTenderOperationStartTime(5);
                schemeA.setTechnologySpecificityEnabled(true);
                schemeA.setPowerGeneratingTechnologiesEligible(setPV);
                schemeA.persist();

                TimeSeriesCSVReader readerRenTarget = new TimeSeriesCSVReader();
                readerRenTarget.setDelimiter(",");
                readerRenTarget.setVariableName("nl_dummyTargetPV");
                readerRenTarget.setFilename("data/nodeAndTechSpecificPotentials.csv");
                readerRenTarget.persist();

                RenewableTarget targetPV = new RenewableTarget();
                targetPV.setRegulator(regulatorA);
                targetPV.setPowerGeneratingTechnology(pv);
                targetPV.setTargetTechnologySpecific(true);
                targetPV.setYearlyRenewableTargetTimeSeries(readerRenTarget);
                targetPV.persist();

                System.out.println("hello");

            }

            @Test
            public void calculateRenewableTargetForTenderRoleTest() {
                calculateRenewableTargetForTenderRole.act(schemeA);
                double expectedGeneration = 0d;
                assertEquals("Expected Generation", expectedGeneration, schemeA.getAnnualExpectedConsumption(), DELTA);
            }

            @Test
            public void testFutureTimePoint() {
                assertEquals("FutureTimePoint", 5, schemeA.getFutureTenderOperationStartTime());
            }

        }

1 个答案:

答案 0 :(得分:0)

@Transactional不会处理静态方法。 你有

@BeforeClass
@Transactional
public static void setUpBeforeClass()

没有为您的设置方法创建任何交易。我不确定你为什么没有得到NotInTransactionException,但这可能会导致它。 在这种情况下,使该方法成为非静态@Before注释方法可能就足够了。