当前位置: 首页 > 知识库问答 >
问题:

@属性映射注释@DataJpaTest、@AutoConfigreTestDatabase不能与@Component注释@Configuration结合使用

端木承业
2023-03-14

我想使用控制器和存储库创建具有Spring Boot上下文配置的完整JUnit测试。我的测试类如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest
//@WebAppConfiguration
@DataJpaTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class ControllerTest {

    @Autowired
    UserController userCtrl;

    @Autowired
    private WebApplicationContext ctx;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
    }

    @Test
    public void loginTest() throws Exception {
        //mockMvc.perform(get("/")).andExpect(status().isOk());

    }
}

我有标准的用户实体、UserController和UserRepository。我在应用程序中配置了数据源。属性,但我无法运行@SpringBootTest和@DataJpaTest-toogether。这是错误消息:

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userControllerTest' defined in file [C:\Users\Mateusz\Documents\mojewyceny\target\test-classes\mojewyceny\test\UserControllerTest.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: The @PropertyMapping annotations @DataJpaTest, @AutoConfigureTestDatabase cannot be used in combination with the @Component annotation @Configuration
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 28 more
Caused by: java.lang.IllegalStateException: The @PropertyMapping annotations @DataJpaTest, @AutoConfigureTestDatabase cannot be used in combination with the @Component annotation @Configuration
    at org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer$PropertyMappingCheckBeanPostProcessor.postProcessBeforeInitialization(PropertyMappingContextCustomizer.java:96)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    ... 42 more

共有1个答案

陈修诚
2023-03-14

好的,我想出来了。测试类应如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ControllerTest {

    @Autowired
    UserController userCtrl;

    @Autowired
    public UserRepository userDao;

    @Autowired
    private WebApplicationContext ctx;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
    }

    @Test
    public void loginTest() throws Exception {

        User user1 = new User();
        user1.setEmail("adres@email.com");
        user1.setPassword("12345");
        userDao.save(user1);

        mockMvc.perform(post("/user/login")
                .param("email", "adres@email.com")
                .param("password", "12345"))
                .andExpect(status().isOk());

    }
}

现在它正在对数据库执行测试并处理请求。一开始它不起作用,因为我还在其他类中混合了DataJpaTest和SpringBootTest注释

 类似资料:
  • 我想让hibernate生成一些带有外键的表等等。我将给出我希望hibernate生成的查询的一个示例: 所以这个查询应该由hibernate通过注释生成。与此对应的类是: 有关中的应映射到的内容的示例(中的每个助记符在中只有一个值): 正如您所看到的,我在文档的帮助下做了一些尝试,但是我不能让Hibernate生成外键。如果有人能告诉我这个类需要的注释和值,那就太好了,这样我就可以自己为其他类做

  • 问题内容: 我正在尝试使用复合主键在学生和教学课程之间建立许多联系: 我的课程: 教学班: 集合对象: 现在,主键: 当我尝试对学生进行坚持时,出现以下错误: 我究竟做错了什么? 问题答案: 我解决了这个问题。我映射了Getter而不是field。

  • 假设我有一个带有属性的注释: 我想创建一个包含多个元注释的复合注释,包括一个带有属性的注释 有没有一种方法可以将复合注释的属性传递给其中一个元注释? 例如,类似这样的东西: 这相当于,但比 谢谢! PS为我对示例注释的错误选择表示歉意-我没有javax。注射@记住命名注释,只是一些具有属性的任意注释。 谢谢大家的回答/评论。 这显然是不可能的。然而,碰巧我的案例有一个简单的解决方法,我将与大家分享

  • 我只是Spring Security的初学者,但我想知道是否可以使用,,和其他注释来配置KeyClope。例如,我在我的简单spring Rest webapp中配置了和Spring Security性,这样我就可以访问控制器中的主体对象,如下所示: 但是当我尝试这样做时(只是一个例子,实际上我想在授权之前执行自定义EL表达式): 我有例外: 组织。springframework。安全认证。Aut

  • 问题内容: 假设我正在调用一个API,该API用产品的以下JSON响应: 我可以使用Jackson标注来映射产品ID和名称: 然后使用fromJson方法创建产品: 但是现在我试图弄清楚如何获取品牌名称,这是一个嵌套属性。我希望这样的事情会起作用: 但是,当然没有。有没有一种简单的方法可以使用注释来完成我想要的工作? 我尝试解析的实际JSON响应非常复杂,即使我只需要一个字段,我也不必为每个子节点