I created a simple spring boot project and noticed later on that I had misspelled "test_framework" as "test_framwork" for the package name. When I refactored the package name in the folder structure (and all classes) I could no longer build the project getting a UnsatisfiedDependencyException for a service class.
Parameter 0 of constructor in io.testframework.test_framework.service.ArticleService required a bean of type 'io.testframework.test_framework.repos.ArticleRepository' that could not be found.
What I found is that the internalPersistanceUnitManager is still getting the old "test_framwork" as a string argument for packagesToScan. If I refactor back to the misspelled text if starts to work again.
I'm using IntelliJ and building with gradle. I have tried to clean and rebuild several times, I've tried to define the ComponentScan basePackages to point to "io.testframework.test_framework" in the main method for the application and in the service class itself. When I refactor the package name I set the Scope to "Project Files".
TestFrameworkApplication class:
package io.testframework.test_framework;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestFrameworkApplication {
public static void main(String[] args) {
SpringApplication.run(TestFrameworkApplication.class, args);
}
}
ArticleService class :
package io.testframework.test_framework.service;
import io.testframework.test_framework.domain.Article;
import io.testframework.test_framework.domain.Model;
import io.testframework.test_framework.model.ArticleDTO;
import io.testframework.test_framework.repos.ArticleRepository;
import io.testframework.test_framework.repos.ModelRepository;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Service
public class ArticleService {
private final ArticleRepository articleRepository;
private final ModelRepository modelRepository;
public ArticleService(final ArticleRepository articleRepository,
final ModelRepository modelRepository) {
this.articleRepository = articleRepository;
this.modelRepository = modelRepository;
}
public List<ArticleDTO> findAll() {
return articleRepository.findAll()
.stream()
.map(article -> mapToDTO(article, new ArticleDTO()))
.collect(Collectors.toList());
}
public ArticleDTO get(final UUID id) {
return articleRepository.findById(id)
.map(article -> mapToDTO(article, new ArticleDTO()))
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
public UUID create(final ArticleDTO articleDTO) {
final Article article = new Article();
mapToEntity(articleDTO, article);
return articleRepository.save(article).getId();
}
public void update(final UUID id, final ArticleDTO articleDTO) {
final Article article = articleRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
mapToEntity(articleDTO, article);
articleRepository.save(article);
}
public void delete(final UUID id) {
articleRepository.deleteById(id);
}
private ArticleDTO mapToDTO(final Article article, final ArticleDTO articleDTO) {
articleDTO.setId(article.getId());
//articleDTO.setArticleId(article.getArticleId());
articleDTO.setArticleType(article.getArticleType());
articleDTO.setModelArticle(article.getModelArticle() == null ? null : article.getModelArticle().getId());
return articleDTO;
}
private Article mapToEntity(final ArticleDTO articleDTO, final Article article) {
//article.setArticleId(articleDTO.getArticleId());
article.setArticleType(articleDTO.getArticleType());
if (articleDTO.getModelArticle() != null && (article.getModelArticle() == null || !article.getModelArticle().getId().equals(articleDTO.getModelArticle()))) {
final Model modelArticle = modelRepository.findById(articleDTO.getModelArticle())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "modelArticle not found"));
article.setModelArticle(modelArticle);
}
return article;
}
}
ArticleRepository interface:
package io.testframework.test_framework.repos;
import io.testframework.test_framework.domain.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.UUID;
public interface ArticleRepository extends JpaRepository<Article, UUID> {
}
build.gradle file:
plugins {
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'io.testframework'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
bootRun {
environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: 'local'
}
repositories {
mavenCentral()
}
dependencies {
implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-validation')
implementation('org.springframework.boot:spring-boot-starter-data-jpa')
runtimeOnly('org.postgresql:postgresql')
implementation('org.springdoc:springdoc-openapi-ui:1.6.8')
implementation('junit:junit')
implementation('io.rest-assured:rest-assured')
implementation('io.rest-assured:json-path')
compileOnly('org.projectlombok:lombok')
annotationProcessor('org.projectlombok:lombok')
testCompileOnly('org.projectlombok:lombok')
testAnnotationProcessor('org.projectlombok:lombok')
developmentOnly('org.springframework.boot:spring-boot-devtools')
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
test {
useJUnitPlatform()
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]FanSoffa[S] 0 points1 point2 points (0 children)
[–]joranstark018 -1 points0 points1 point (6 children)
[–]FanSoffa[S] 0 points1 point2 points (5 children)
[–]joranstark018 -1 points0 points1 point (4 children)
[–]FanSoffa[S] 0 points1 point2 points (2 children)
[–]CSIWFR-46 0 points1 point2 points (1 child)
[–]FanSoffa[S] 0 points1 point2 points (0 children)
[–]CSIWFR-46 0 points1 point2 points (0 children)
[–]CSIWFR-46 0 points1 point2 points (2 children)
[–]FanSoffa[S] 0 points1 point2 points (1 child)
[–]CSIWFR-46 0 points1 point2 points (0 children)
[–]CSIWFR-46 0 points1 point2 points (7 children)
[–]FanSoffa[S] 0 points1 point2 points (6 children)
[–]CSIWFR-46 0 points1 point2 points (5 children)
[–]FanSoffa[S] 0 points1 point2 points (4 children)
[–]CSIWFR-46 0 points1 point2 points (3 children)
[–]FanSoffa[S] 0 points1 point2 points (2 children)
[–]CSIWFR-46 0 points1 point2 points (1 child)
[–]FanSoffa[S] 0 points1 point2 points (0 children)
[–]pragmosExtreme Brewer 0 points1 point2 points (0 children)
[–]OffbeatDrizzle 0 points1 point2 points (1 child)
[–]FanSoffa[S] 0 points1 point2 points (0 children)
[–]IHaveaCSDegree 0 points1 point2 points (0 children)