I have this code.
@RunWith(SpringRunner.class)
@SpringBootTest(
webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = ApiDbApplication.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestPropertySource(
locations = "classpath:application.yml")
@ExtendWith(SpringExtension.class)
@Transactional
public class LocationIT {
@MockBean
CompanyRepository companyRepository;
@MockBean
ShipmentRepository shipmentRepository;
@MockBean
ContactRepository contactRepository;
private LocationController locationController;
private LocationService locationService;
@Autowired
LocationRepository locationRepository;
@LocalServerPort
private int port;
TestRestTemplate restTemplate = new TestRestTemplate();
HttpHeaders headers = new HttpHeaders();
@Before
public void setup() {
locationService = new LocationService(locationRepository);
this.locationController = new LocationController(locationService);
}
@Test
public void testAddLocation() {
ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule());
;
Location location = Location.builder()
.id(Long.valueOf(7))
.city("Fayetteville")
.lat(32.33)
.lon(37.49)
.name("Big place")
.State("Arkansas").build();
ResponseEntity<String> responseEntity = this.restTemplate
.postForEntity("http://localhost:" + port + "/api/location/save", location, String.class);
ResponseEntity<List<Location>> results = restTemplate.exchange("http://localhost:" + port + "/api/location/list",
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Location>>(){});
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertEquals(HttpStatus.OK, results.getStatusCode());
assertEquals(Collections.singletonList(location), results.getBody());
}
}
Whenever I run the test. My location repository is null and I have the @Repository annotation. This is the error I get: No qualifying bean of type 'com.example.apidb.location.LocationRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
I want to hit the endpoint using restTemplate, so I would rather not use @DataJPATest
.
This question is similar: How can I use @SpringBootTest(webEnvironment) with @DataJpaTest?