-1
我們已經通過springframework.test.context.junit4
爲功能創建了測試。java.lang.AssertionError throwing null
下面是測試案例之一:
測試類
@Test
public void testAuthorizedPut() throws Exception {
ThreadContext.bind(subject);
when(subject.isPermitted(getPlural() + ":put")).thenReturn(true);
when(subject.isPermitted(getPlural() + ":get")).thenReturn(true);
assertThat(adminTemplate().exchange(getTestHost() + "/api/1/" + getPlural() + "/45454-67567d-f5224666a9", HttpMethod.PUT, new HttpEntity<Object>(getInstance(),getStandardHeaders()),entityClass).getBody(), equalTo(getInstance()));
}
當代碼移動到資源是用來模擬類。像上面的測試代碼將移動到PUT資源的要求:
MyResource類
@ApiOperation(value = "Update MyResource definition", notes = "Enter the id of the MyResource definition, returns updated MyResource definition", response = MyResource.class)
@Path("/{id}")
@PUT
@Timed
@Override
public MyResource update(@PathParam("id") String id, MyResource newInstance) {
return super.update(id, newInstance);
}
當我們運行上面testAuthorizedPut書面測試,然後扔了以下錯誤:
java.lang.AssertionError:
Expected: <MyResource(id=null, metadataTemplate=null, name=null, shortLabel=null, description=null, embedInObject=false, optional=false, type=null, possibleValues=null)>
but: was null
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at org.openskye.resource.AbstractUpdatableResourceTestBase.testAuthorizedPut(AbstractUpdatableResourceTestBase.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
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:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:121)
但是當我寫資源類PUT爲,意思是返回newInstance分開解決上述錯誤:
public MyResource update(@PathParam("id") String id, MyResource newInstance) {
super.update(id, newInstance);
return newInstance;
}
那麼返回newInstance與超級調用或單獨調用有什麼區別。如果沒有以上更改,這個錯誤如何解決?