Я использовал следующую статью:
https://www.baeldung.com/spring-boot- testing#integration-testing-with-datajpatest
но возникла проблема с загрузкой контекста приложения. Я думаю, что это проблема SpringBoot/Bean.
Вот мой класс тестирования:
Код: Выделить всё
package ak19f.repositories;
import ak19f.entities.ComponentOur;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.sql.DataSource;
import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest
@ExtendWith(SpringExtension.class)
public class ComponentRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private ComponentRepository componentRepository;
@Test
public void testGetOne() {
// given
ComponentOur c1 = new ComponentOur();
c1.setId(1);
c1.setComponentBrand("d");
c1.setComponentDescription("s");
c1.setSerialNumber("123");
entityManager.persist(c1);
entityManager.flush();
// when
ComponentOur found = componentRepository.getOne(1);
// then
assertThat(found.getComponentBrand())
.isEqualTo(c1.getComponentBrand());
assertThat(found.getComponentDescription())
.isEqualTo(c1.getComponentDescription());
assertThat(found.getComponentTyp())
.isEqualTo(c1.getComponentTyp());
assertThat(found.getSerialNumber())
.isEqualTo(c1.getSerialNumber());
}
}
Код: Выделить всё
@SpringBootApplication
@EnableWebMvc
public class PrototypApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(PrototypApplication.class, args);
}
Код: Выделить всё
package ak19f.repositories;
import ak19f.entities.ComponentOur;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ComponentRepository extends JpaRepository{
}
Код: Выделить всё
package ak19f.entities;
import ak19f.entities.ServiceAgreement;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@Entity
@Table(name = "component_our")
public class ComponentOur implements Serializable, EntityBase {
@Id
@Column(name = "component_id" , nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "component_typ" , nullable = false)
private String componentTyp;
@Column(name = "component_description")
private String componentDescription;
@Column(name = "component_brand")
private String componentBrand;
@Column( name = "serial_number")
private String serialNumber;
public ComponentOur(){}
public ComponentOur(String componentTyp, String componentDescription, String componentBrand){
this.componentTyp = componentTyp;
this.componentDescription = componentDescription;
this.componentBrand= componentBrand;
}
@Override
public Integer getId() {
return id;
}
@Override
public void setId(Integer id) {
}
public String getComponentTyp() {
return componentTyp;
}
public void setComponentTyp(String componentTyp) {
this.componentTyp = componentTyp;
}
public String getComponentDescription() {
return componentDescription;
}
public String getComponentBrand() {
return componentBrand;
}
public void setComponentBrand(String componentBrand) {
this.componentBrand = componentBrand;
}
public void setComponentDescription(String componentDescrip) {
this.componentDescription = componentDescrip;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
}
Вот пример стека ошибок:
Код: Выделить всё
java.lang.IllegalStateException: F a i l e d t o l o a d A p p l i c a t i o n C o n t e x t < b r / > < b r / > a t o r g . s p r i n g f r a m e w o r k . t e s t . c o n t e x t . c a c h e . D e f a u l t C a c h e A w a r e C o n t e x t L o a d e r D e l e g a t e . l o a d C o n t e x t ( D e f a u l t C a c h e A w a r e C o n t e x t L o a d e r D e l e g a t e . j a v a : 1 3 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . t e s t . c o n t e x t . s u p p o r t . D e f a u l t T e s t C o n t e x t . g e t A p p l i c a t i o n C o n t e x t ( D e f a u l t T e s t C o n t e x t . j a v a : 1 2 3 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . t e s t . c o n t e x t . s u p p o r t . D e p e n d e n c y I n j e c t i o n T e s t E x e c u t i o n L i s t e n e r . i n j e c t D e p e n d e n c i e s ( D e p e n d e n c y I n j e c t i o n T e s t E x e c u t i o n L i s t e n e r . j a v a : 1 1 8 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . t e s t . c o n t e x t . s u p p o r t . D e p e n d e n c y I n j e c t i o n T e s t E x e c u t i o n L i s t e n e r . p r e p a r e T e s t I n s t a n c e ( D e p e n d e n c y I n j e c t i o n T e s t E x e c u t i o n L i s t e n e r . j a v a : 8 3 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . b o o t . t e s t . a u t o c o n f i g u r e . S p r i n g B o o t D e p e n d e n c y I n j e c t i o n T e s t E x e c u t i o n L i s t e n e r . p r e p a r e T e s t I n s t a n c e ( S p r i n g B o o t D e p e n d e n c y I n j e c t i o n T e s t E x e c u t i o n L i s t e n e r . j a v a : 4 3 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . t e s t . c o n t e x t . T e s t C o n t e x t M a n a g e r . p r e p a r e T e s t I n s t a n c e ( T e s t C o n t e x t M a n a g e r . j a v a : 2 4 4 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . t e s t . c o n t e x t . j u n i t . j u p i t e r . S p r i n g E x t e n s i o n . p o s t P r o c e s s T e s t I n s t a n c e ( S p r i n g E x t e n s i o n . j a v a : 9 8 ) < b r / > a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . C l a s s B a s e d T e s t D e s c r i p t o r . l a m b d a $ i n v o k e T e s t I n s t a n c e P o s t P r o c e s s o r s $ 5 ( C l a s s B a s e d T e s t D e s c r i p t o r . j a v a : 3 3 7 ) < b r / > a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . C l a s s B a s e d T e s t D e s c r i p t o r . e x e c u t e A n d M a s k T h r o w a b l e ( C l a s s B a s e d T e s t D e s c r i p t o r . j a v a : 3 4 2 ) < b r / > a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . C l a s s B a s e d T e s t D e s c r i p t o r . l a m b d a $ i n v o k e T e s t I n s t a n c e P o s t P r o c e s s o r s $ 6 ( C l a s s B a s e d T e s t D e s c r i p t o r . j a v a : 3 3 7 ) < b r / > a t j a v a . u t i l . s t r e a m . R e f e r e n c e P i p e l i n e $ 3 $ 1 . a c c e p t ( R e f e r e n c e P i p e l i n e . j a v a : 1 9 3 ) < b r / > a t j a v a . u t i l . s t r e a m . R e f e r e n c e P i p e l i n e $ 2 $ 1 . a c c e p t ( R e f e r e n c e P i p e l i n e . j a v a : 1 7 5 ) < b r / > a t j a v a . u t i l . A r r a y L i s t $ A r r a y L i s t S p l i t e r a t o r . f o r E a c h R e m a i n i n g ( A r r a y L i s t . j a v a : 1 3 8 2 ) < b r / > a t j a v a . u t i l . s t r e a m . A b s t r a c t P i p e l i n e . c o p y I n t o ( A b s t r a c t P i p e l i n e . j a v a : 4 8 2 ) < b r / > a t j a v a . u t i l . s t r e a m . A b s t r a c t P i p e l i n e . w r a p A n d C o p y I n t o ( A b s t r a c t P i p e l i n e . j a v a : 4 7 2 ) < b r / > a t j a v a . u t i l . s t r e a m . S t r e a m S p l i t e r a t o r s $ W r a p p i n g S p l i t e r a t o r . f o r E a c h R e m a i n i n g ( S t r e a m S p l i t e r a t o r s . j a v a : 3 1 3 ) < b r / > a t j a v a . u t i l . s t r e a m . S t r e a m s $ C o n c a t S p l i t e r a t o r . f o r E a c h R e m a i n i n g ( S t r e a m s . j a v a : 7 4 3 ) < b r / > a t j a v a . u t i l . s t r e a m . S t r e a m s $ C o n c a t S p l i t e r a t o r . f o r E a c h R e m a i n i n g ( S t r e a m s . j a v a : 7 4 2 ) < b r / > a t j a v a . u t i l . s t r e a m . R e f e r e n c e P i p e l i n e $ H e a d . f o r E a c h ( R e f e r e n c e P i p e l i n e . j a v a : 6 4 7 ) < b r / > a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . C l a s s B a s e d T e s t D e s c r i p t o r . i n v o k e T e s t I n s t a n c e P o s t P r o c e s s o r s ( C l a s s B a s e d T e s t D e s c r i p t o r . j a v a : 3 3 6 ) < b r / > a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . C l a s s B a s e d T e s t D e s c r i p t o r . i n s t a n t i a t e A n d P o s t P r o c e s s T e s t I n s t a n c e ( C l a s s B a s e d T e s t D e s c r i p t o r . j a v a : 2 5 9 ) < b r / > a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . C l a s s B a s e d T e s t D e s c r i p t o r . l a m b d a $ t e s t I n s t a n c e s P r o v i d e r $ 2 ( C l a s s B a s e d T e s t D e s c r i p t o r . j a v a : 2 5 2 ) < b r / > a t j a v a . u t i l . O p t i o n a l . o r E l s e G e t ( O p t i o n a l . j a v a : 2 6 7 ) < b r / > a t o r g . j u n i t . j u p i t e r . e n g i n e . d e s c r i p t o r . C l a s s B a s e d T e s t D e s c r i p t o r . l a m b d a $ t e s t I n s t a n c e s P r o v i d e r $3(ClassBasedTestDescriptor.java:251)
at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:29)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:106)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:105)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:69)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:107)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:107)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:75)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:125)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
... 69 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
... 87 more
Caused by: java.lang.IllegalStateException: No ServletContext set
at org.springframework.util.Assert.state(Assert.java:73)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:533)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 88 more
Есть ли кто-нибудь, у кого такая же проблема или решение? ?
Если вам нужна дополнительная информация/фрагменты кода/стеки ошибок, свяжитесь со мной.
спасибо
Подробнее здесь: https://stackoverflow.com/questions/598 ... stateexcep