java.lang.IllegalStateException: 找不到@SpringBootConfiguration,您需要使用@ContextConfiguration或@SpringBootTest(classes=...)。

5

嘿,我开始学习使用Spring Boot Test框架进行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));

}
}
1个回答

5

从错误日志中可以看出需要使用classes属性来修饰@SpringBootTest,所以需要将@SpringBootTest修改为@SpringBootTest(classes = Application.class)

像下面这样给出完全限定的类名。

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

另请参考此帖子


如果我像下面这样添加我的TestWebApp的完全限定名称,我会收到错误提示:@SpringBootTest(classes =/rental-car-system/src/test/java/com/test/project/rentalcarsystem/TestWebApp.java)。 - AVINASH M
我应该在这里添加哪个类路径来引用TestWebApp.class文件呢?@Alien - AVINASH M
应用程序主类,使用 @SpringBootApplication 进行注释。 - Alien
但现在我遇到了异常:java.lang.AssertionError: JSON路径“$.setMilleage”没有值,异常:$['setMilleage']的结果为空。 位于org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:245)。 - AVINASH M
这是与JSON键相关的内容,请参考https://dev59.com/B1wY5IYBdhLWcg3wpJA-?rq=1。 - Alien

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接