加载应用程序上下文失败 (Spring-boot/Repository)

15 浏览
0 Comments

加载应用程序上下文失败 (Spring-boot/Repository)

我正在尝试测试我创建的一个数据库类,但是返回了错误信息"Failed to load ApplicationContext..."。我该如何解决这个问题?

错误信息:

java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@1fde0371 testClass = com.madi.StudentDataRestApi.ControllerInputTest, locations = [], classes = [com.madi.StudentDataRestApi.StudentDataRestApiApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true"], contextCustomizers = [[ImportsContextCustomizer@70c0a3d5 key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.sql.init.SqlInitializationAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@5e7cd6cc, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2dfaea86, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@210ab13f, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@9da1, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@90e4fd58, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@49c90a9c, org.springframework.boot.test.context.SpringBootTestAnnotation@1ad92308], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
...

依赖:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        3.0.2
         
    
    com.madi
    StudentDataRestApi
    0.0.1-SNAPSHOT
    StudentDataRestApi
    Java course final project
    
        19
    
    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.mysql
            mysql-connector-j
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-test-autoconfigure
            3.0.2
            test
        
        
            junit
            junit
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

我已经创建了一个Spring Boot的Java类,用于将学生对象存储在数据库中。我正在尝试测试输入和查找功能,但是遇到了错误。

编辑后的代码:

package com.medi.StudentDataRestApi;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ActiveProfiles;
@DataJpaTest
@ActiveProfiles("test")
public class InputFindTest {
    @Autowired
    private MainController mainController;
    @Autowired
    private StudentRepo studentRepo;
    @Test
    void InputSearchTest() {
        Student input = new Student(132123, "Example", "Anon", "CST", 7f);
        mainController.addNewStud(input);
        Iterable response = mainController.getStud(130267);
        assertEquals(input, response);
    }
}

新的错误信息:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.medi.StudentDataRestApi.InputFindTest': Unsatisfied dependency expressed through field 'mainController': No qualifying bean of type 'com.medi.StudentDataRestApi.MainController' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

控制器代码:

package com.medi.StudentDataRestApi;
import jakarta.persistence.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
public class MainController {
    @Autowired
    private StudentRepo studentRepo;
    @GetMapping(path = "/")
    public  @ResponseBody String welcome(){
        return "Welcome to the student database api";
    }
    @PostMapping(path="/restapi/std")
    public @ResponseBody String addNewStud (@RequestBody Student student) {
        studentRepo.save(student);
        return "Saved";
    }
    @GetMapping(path="/restapi/std")
    public @ResponseBody Iterable getStud(@RequestParam Integer stdid) {
        return studentRepo.findStudentByStdId(stdid);
    }
    @DeleteMapping(path="/restapi/std")
    public @ResponseBody String deleteStud(@RequestParam Integer stdid) {
        studentRepo.deleteStudentByStdId(stdid);
        return "removed";
    }
    @GetMapping(path="/restapi/getstdall")
    public @ResponseBody Iterable getStuds() {
        return studentRepo.findAll();
    }
    @GetMapping(path="/restapi/stdfac")
    public @ResponseBody Iterable getStud(@RequestParam String fac) {
        return studentRepo.findStudentByFaculty(fac);
    }
}

使用@SpringBootTest时的错误:

java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@2d9dff65 testClass = com.medi.StudentDataRestApi.InputFindTest, locations = [], classes = [com.medi.StudentDataRestApi.StudentDataRestApiApplication], contextInitializerClasses = [], activeProfiles = ["test"], propertySourceLocations = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@2d52216b, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@1ed6388a, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@229c6181, 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@6d4d66d2, org.springframework.boot.test.context.SpringBootTestAnnotation@ec3851fb], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]

0