Тест блока контроллера Spring Boot: не удалось загрузить ApplicationContext

Вопрос: У меня простой контроллер загрузки весны, и я хочу написать для него единичный тест, но есть ошибка. Я много работал в Интернете, но до сих пор не могу найти решение. Вот код: HelloController.java @RestController public class HelloController { @Autowired private HelloService helloService; @GetMapping("/hello") public String sayHello(){ return helloService.sayHello(); } } HelloService.java: @Service public class

Вопрос:

У меня простой контроллер загрузки весны, и я хочу написать для него единичный тест, но есть ошибка. Я много работал в Интернете, но до сих пор не могу найти решение. Вот код:

HelloController.java

@RestController public class HelloController { @Autowired private HelloService helloService; @GetMapping(«/hello») public String sayHello(){ return helloService.sayHello(); } }

HelloService.java:

@Service public class HelloService { public String sayHello(){ return «Hello»; } }

И единичный тестовый файл: HelloControllerTest.java:

@RunWith(SpringJUnit4ClassRunner.class) @WebMvcTest(HelloController.class) public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Mock private HelloService helloService; @Before public void setUp(){ MockitoAnnotations.initMocks(this); } @Test public void sayHello() throws Exception { when(helloService.sayHello()).thenReturn(«thach»); mockMvc.perform(get(«/hello»)) .andExpect(status().isOk()) .andExpect(content().string(«thach»)); } }

Но есть ошибка:

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105) at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.lang.IllegalArgumentException: Cannot load an ApplicationContext with a NULL ‘contextLoader’. Consider annotating your test class with @ContextConfiguration or @ContextHierarchy. at org.springframework.util.Assert.notNull(Assert.java:134) at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:57) at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) … 24 more

Может кто-нибудь мне помочь? Я просто новичок в Spring boot

Лучший ответ:

У меня была аналогичная проблема. См. Код ниже:

@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class ApplicationControllerTest { @Autowired MockMvc mockMvc; @MockBean ApplicationService applicationService; @Test public void testGetImagePath() throws Exception { RequestBuilder requestBuilder = MockMvcRequestBuilders.get(«/application/get-image») .contentType(MediaType.IMAGE_GIF); MvcResult result = mockMvc.perform(requestBuilder).andReturn(); System.out.println(result.getResponse()); }

См. Ниже:

Проверьте аннотации, которые вы использовали, например: 1) @RunWith (SpringRunner.class) 2) @SpringBootTest 3) @AutoConfigureMockMvc

Ответ №1

Здесь отсутствует аннотация @SpringBootTest. Эта аннотация используется для загрузки классов @Configuration для тестирования и запуска встроенного контейнера с портом по умолчанию.
Если класс @Configuration не указан, он будет использовать класс @SpringBootApplication по умолчанию, поскольку он имеет @Configuration внутри своей аннотации.

Оцените статью
Добавить комментарий