I keep getting a NoClassDefFoundError whenever I try to run this block of code
ClassTable.getInstance();
Here is the code for ClassTable
public class ClassTable
{
private static ClassTable classTable = new ClassTable();
private Map<String,Object> pricingTable;
private Date expiry;
private ClassTable()
{
this.expiry = this.getExpiry();
this.pricingTable = buildPricingTables();
}
private Date getExpiry()
{
return DateUtils.INSTANCE.getCutOff();
}
public static ClassTable getInstance()
{
return classTable;
}
}
I tried stepping into the method getInstance(); however, it throws the error instantly. Here is the stack trace
java.lang.NoClassDefFoundError: Could not initialize class helper.ClassTable
at models.SinglePaymentLoan.getPricingTable(SinglePaymentLoan.java:742)
at unit.SinglePaymentLoanTest.testPricingTable(SinglePaymentLoanTest.java:135)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:114)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:47)
at org.junit.rules.RunRules.evaluate(RunRules.java:18)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:58)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at play.test.TestEngine.run(TestEngine.java:112)
at controllers.TestRunner$1.doJobWithResult(TestRunner.java:71)
at controllers.TestRunner$1.doJobWithResult(TestRunner.java:1)
at play.jobs.Job.call(Job.java:146)
at play.jobs.Job$1.call(Job.java:66)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)
Note that it states that it cannot initialize the class. That indicates a problem with static initialization of the class.
You have one static variable classTable that attempts to create a new ClassTable instance.
private static ClassTable classTable = new ClassTable();
private Map<String,Object> pricingTable;
private Date expiry;
private ClassTable()
{
this.expiry = this.getExpiry();
this.pricingTable = buildPricingTables();
}
So now any error in creating a ClassTable instance will lead to a class initialization failure. You do 2 things in the constructor, each of which could fail.
You call getExpiry, which we see turns and calls DateUtils.INSTANCE.getCutOff(). You have not provided this code, so it is unclear what this does.
Finally, you call buildPricingTables(). Again, code has not been provided for this method.
So your 2 mostly likely culprits are DateUtils.INSTANCE.getCutOff() and buildPricingTables().
Related
This is my test case:
public class SearchStudentDetailsServiceImpl extends JunitCommonUtil {
#Test
public void searchStudentDetails()
{
StudentDetailsSearch studentDetailsSearch=new StudentDetailsSearch();
String sortFlag = "name";
UserService userService = (UserService) ServiceLocator.getInstance()
.getBean("userService");
StringSearchField srchFld = new StringSearchField();
srchFld.setValue("stu1");
studentDetailsSearch.setId(srchFld);
userService.searchStudentDetails(studentDetailsSearch, sortFlag);
assertEquals("Result", "stu1", studentDetailsSearch.getId());
}
}
The error i am getting while running junit test case:
java.lang.AssertionError: Result expected:<stu1> but was:<com.intertek.domain.StringSearchField#740edac1>
at org.junit.Assert.fail(Assert.java:93)
at org.junit.Assert.failNotEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:128)
at com.intertek.test.SearchStudentDetailsServiceImpl.searchStudentDetails(SearchStudentDetailsServiceImpl.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Actually I got the error:
I replaced
assertEquals("Result", "stu1", studentDetailsSearch.getId());
with
assertEquals("Result", "stu1", studentDetailsSearch.getId().getValue());
How to pass and set the ID in BetRecordAgEBR? Please Help me:) Thanks in advance. This is my only problem that encounter. The BetRecordAgEBR class is for may database using jpa.
if ("GR".equals(dataType)) {
ByteArrayInputStream bis = new ByteArrayInputStream(
aw.getBytes());
StreamSource ss = new StreamSource(bis);
brElement = (JAXBElement<T>) jaxbUnmarshaller
.unmarshal(ss, BetRecordAgGR.class);
ret2 = brElement.getValue();
counter++;
if (counter > 100) {
return brList;
}
/*
*
* String uuid1 = java.util.UUID.randomUUID()
* .toString(); dataOpe.setEntityManager(em);
* ret2.setID(uuid1); dataOpe.add(ret2);
*/
brList.add((T) ret2);
System.out.println(brList);
}
The Exception:
javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): us.kirin.bc.BetRecordAgGR
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:881)
at us.kirin.bc.service.DataService.add(DataService.java:40)
at us.kirin.bc.service.AGServiceTest.BetHistoryAGIN(AGServiceTest.java:255)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): us.kirin.bc.BetRecordAgGR
at org.hibernate.id.Assigned.generate(Assigned.java:52)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:852)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:826)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:830)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:875)
... 26 more
I change ret2 to BetRecordAgGR ret2;
if ("GR".equals(dataType)) {
ByteArrayInputStream bis = new ByteArrayInputStream(
aw.getBytes());
StreamSource ss = new StreamSource(bis);
brElement = (JAXBElement<T>) jaxbUnmarshaller
.unmarshal(ss, BetRecordAgGR.class);
ret2= (BetRecordAgGR) brElement.getValue();
counter++;
if (counter > 100) {
return brList;
}
String uuid1 = java.util.UUID.randomUUID()
.toString();
ret2.setID(uuid1);
dataOpe.setEntityManager(em);
dataOpe.add(ret2);
brList.add((T) ret2);
System.out.println(brList);
}
I have a Play 1.2.7 project, and need to do the following.
When a particular Article is updated the lastUpdate field is automatically set to the current time. Now related articles need to have their lastUpdate field set to the same as the edited article.
The super class of Article has the following:
#PrePersist
protected void onCreate() {
addDate = new Date();
if(autoLastUpdate) {
lastUpdate = addDate;
}
}
#PreUpdate
protected void onUpdate() {
if(autoLastUpdate) {
lastUpdate = new Date();
}
}
So in the Article class itself I have:
#PreUpdate
protected void onUpdate() {
super.onUpdate();
try{
this.editedBy = Security.connectedUser();
} catch (NullPointerException npe){
this.editedBy = null;
}
// Update all lastUpdate on other languages, needed for new selection of articles server side using lastUpdate.
for (Article article : getAllLanguages()) {
if (!article.id.equals(this.id) && !article.lastUpdate.equals(this.lastUpdate)) {
article.refresh();
article.setLastUpdate(this.lastUpdate);
article.save();
}
}
}
But this causes an stack overflow, as when article.save() is called, then actually it seems this.save() is called, as the procedure is run again on the current object:
java.lang.RuntimeException has been caught, java.lang.StackOverflowError
or a PersistenceException on the Article.save():
javax.persistence.PersistenceException has been caught, org.hibernate.HibernateException: Found shared references to a collection: models.Article.accessGroups
And I do not understand why. I have spend a few hours on this, as it seems to me simple problem, but cannot figure it out.
Can I not do a save() on another object in the #PreUdate? What way could I otherwise do this automatically on any change on the Article object?
[EDIT:]
An important item which I forgot:
public List<Article> getAllLanguages(boolean includeRemoved){
if(includeRemoved){
return find("commonId = ? AND commonId != 0 AND site.app = ?", this.commonId, this.site.app).fetch();
} else {
return find("commonId = ? AND commonId != 0 AND site.app = ? AND removed = ?", this.commonId, this.site.app, false).fetch();
}
}
I also tried to do the following instead of the above:
#PostUpdate
protected void afterUpdate(){
// Update all lastUpdate on other languages, needed for new selection of articles server side using lastUpdate.
for (Article anArticle : getAllLanguages()) {
Logger.debug("Article " + anArticle.id + " time " + anArticle.lastUpdate + " from afterUpdate of " + this.id + " " + this.lastUpdate);
if (!anArticle.id.equals(this.id) && anArticle.lastUpdate.before(this.lastUpdate)) {
//anArticle.refresh();
anArticle.setLastUpdate(this.lastUpdate);
Logger.debug("Going to save article " + anArticle.id + " from afterUpdate on " + this.id);
anArticle.save();
}
}
}
But now I get an error on my test runner, and I do not know what causes it:
A java.lang.IndexOutOfBoundsException has been caught, Index: 1, Size: 0
In /test/models/ArticleTest.java, line 786 :article1.save();
java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:962)
at play.db.jpa.JPABase._save(JPABase.java:41)
at play.db.jpa.GenericModel.save(GenericModel.java:204)
at models.Article.afterUpdate(Article.java:327)
at sun.reflect.GeneratedMethodAccessor571.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.ejb.event.BeanCallback.invoke(BeanCallback.java:37)
at org.hibernate.ejb.event.EntityCallbackHandler.callback(EntityCallbackHandler.java:94)
at org.hibernate.ejb.event.EntityCallbackHandler.postUpdate(EntityCallbackHandler.java:83)
at org.hibernate.ejb.event.EJB3PostUpdateEventListener.handlePostUpdate(EJB3PostUpdateEventListener.java:70)
at org.hibernate.ejb.event.EJB3PostUpdateEventListener.onPostUpdate(EJB3PostUpdateEventListener.java:62)
at org.hibernate.action.EntityUpdateAction.postUpdate(EntityUpdateAction.java:199)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:177)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:185)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:962)
at play.db.jpa.JPABase._save(JPABase.java:41)
at play.db.jpa.GenericModel.save(GenericModel.java:204)
at models.ArticleTest.testOnLastUpdatePropagationToOtherLanguages(ArticleTest.java:786)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:114)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:58)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at play.test.TestEngine.run(TestEngine.java:112)
at controllers.TestRunner.run(TestRunner.java:66)
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 play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:251)
at play.Invoker$Invocation.run(Invoker.java:278)
at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:229)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745
I write this code and I am comparing these two dates, but I get an extra -4:00, I am not sure where it is coming from as I specify that I want yyyy-MM-dd'T'HH:mm:ss which ends at the seconds, so I do not know how to get rid of the -4:00.
#Test
public void testOrder5() throws Exception {
Phone input = new DefaultPhone();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = new Date();
input.setCreatedDate(date);
Phoneobj output = Book.change(input);
assertThat(output.getCreatedDate(), is(sf.format(date)));
}
Error is
java.lang.AssertionError:
Expected: is "2013-10-09T14:27:10"
got: "2013-10-09T14:27:10-04:00"
at org.junit.Assert.assertThat(Assert.java:780)
at org.junit.Assert.assertThat(Assert.java:738)
at ca.on.oicr.DtosTest.testOrder5(DtosTest.java:64)
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:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
So I am not sure where the -4:00 comes from or how to get rid of it
public class Defaultphone implements Phone{
private Date createdDate;
#Override public Date getCreatedDate()
{
return createdDate;
}
#Override public void setCreatedDate(Date createdDate)
{
this.createdDate = createdDate;
}
}
then my change method consists of
public static PhoneObj change(Order from) {
PhoneObj phoneobj = new OrderDto();
if (from.getCreatedDate() != null) {
phoneobj.setCreatedDate(dateTimeFormatter.print(from.getCreatedDate().getTime()));
}
return phoneobj;
}
You have to change your pattern in yout test:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
With this pattern you will get the same format used in PhoneObj class.
Looks like output.getCreatedDate() is returning "2013-10-09T14:27:10-04:00".
I am sure if you debug output object of the line --> Phoneobj output = Book.change(input);
you will know the reason.
Please share the change Book.change(input), if you are unable to find.
Below link will give you dateTimeFormatter predefined formatter: http://download.java.net/jdk8/docs/api/java/time/format/DateTimeFormatter.html
I am trying to set up a test for a Resource:
#Override
protected void setUpResources() throws Exception {
when(ratingDao.getRating("karan")).thenReturn(rating);
addResource(new RatingResource(ratingDao));
}
#Test
public void testRatingsGetsTracks() throws Exception{
assertThat(client().resource("/ratings").queryParam("username", "karan").get(Rating.class)).isEqualTo(rating);
verify(ratingDao).getRating("karan");
}
However, I get a 204:
1 > GET /ratings?username=karan
1 >
23:18:24.705 [main] INFO c.s.j.a.c.filter.LoggingFilter - 1 * Server out-bound response
1 < 204
1 < Content-Type: application/json
1 <
23:18:24.708 [main] INFO c.s.j.t.f.s.c.i.InMemoryTestContainerFactory$InMemoryTestContainer - Stopping low level InMemory test container
com.sun.jersey.api.client.UniformInterfaceException: Client response status: 204
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:540)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:684)
at com.sun.jersey.api.client.WebResource.get(WebResource.java:191)
at RatingResourceTest.testRatingsGetsTracks(RatingResourceTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
I have no idea why. I have tested that a Rating can be serialized into a json.
Here is the implementation of the resource:
#Path("/ratings")
#Produces(MediaType.APPLICATION_JSON)
public class RatingResource {
private RatingDAO ratingDao;
public RatingResource(RatingDAO ratingDao) {
this.ratingDao = ratingDao;
}
#GET
#Timed
public Rating getRating(#QueryParam("username") String username) {
return ratingDao.getRating(username);
}
}
#Override
protected void setUpResources() throws Exception {
when(ratingDao.getRating("karan")).thenReturn(rating);
addResource(new RatingResource(ratingDao));
}
For some reason, the ratingDao mock was not being set up. When I moved that line to the setUp method, this worked.