title: Spring testing SpringBootTest annotation explanation and problem
body: <p>I'm new to testing and am currently testing services and controllers in the spring application.  </p>
  
  <p>When I started doing integration tests in the service layer, I posted an annotation  </p>
  
  <pre><code>@SpringBootTest (webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)   
  </code></pre>
  
  <p>and always got an error:</p>
  
  <pre><code>java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest (classes = ...) with your test
  </code></pre>
  
  <p>And then, as a solution, I set annotation like this:<br>
  <code>@SpringBootTest (classes = MyApplication.class)</code>  and everything worked ok.</p>
  
  <p>However now that I have started testing the controller layer and I need a TestRestTemplate:  </p>
  
  <pre><code>@Autowired
  private TestRestTemplate restTemplate;
  </code></pre>
  
  <p>I get the error :  </p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ftn.controllers.AddressControllerTest': Unsatisfied dependency expressed through field 'restTemplate'; 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 an autowire candidate. Dependency annotations: {@ org.springframework.beans.factory.annotation.Autowired (required = true)}
  </code></pre>
  
  <p>My test class: </p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = TicketServiceApplication.class)
  @Transactional
  @TestPropertySource("classpath:application-test.properties")
  public class AddressControllerTest {
  
      @Autowired
      private AddressRepository addressRepository;
  
      @Autowired
      private TestRestTemplate restTemplate;
  
      private String accessToken;
  
      private HttpHeaders headers = new HttpHeaders();
  
      @Before
      public void login() {
          ResponseEntity&lt;LoggedInUserDTO&gt; login = restTemplate.postForEntity("/login",  new LoginDTO("admin", "admin"), LoggedInUserDTO.class);
          accessToken = login.getBody().getToken();
          headers.add("Authorization", "Bearer "+accessToken);
      }
  
      @Test
      public void testGetAllAddresses() {
          ResponseEntity&lt;AddressDto[]&gt; responseEntity = restTemplate.getForEntity("/api/address", AddressDto[].class);
          AddressDto[] addressesDto = responseEntity.getBody();
          AddressDto a1 = addressesDto[1];
  
  
          assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
          assertNotNull(addressesDto);
          assertNotEquals(0, addressesDto.length);
          assertEquals(2, addressesDto.length);
          assertEquals(AddressConst.DB_STATE, a1.getState());
          assertEquals(AddressConst.DB_CITY, a1.getCity());
          assertEquals(AddressConst.DB_STREET, a1.getStreet());
          assertEquals(AddressConst.DB_NUM, a1.getNumber());
      }
  }
  </code></pre>
  
  <p>What is the right solution and what is the right way to do the integration tests properly?<br>
  Why makes me a problem to use webEnvironment in SpringBootTest?</p>
  
  <p>EDIT: Now when I remove @RunWith() anotation, TestRestTemlpate is null and I have NullPointerException in first line of test where I try to get response object.</p>
  
  <p>If anyone can help and explain a little bit about this, I'm sorry but I'm new to spring testing. Thank you!</p>