Skip to content

Improve error reporting when @JsonTest is used with @SpringBootTest #9592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chinfeng opened this issue Jun 25, 2017 · 5 comments
Closed

Improve error reporting when @JsonTest is used with @SpringBootTest #9592

chinfeng opened this issue Jun 25, 2017 · 5 comments
Labels
status: duplicate A duplicate of another issue

Comments

@chinfeng
Copy link

chinfeng commented Jun 25, 2017

version: 1.5.4

Following the reference guide to use json in my test:

@RunWith(SpringRunner.class)
@JsonTest
@SpringBootTest
public class MyTest {
  @Autowired
  private JacksonTester<MyObject> json;

  @Test
  public void test () {
    json.read("classpath:expected.json");  // exception being thrown on this line
  }
}

It throws:

java.lang.IllegalStateException: Uninitialized JsonMarshalTester (ResourceLoadClass is null)
	at org.springframework.util.Assert.state(Assert.java:70)
	at org.springframework.boot.test.json.AbstractJsonMarshalTester.verify(AbstractJsonMarshalTester.java:314)
	at org.springframework.boot.test.json.AbstractJsonMarshalTester.write(AbstractJsonMarshalTester.java:131)
	at com.stonewomb.cnic.test.WorkOrderTest.testService(WorkOrderTest.java:91)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	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.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	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:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	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:191)
	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:678)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 25, 2017
@snicoll
Copy link
Member

snicoll commented Jun 25, 2017

TL;DR: remove @SpringBootTest or replace @JsonTest by @AutoConfigureJsonTesters

Following the reference guide to use json in my test:

The reference guide doesn't mention anywhere the use of @SpringBootTest with @JsonTest. it's one or the other: either you're slicing your app and only focus on json (the latter) or you write an integration test with everything (the former).

Actually there's a note right before that section

It’s also possible to use the @AUTOCONFIGURE…​ annotations with the standard @SpringBootTest annotation. You can use this combination if you’re not interested in ‘slicing’ your application but you want some of the auto-configured test beans.

Having said that, we need to improve the error reporting when two conflicting test bootstrap annotations are used and we can use that issue to track that.

@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 25, 2017
@snicoll snicoll changed the title JacksonTester throws "ResourceLoadClass is null" Improve error reporting when @JsonTest is used with @SpringBootTest Jun 25, 2017
@chinfeng
Copy link
Author

chinfeng commented Jun 28, 2017

Those annotation combinations are really confused me. Thanks to @snicoll .

But replacing @JsonTest with @AutoConfigureJsonTesters, It throws another exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.json.JacksonTester<com.stonewomb.entity.MyObject>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	... 29 common frames omitted

@philwebb
Copy link
Member

@chinfeng You might be hitting #9515, can you try the latest 1.5.5 SNAPSHOT?

@chinfeng
Copy link
Author

Thanks @philwebb . Problem solved.

It seems JacksonTester is not ready.

@mbhave
Copy link
Contributor

mbhave commented Oct 30, 2018

Closing in favor of #14981.

@mbhave mbhave closed this as completed Oct 30, 2018
@mbhave mbhave added status: duplicate A duplicate of another issue and removed type: enhancement A general enhancement labels Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants