스프링 부트 1.4테스트: 구성 오류: @Bootstrap 선언이 여러 개 발견되었습니다.와 함께
다음 공식 문서를 참조하십시오.http://docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/ # Testing
다음과 같은 REST API 메서드 중 하나를 테스트하고 싶었습니다.
@RunWith(SpringRunner.class)
@WebMvcTest(LoginController.class)
@SpringBootTest(classes = Application.class)
public class AuthorizationServiceTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void test() {
Object returnedObject=his.restTemplate.getForObject("/login", Object.class);
}
}
문서에 기재된 바와 같이:
검색 알고리즘은 테스트를 포함하는 패키지에서 @SpringBootApplication 또는 @SpringBootConfiguration 주석 클래스를 찾을 때까지 작동합니다.코드를 합리적인 방식으로 구성하면 일반적으로 기본 구성이 검색됩니다.
코드를 올바르게 구성했습니다(적어도 저는 그렇게 생각합니다).
Authorization Service : 패키지 com 아래에 있습니다.xxx.yy.zzz.허가
Authorization Service Test :는 패키지 com 아래에 있습니다.xxx.yy.zzz.허가테스트
이 예외(전체 추적):
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.orangeraid.rasberry.gateway.authorizationTest.AuthorizationServiceTest]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]
at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:155)
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:126)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
도와주십시오, 저는 이미 2, 3시간 넘게 아무 일도 없이 지내고 있습니다.
감사해요.
이 예외는 스프링테스트에서 메인컨피규레이션클래스를 찾을 수 없는 경우에 발생합니다.테스트 클래스에 @ContextConfiguration anootation을 추가해 보십시오.자세한 내용은 스프링 테스트 설명서를 참조하십시오(섹션 감지 테스트 구성).
테스트 클래스의 예는 다음과 같습니다.
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=Application.class)
@WebMvcTest(MyController.class)
public class MyConrollerTests {
...
}
@Spring Boot 삭제만 하면 됩니다.테스트 및 모든 것이 정상적으로 동작합니다.@Spring Boot 사용에서도 같은 문제가 있었습니다.테스트 및 @DataJpaTest. 이 에러는 pom.xml 부모 springboot 버전을 2.1.0으로 업그레이드 했을 때 다음과 같이 발생합니다.버전 2.0.5를 사용하고 있을 때는 이 에러가 발생하지 않았습니다.
@RunWith(SpringRunner.class)
//@SpringBootTest//(classes = KalahApplication.class)
// DataJpaTest supports rollback after running every test case
@DataJpaTest
public class GameRepositoryTest {
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
이 질문에 답하기엔 너무 늦은 건 알지만 미래에 누군가에게 도움이 될 수도 있어저도 같은 문제가 있었는데 조사를 좀 해보니 그런 일이 있어서는 안 된다는 걸 알게 됐어요@WebMvcTest
있다면@SpringBootTest
제거만 하면 됩니다.@WebMvcTest
그리고.@SpringBootTest
나머지를 돌보고 있어요.
@WebMvcTest와 @SpringBoot를 모두 선언했기 때문에 발생합니다.테스트. 동일한 문제를 해결하려면 @SpringBoot을 삭제해야 합니다.시험
사용만 해야 합니다.@DataJpaTest
.제거한다.@SpringBootTest
테스트 클래스
이게 도움이 되길 바라.@SpringBoot에서 벗었더니 효과가 있었어요.테스트하고 나중에 웹에서 동일한 @AutoWired 에 대해 테스트하고 있었습니다.인터페이스를 사용했을 때와 같은 에러가 발생합니다.오히려 효과가 있었던 것과 같은 것을 조롱하라.
package com.naveen.productreview;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import com.naveen.productreview.service.IProductReviewService;
@RunWith(SpringRunner.class)
@WebMvcTest
//@SpringBootTest
public class WebLayerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private IProductReviewService productReviewService;
@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/api/product-review")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Naveen")));
}
}
적분 테스트를 작성하려는 것 같네요.그래서 사용만 권합니다.
@SpringBootTest(classes = Application.class)
junit4를 사용하고 싶다면
@RunWith(SpringRunner.class)
Junit 5의 경우:
@ExtendWith(SpringExtension.class)
@DataJpaTest
class DeliveryRepositoryTest {
@Autowired
private DeliveryRepository repository;
...
}
이 설정에서도 같은 문제가 있었습니다.
@WebMvcTest
@SpringBootTest
@ContextConfiguration(classes = {Controller.class})
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
...}
컨트롤러에 스프링 서비스 빈이 자동 연결되어 있습니다.
으로 올바른 놔두는 입니다.@WebMvcTest
하고 MockMvc를 @MockBean
서비스를 위해.최종 작업 테스트는 다음과 같습니다.
@WebMvcTest(Controller.class)
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CustomerService customerService;
...}
앱의 server.port를 지정할 때는 테스트 메인 어플리케이션에서도 정의해야 합니다.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
언급URL : https://stackoverflow.com/questions/41057602/spring-boot-1-4-testing-configuration-error-found-multiple-declarations-of-b
'source' 카테고리의 다른 글
스프링 부트 컨트롤러 엔드포인트 유닛테스트 작성 방법 (0) | 2023.04.04 |
---|---|
Cross-Origin Resource Sharing(CORS) 사후 요청을 처리하는 방법 (0) | 2023.04.04 |
PowerShell : 필드 값으로 JSON 개체를 검색합니다. (0) | 2023.04.04 |
페이지 새로고침 시 리액트 라우터에서 location.state를 클리어하려면 어떻게 해야 합니까? (0) | 2023.04.04 |
스프링 부트: Spring Boot Servlet이니셜라이저는 권장되지 않습니다. (0) | 2023.04.04 |