5

Hey i have started learning spring-boot junit testing using spring boot Test framework at the time of creating the test case i am facing issues below .

how to resolve this issue.

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)

this is my 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()
       {

       }
}

this is my 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));

}
}
AVINASH M
  • 487
  • 3
  • 7
  • 19

1 Answers1

5

From the error logs it gives hint to use classes attribute for @SpringBootTest so

Change @SpringBootTest to @SpringBootTest(classes = Application.class)

Give a fully classified name of the class like below.

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

Also refer this thread

Alien
  • 15,141
  • 6
  • 37
  • 57
  • if i add my TestWebApp fully quilified name like below .I'm getting error : @SpringBootTest(classes =/rental-car-system/src/test/java/com/test/project/rentalcarsystem/TestWebApp.java) – AVINASH M Dec 04 '18 at 07:22
  • which class path should i add here TestWebApp.class file ryt @Alien – AVINASH M Dec 04 '18 at 09:30
  • application main class which is annotated with @SpringBootApplication – Alien Dec 04 '18 at 09:31
  • But now i'm getting the exception like : java.lang.AssertionError: No value at JSON path "$.setMilleage", exception: No results for path: $['setMilleage'] at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:245) – AVINASH M Dec 04 '18 at 09:49
  • this is something related to json keys so refer https://stackoverflow.com/questions/32397690/how-to-test-if-json-path-does-not-include-a-specific-element-or-if-the-element?rq=1 – Alien Dec 04 '18 at 09:53