1

I am runnumg a Spring Boot test using a TestRestTemplate. My code has the following autowire:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)
class TestMyService
{
    ...
   @Autowired
   private TestRestTemplate temp;
   private int port = 8080;
   ...

and my test does a basic POST using the temp object:

String json = temp..postForObject("http://localhost:"+port,params,String.class);

my test compiles and runs without problems, but when I try to compile the Jar file using:

mvnw clean package

the compilation fails with the following failure:

[ERROR] basicLetterService(com.factor3.app.counter.TestMyService)  Time elapsed: 0.001 s  <<< ERROR!
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.factor3.app.counter.TestMyService': Unsatisfied dependency expressed through field 'temp'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Note that the failure is due to Boot being unable to autowire the TestRestTemplate object that was declared and successfully compiled to run the test.

Why is this occurring? Is there some configuration detail I need to do?

Factor Three
  • 2,094
  • 5
  • 35
  • 51

0 Answers0