我如何防止春季靴子IllegalstateException?
我使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring 需要 @SpringBootTest 注解来查找主类配置并使用该类来启动应用程序上下文。
请尝试以下操作:
Spring needs the @SpringBootTest annotation to look for the main class configuration and use that class to start the application context.
Try the following: