我如何防止春季靴子IllegalstateException?

发布于 2025-01-19 06:40:25 字数 2845 浏览 0 评论 0原文

我使用Spring Boot Data JPA创建了一个简单的模型和存储库层。要测试这些层,我想通过JUNIT编写单元测试代码并测试代码。但是我会遇到以下错误。如何解决此错误并测试我的代码?

错误日志消息:

    java.lang.IllegalStateException: Failed to load ApplicationContext
    
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
        at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
        at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
        at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
        at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$8(ClassBasedTestDescriptor.java:363)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:368)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$9(ClassBasedTestDescriptor.java:363)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.jav

a:177)

模型层

@Entity
@Table(name="product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private Long id;
    @Column
    private String name;
    @Column
    private BigDecimal price;

    //getter setter
}

repositoryLayerer

public interface ProductRepository extends JpaRepository<Product,Long> {
    @Query("SELECT p FROM Product p WHERE p.name IS NOT NULL order by p.id ASC")
    public List<Product> findAllProducts();

    @Query("SELECT p FROM Product p WHERE p.name = ?1 order by p.id ASC")
    public List<Product> findProductsByName(String name);

    @Query("SELECT p FROM Product p WHERE p.price = ?1 order by p.id ASC")
    public List<Product> findProductsByPrice(BigDecimal price);
}

单位测试类

@DataJpaTest
public class JpaTest {

    @Autowired
    ProductRepository productRepository;

    @Test
    public void getProductByIdTest() {
        Product c = productRepository.findById(1L).get();

        Assertions.assertThat(c.getId()).isEqualTo(1L);
    }
}

I created a simple model and repository layers using spring boot data jpa. To test these layers, I want to write unit test code over junit and test the code. But I am getting error as below. How can I get around this error and test my code?

Error log message:

    java.lang.IllegalStateException: Failed to load ApplicationContext
    
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
        at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
        at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
        at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
        at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$8(ClassBasedTestDescriptor.java:363)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:368)
        at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$9(ClassBasedTestDescriptor.java:363)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.jav

a:177)

Model Layer

@Entity
@Table(name="product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private Long id;
    @Column
    private String name;
    @Column
    private BigDecimal price;

    //getter setter
}

RepositoryLayer

public interface ProductRepository extends JpaRepository<Product,Long> {
    @Query("SELECT p FROM Product p WHERE p.name IS NOT NULL order by p.id ASC")
    public List<Product> findAllProducts();

    @Query("SELECT p FROM Product p WHERE p.name = ?1 order by p.id ASC")
    public List<Product> findProductsByName(String name);

    @Query("SELECT p FROM Product p WHERE p.price = ?1 order by p.id ASC")
    public List<Product> findProductsByPrice(BigDecimal price);
}

Unit Test Class

@DataJpaTest
public class JpaTest {

    @Autowired
    ProductRepository productRepository;

    @Test
    public void getProductByIdTest() {
        Product c = productRepository.findById(1L).get();

        Assertions.assertThat(c.getId()).isEqualTo(1L);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2025-01-26 06:40:25

Spring 需要 @SpringBootTest 注解来查找主类配置并使用该类来启动应用程序上下文。

请尝试以下操作:

@SpringBootTest
@DataJpaTest
public class JpaTest {

    @Autowired
    ProductRepository productRepository;

    @Test
    public void getProductByIdTest() {
        Product c = productRepository.findById(1L).get();

        Assertions.assertThat(c.getId()).isEqualTo(1L);
    }
}

Spring needs the @SpringBootTest annotation to look for the main class configuration and use that class to start the application context.

Try the following:

@SpringBootTest
@DataJpaTest
public class JpaTest {

    @Autowired
    ProductRepository productRepository;

    @Test
    public void getProductByIdTest() {
        Product c = productRepository.findById(1L).get();

        Assertions.assertThat(c.getId()).isEqualTo(1L);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文