Below is my DTO class:
@Entity
@Data
@Table(name = "carservice")
public class CarService {
// public static void main(String[] args) {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
long serviceCode;
@Column(name = "service")
private String service;
@Column(name = "hatchback")
private String hatchback;
@Column(name = "sedan")
private String sedan;
@Column(name = "suv")
private String suv;
public CarService(long serviceCode, String service, String hatchback, String sedan, String suv) {
super();
this.serviceCode = serviceCode;
this.service = service;
this.hatchback = hatchback;
this.sedan = sedan;
this.suv = suv;
}
public long getServiceCode() {
return serviceCode;
}
public void setServiceCode(long serviceCode) {
this.serviceCode = serviceCode;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getHatchback() {
return hatchback;
}
public void setHatchback(String hatchback) {
this.hatchback = hatchback;
}
public String getSedan() {
return sedan;
}
public void setSedan(String sedan) {
this.sedan = sedan;
}
.....
My controller is as follows:
@CrossOrigin(origins = "http://localhost:8081")
@RestController
@RequestMapping("/api")
public class CarServiceController {
@Autowired
CarServiceRepository CarServiceRepository;
@GetMapping("/CarServices/{id}")
public ResponseEntity<CarService> getCarServiceById(@PathVariable("id") long id) {
List<CarService> CarServiceData = CarServiceRepository.findByServiceCode(id);
if (CarServiceData!=null) {
return new ResponseEntity<>(CarServiceData.get(0), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
And the repository class is as follows:
@Repository
public interface CarServiceRepository extends JpaRepository<CarService, Long> {
@Query(value = "SELECT * FROM CARSERVICE", nativeQuery = true)
List<CarService> findByServiceCode(Long id);
// List<CarService> findByTitleContaining(Long id);
}
When I am running the maven install command , the tests are failing and the error in surefire-reports are as follows:
-------------------------------------------------------------------------------
Test set: com.example.demo.CarServiceApplicationTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.445 s <<< FAILURE! - in com.example.demo.CarServiceApplicationTests
contextLoads Time elapsed: 0.036 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@7a78d380 testClass = com.example.demo.CarServiceApplicationTests, locations = [], classes = [com.example.demo.CarServiceApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5082d622, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@4278284b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@44c73c26, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@9da1, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@1ab06251, org.springframework.boot.test.context.SpringBootTestAnnotation@8dc993d1], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'carServiceController': Unsatisfied dependency expressed through field 'CarServiceRepository': Error creating bean with name 'carServiceRepository' defined in com.example.demo.repository.CarServiceRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.demo.model.CarService
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carServiceRepository' defined in com.example.demo.repository.CarServiceRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.demo.model.CarService
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.demo.model.CarService
I've tried many solutions found on the net regarding similar issues but didn't solve my issue. Please help. Thanks in advance!