【问题标题】:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...)java.lang.IllegalStateException: 找不到@SpringBootConfiguration,需要使用@ContextConfiguration 或@SpringBootTest(classes=...)
【发布时间】:2019-05-05 12:22:05
【问题描述】:

嘿,在创建测试用例时,我已经开始使用 spring boot 测试框架学习 spring-boot junit 测试,我面临以下问题。

如何解决这个问题。

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
    at org.springframework.util.Assert.state(Assert.java:70)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:202)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:137)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323)

这是我的 RentalCarSystemApplicationTests.java

package com.test.project.rentalcarsystem;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RentalCarSystemApplicationTests {

       @Test
       public void contextLoads()
       {

       }
}

这是我的 TestWebApp.java

package com.test.project.rentalcarsystem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.springframework.web.context.WebApplicationContext;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

public class TestWebApp extends RentalCarSystemApplicationTests {
    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

@Before
public void setup() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void testEmployee() throws Exception {
    mockMvc.perform(get("/car")).andExpect(status().isOk())
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$.setMilleage").value("24")).andExpect(jsonPath("$.setModelname").value("BMW"))
            .andExpect(jsonPath("$.setSeating_capacity").value("5")).andExpect(jsonPath("$.setType").value("SUV"))
            .andExpect(jsonPath("$.setCost").value("3000")).andExpect(jsonPath("$.setEmail").value("demo@gmail.com"))
            .andExpect(jsonPath("$.setNumber").value("9845658789")).andExpect(jsonPath("$.setPincode").value(560036));

}
}

【问题讨论】:

    标签: spring-boot junit junit4


    【解决方案1】:

    从错误日志中它提示使用classes 属性为@SpringBootTest 所以

    @SpringBootTest 更改为@SpringBootTest(classes = Application.class)

    给出类的完全分类名称,如下所示。

    @SpringBootTest(classes=com.package.path.class)
    

    另请参阅this thread

    【讨论】:

    • 如果我像下面这样添加我的 TestWebApp 完整名称。我收到错误:@SpringBootTest(classes =/rental-car-system/src/test/java/com/test/project/rentalcarsystem /TestWebApp.java)
    • 我应该在此处添加哪个类路径 TestWebApp.class 文件 ryt @Alien
    • 应用程序主类,带有@SpringBootApplication注解
    • 但是现在我得到了这样的异常:java.lang.AssertionError:JSON 路径“$.setMilleage”处没有值,异常:路径没有结果:$['setMilleage'] at org .springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:245)
    • 这是与 json 键相关的内容,请参考 stackoverflow.com/questions/32397690/…
    猜你喜欢
    • 2018-05-09
    • 2018-11-05
    • 2019-03-28
    • 2017-07-26
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    相关资源
    最近更新 更多