mercredi 25 septembre 2019

How do I resolve this bean defninition override?

I've upgraded from Spring Boot 1.5 to Spring Boot 2.1.8. I had some tests that were working but are now failing. I also was using maven-surefire plugin at version 2.9 and it worked, but I upgraded that to 2.22.0 as well, if that matters.

@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.
}

There's another class like above, but replace Site1 with Site2.

There is an ElementController & Service class as well.

I get this exception:

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.

I didn't write the tests, it's code that I've inherited, in a code base that I'm just getting spooled up on.

Aucun commentaire:

Enregistrer un commentaire