JUnit测试与调试

huangapple 未分类评论45阅读模式
英文:

Junit testing and debugging

问题

以下是翻译好的部分:

我正在尝试运行一些 JUnit 测试,但在几个测试中遇到了以下错误。这些是我遇到的错误信息。

    [junit] 无法获取属性值
        [junit] java.lang.IllegalStateException: 无法获取属性值
        [junit] 	at org.hibernate.validator.ClassValidator.getMemberValue(ClassValidator.java:539)
        [junit] 	at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:384)
        [junit] 	at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:352)
        [junit] 	at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:139)
        [junit] 	at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172)
        ...
        [junit] Caused by: java.lang.NullPointerException
        [junit] 	at org.unavco.pbo.mdm.model.Station.validateProjectDate(Station.java:740)

在错误信息中提到的代码行:

    @AssertTrue
    	private boolean validateProjectDate() {
    		if (projectDate.before(this.stationProject.getStartDate()))  {
    			return false;
    		}
    
    		if (projectDate.after(this.stationProject.getEndDate())) {
    			return false;
    		}
    
    		return true;
    	}

受影响的测试方法:

public void testStationWithFourcharInherit() throws Exception {
	
	String fourCharId = "IOA2";
	Date projectDate = new Date();

	endTransaction();
	Locale locale = createAndSaveLocale(fourCharId);
	Site site = createAndSaveSite(locale);
	Station station = createAndSaveStation(site, fourCharId);
	startNewTransaction();
	
	verifyStationMatches(station, fourCharId);
}

测试中涉及的函数:

protected Station createAndSaveStation(Site site, String fourCharId) {
		Station station = new Station();
		station.setLongName("MyStationsLongName");
		if (fourCharId != null) {
			station.setFourCharId(fourCharId);
		}
   	 	StationType stnType = (StationType) stationDao.getObject(StationType.class, 1L);
   	 	station.setStationType(stnType);
   	 	StationBuildStatus bldStat = (StationBuildStatus) stationDao.getObject(StationBuildStatus.class, 1L);
   	 	station.setStationBuildStatus(bldStat);
   	 	Project stnProject = (Project) stationDao.getObject(Project.class, 1L);
   	 	station.setStationProject(stnProject);
   	 	station.setCommsType(CommsType.CDMA);
   	 	station.setLatitude(31.009);
   	 	station.setLongitude(128.9930);
   	 	station.setElevation(890.3);
   	 	station.setStationStatus(StationOperationalStatus.Operable);
   	 	station.setLongName("long name");
   	 	station.setInstallDate(new Date());
		station.setProjectDate(new Date());
		site.addStation(station);
		stationDao.saveStation(station);
		stationDao.validateStartDate(station);
		return station;
	}

有人知道如何修复这个错误吗?

请注意,由于您要求只返回翻译好的部分,我只提供了代码和文本的翻译,没有添加其他内容。如果您需要进一步的帮助或解释,请随时提问。

英文:

I am trying to run a few junit tests and I come across this error in several tests.
These are the errors I am getting.

[junit] Could not get property value
    [junit] java.lang.IllegalStateException: Could not get property value
    [junit] 	at org.hibernate.validator.ClassValidator.getMemberValue(ClassValidator.java:539)
    [junit] 	at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:384)
    [junit] 	at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:352)
    [junit] 	at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:139)
    [junit] 	at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172)
    [junit] 	at org.hibernate.action.EntityInsertAction.preInsert(EntityInsertAction.java:177)
    [junit] 	at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:72)
    [junit] 	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
    [junit] 	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:260)
    [junit] 	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:179)
    [junit] 	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    [junit] 	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    [junit] 	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
    [junit] 	at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
    [junit] 	at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:408)
    [junit] 	at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:375)
    [junit] 	at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:738)
    [junit] 	at org.unavco.pbo.mdm.dao.hibernate.BaseDaoHibernate.saveObject(BaseDaoHibernate.java:52)
    [junit] 	at org.unavco.pbo.mdm.dao.hibernate.StationDaoHibernate.saveStation(StationDaoHibernate.java:201)
    [junit] 	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    [junit] 	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
    [junit] 	at com.sun.proxy.$Proxy30.saveStation(Unknown Source)
    [junit] 	at org.unavco.pbo.mdm.dao.StationFourcharBackwardCompatTest.createAndSaveStation(StationFourcharBackwardCompatTest.java:228)
    [junit] 	at org.unavco.pbo.mdm.dao.StationFourcharBackwardCompatTest.testStationWithFourcharInherit(StationFourcharBackwardCompatTest.java:49)
    [junit] 	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:79)
    [junit] Caused by: java.lang.IllegalStateException: Unable to invoke validateProjectDate
    [junit] 	at org.hibernate.annotations.common.reflection.java.JavaXMethod.invoke(JavaXMethod.java:66)
    [junit] 	at org.hibernate.validator.ClassValidator.getMemberValue(ClassValidator.java:536)
    [junit] Caused by: java.lang.reflect.InvocationTargetException
    [junit] 	at org.hibernate.annotations.common.reflection.java.JavaXMethod.invoke(JavaXMethod.java:57)
    [junit] Caused by: java.lang.NullPointerException
    [junit] 	at org.unavco.pbo.mdm.model.Station.validateProjectDate(Station.java:740)

Code the last line it is refering to.

@AssertTrue
	private boolean validateProjectDate() {
		if (projectDate.before(this.stationProject.getStartDate()))  {
			return false;
		}

		if (projectDate.after(this.stationProject.getEndDate())) {
			return false;
		}

		return true;
	}

The test that is affected.

public void testStationWithFourcharInherit() throws Exception {

String fourCharId = "IOA2";
Date projectDate = new Date();

endTransaction();
Locale locale = createAndSaveLocale(fourCharId);
Site site = createAndSaveSite(locale);
Station station = createAndSaveStation(site, fourCharId);
startNewTransaction();

verifyStationMatches(station, fourCharId);

}

The refered function in test.

protected Station createAndSaveStation(Site site, String fourCharId) {
		Station station = new Station();
		station.setLongName("MyStationsLongName");
		if (fourCharId != null) {
			station.setFourCharId(fourCharId);
		}
   	 	StationType stnType = (StationType) stationDao.getObject(StationType.class, 1L);
   	 	station.setStationType(stnType);
   	 	StationBuildStatus bldStat = (StationBuildStatus) stationDao.getObject(StationBuildStatus.class, 1L);
   	 	station.setStationBuildStatus(bldStat);
   	 	Project stnProject = (Project) stationDao.getObject(Project.class, 1L);
   	 	station.setStationProject(stnProject);
   	 	station.setCommsType(CommsType.CDMA);
   	 	station.setLatitude(31.009);
   	 	station.setLongitude(128.9930);
   	 	station.setElevation(890.3);
   	 	station.setStationStatus(StationOperationalStatus.Operable);
   	 	station.setLongName("long name");
   	 	station.setInstallDate(new Date());
		station.setProjectDate(new Date());
		site.addStation(station);
		stationDao.saveStation(station);
		stationDao.validateStartDate(station);
		return station;
	}

Does anyone know how to fix this error?

huangapple
  • 本文由 发表于 2020年4月10日 06:46:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/61131422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定