Unit Test Spring Boot

0

I'm doing unit tests for the first time in Spring Boot. It turns out that it shows me an Error:

Field cardRepository in ec.com.alquimiasoft.futgolazoback.service.CardService required a bean of type 'ec.com.alquimiasoft.futgolazoback.repository.CardRepository' that could not be found.

Within my driver test class I have only these references:

@Autowired
private MockMvc mockMvc;
@MockBean
private PrognosticService prognosticService;

And within the service class you are only

@Autowired
private PrognosticRepository prognosticRepository;

My doubt is related to that there may be some configuration that is checking in another service that I am not using from the test class. In short, if I'm running a unit of tests, because it could be happening I throw errors related to other components within the application that are unrelated to the test I'm running.

    
asked by Ernesto Garcia Orduñez 30.10.2018 в 18:39
source

1 answer

0

You can use Mockito

// Crea un mock de PrognosticRepository para el testeo.
@Mock
private PrognosticRepository prognosticRepository;

// Probando la instancia, mockea **prognosticRepository** deberia injectar mocks. 
@InjectMocks
@Resource
private PrognosticService prognosticService;

@Before
public void setUp() throws Exception {
    // Inicializa los mocks creados
    MockitoAnnotations.initMocks(this);
    // cambia el comportamiento de  'prognosticRepository'
    when(prognosticRepository.save("Valor")).thenReturn("Valor");   
}
    
answered by 30.10.2018 в 20:23