Spring boot java.lang.IllegalStateException:找不到@SpringBootConfiguration,您需要使用@ContextConfiguration或@SpringBootTest(类=…)

Spring boot java.lang.IllegalStateException:找不到@SpringBootConfiguration,您需要使用@ContextConfiguration或@SpringBootTest(类=…),spring-boot,junit,junit4,Spring Boot,Junit,Junit4,嘿,在创建下面的测试用例时,我已经开始学习使用SpringBootTestFramework进行SpringBootJUnit测试 如何解决这个问题 java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test at org.spr

嘿,在创建下面的测试用例时,我已经开始学习使用SpringBootTestFramework进行SpringBootJUnit测试

如何解决这个问题

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));

}
}

从错误日志中,它提示对
@springbootest
使用
classes
属性

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

给出一个完全分类的类名,如下所示

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

如果我像下面这样添加我的TestWebApp完全限定的名称,也请参考

。我得到了错误:@SpringBootTest(classes=/rental car system/src/test/java/com/test/project/rentalcarsystem/TestWebApp.java)我应该在这里添加哪个类路径TestWebApp.class文件ryt@Alienapplication main class,它用@springbootcapplication注释,但现在我得到了如下异常:java.lang.AssertionError:JSON路径“$.setMillage”没有值,异常:路径:$['setMillage'没有结果]在org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:245)中,这与json键有关,因此请参考