Тестовый модуль Spring boot: невозможно зарегистрировать макетный компонентJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Тестовый модуль Spring boot: невозможно зарегистрировать макетный компонент

Сообщение Anonymous »

У меня есть два класса тестов: 1- для модульного теста класса Controller и 2- для модульного теста класса Service, как показано ниже:

< Strong>1- Контроллер тестового класса:

Код: Выделить всё

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CentroDeCustoRestControllerTeste {

@Autowired
private MockMvc mvc;

@MockBean
private CentroDeCustoService service;

@Test
@WithMockUser(username = "teste-app@teste.com", authorities = {"ADMIN"})
public void aoBuscarUmCentroDeCustoRetornarUmJsonComPropriedadeCentroDeCusto() throws Exception {
CentroDeCusto centroDeCusto = new CentroDeCusto();
centroDeCusto.setNome("Financeiro");

given(service.centroDeCusto(1L)).willReturn(Optional.of(centroDeCusto));

mvc.perform(get("/centrosdecustos/1").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.centroDeCusto", org.hamcrest.core.Is.is(centroDeCusto.getCentroDeCusto())));
}
}
2- Тестовый класс обслуживания:

Код: Выделить всё

@RunWith(SpringRunner.class)
public class CentroDeCustoServiceImplTeste {

@TestConfiguration
static class CentroDeCustoServiceImplConfiguracaoContexto {
@Bean
public CentroDeCustoService centroDeCustoService() {
return new CentroDeCustoServiceImpl();
}
}

@Autowired
private CentroDeCustoService service;

@MockBean
private CentroDeCustoRepository repository;

@Before
public void setup() {
CentroDeCusto centroDeCusto = new CentroDeCusto();
centroDeCusto.setId(1L);
Optional centroDeCustoOpt = Optional.of(centroDeCusto);

Mockito.when(repository.findById(1L)).thenReturn(centroDeCustoOpt);
}

@Test
public void aoBuscarUmCentroDeCustoExistenteRetornarUmCentroDeCustoOptional() {
Optional resultado = service.centroDeCusto(1L);
CentroDeCusto centroDeCusto = resultado.get();

Assertions.assertThat(centroDeCusto.getId())
.isEqualTo(1L);
}
}
Интерфейс CentroDeCustoService

Код: Выделить всё

public interface CentroDeCustoService {

Optional centroDeCusto(Long id);

Page centrosDeCusto(Pageable pagina);

Optional buscaCentroDeCustoPeloNumero(String numero);

List buscaCentrosDeCustoDeUmaContaGerencial(Long idDaContaGerencial);

boolean verificaSeCentroDeCustoJahExiste(CentroDeCusto centroDeCusto);

CentroDeCusto criaCentroDeCusto(CentroDeCusto centroDeCusto);

CentroDeCusto atualizaCentroDeCusto(CentroDeCusto centroDeCusto);

boolean delataCentroDeCusto(Long id);
}
Класс CentroDeCustoServiceImpl

Код: Выделить всё

@Service
public class CentroDeCustoServiceImpl implements CentroDeCustoService {
@Autowired
private CentroDeCustoRepository repository;

@Override
public Optional centroDeCusto(Long id) {
return repository.findById(id);
}

@Override
public Page centrosDeCusto(Pageable pagina) {
return repository.departamentosPorDataDeAtualizacao(pagina);
}

@Override
public Optional buscaCentroDeCustoPeloNumero(String numero) {
return repository.findByCentroDeCusto(numero);
}

@Override
public List buscaCentrosDeCustoDeUmaContaGerencial(Long idDaContaGerencial) {
return repository.findByContasGerenciaisId(idDaContaGerencial);
}

@Override
public boolean verificaSeCentroDeCustoJahExiste(CentroDeCusto centroDeCusto) {
String numeroDoCentroDeCusto = centroDeCusto.getCentroDeCusto();
Long idDoCentroDeCusto = centroDeCusto.getId();
Optional  centroDeCustoOpt = repository.findByCentroDeCustoAndIdNotIn(numeroDoCentroDeCusto, idDoCentroDeCusto);

if (centroDeCustoOpt.isPresent())
return true;

return false;
}

@Override
public CentroDeCusto criaCentroDeCusto(CentroDeCusto centroDeCusto) {
return repository.save(centroDeCusto);
}

@Override
public CentroDeCusto atualizaCentroDeCusto(CentroDeCusto centroDeCusto) {
return repository.save(centroDeCusto);
}

@Override
public boolean delataCentroDeCusto(Long id) {
Optional centroDeCustoOpt = centroDeCusto(id);
if (centroDeCustoOpt.isPresent()) {
repository.delete(centroDeCustoOpt.get());
return true;
}

return false;
}
}
Когда я запускаю контроллер, кажется, что тест, созданный для класса Service, влияет, поскольку оба теста вызывают объект CentroDeCustoService. Как мне правильно настроить тестовые классы, чтобы больше не было конфликтов?

Ошибка: Невозможно зарегистрировать макетный компонент br.com.teste.reembolso.centrocusto. CentroDeCustoService ожидал, что будет заменен один соответствующий bean-компонент, но обнаружил [centroDeCustoService, centroDeCustoServiceImpl]:

Код: Выделить всё

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
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.IllegalStateException:  Unable to register mock bean br.com.teste.reembolso.centrocusto.CentroDeCustoService expected a single matching bean to replace but found [centroDeCustoService, centroDeCustoServiceImpl]
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.getBeanName(MockitoPostProcessor.java:239)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.registerMock(MockitoPostProcessor.java:184)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.register(MockitoPostProcessor.java:174)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:144)
at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:131)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:282)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:170)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:139)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
... 24 more
Похоже, это конфликт, но я не знаю, как его решить. может кто-нибудь мне помочь?

Подробнее здесь: https://stackoverflow.com/questions/546 ... -mock-bean
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»