Как мне разрешить это переопределение определения бина? - PullRequest
0 голосов
/ 25 сентября 2019

Я обновил Spring Boot 1.5 до Spring Boot 2.1.8.У меня были некоторые тесты, которые работали, но сейчас не удается.Я также использовал плагин maven-surefire в версии 2.9, и он работал, но я также обновил его до 2.22.0, если это имеет значение.

@ExtendWith(SpringExtension.class)
@WebMvcTest(value = ElementController.class, secure = false)
@ContextConfiguration(classes = TestSite1Config.class)
public class ElementControllerSite1IT {
  @Autowired
  protected MockMvc mvc;
  @MockBean
  ElementService elementService;
  @BeforeEach
  public void setup() {
    when(elementService.getElementTable( ... )) //skipping args for brevity
       .thenReturn(new ElementTable());
  }

  @Configuration
  public static class TestSite1Config {
    @Bean
    @Autowired
    public ElementController elementController(final ElementService elementService) {
      return new ElementController(elementService, new ElementControllerProperties(DeploymentLocation.SITE1));
  }

  @Test
  public void failSite1ValidationWithoutId() throws Exception {
    ElementParameters params = getParams(false);
    mvc.perform(post("/element")
      .contentType(JSON)
      .andExpect(status().isBadRequest());
  }

  //more tests, but doesn't matter.
}

Есть другой класс, как выше, но замените Site1 на Site2.

Существует также класс ElementController & Service.

Я получаю это исключение:

Caused by BeanDefinitionOverrideException: Invalid bean definition with name 'elementController' defined in class path resource [ui/v2/web/ElementControllerSite1IT$TestSite1Config.class]: Cannot register bean definition [Root bean: class [null]; ... defined in class path resource [ui/v2/web/ElementControllerSite1ITConfig.class] for bean 'elementController': There is already [Generic bean: class [ui.v2.web.ElementController]; .. defined in file [...ui/v2/web/ElementController.class]] bound.

Я не писал тесты, это код, который я 'Я унаследовал, в кодовой базе, на которую меня просто навалили.

Ответы [ 3 ]

0 голосов
/ 25 сентября 2019

Вы можете попробовать @TestPropertySource(properties ="...":

@ExtendWith(SpringExtension.class)
@WebMvcTest(value = ElementController.class, secure = false)
@ContextConfiguration(classes = TestSite1Config.class)
@TestPropertySource(properties = {"spring.main.allow-bean-definition-overriding=true", "local.server.port=7777"})
public class ElementControllerSite1IT {
 ...
}
0 голосов
/ 27 сентября 2019

Получил работу с этим: (для тех, кто сталкивается с этим вопросом)

@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@WebMvcTest
@ContextConfiguration(classes = {ElementController.class,TestSite1Config.class})
public class ElementControllerSite1IT {
  @Autowired
  private MockMvc mvc;
  ...
  @Configruation
  public static class TestSite1Config {
    @Bean
    @Primary
    public ElementControllerProperties elementControllerProperties() { return ... }
  }
  ...
}
0 голосов
/ 25 сентября 2019

Добавить spring.main.allow-bean-definition-overriding=true в application.properties

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...