@MockBean return always false

I'm trying to test my app, but when I use @MockBean, all the functions return false. This is my test

@RunWith(SpringRunner.class)
@SpringBootTest
public class AppParkingApplicationTests {

@MockBean
Vehicle vehicle;
@MockBean
VehicleController vehicleController;

@Test
public void getValidVehicleTest(){
    //Arrange
    boolean resp=false;
    when(vehicle.getTipo()).thenReturn("Carro");
    //Act
    resp=vehicleController.getValidVehicle(vehicle.getTipo());
    //Assert
    assertEquals(true, resp);
}

And this is the function

public boolean getValidVehicle(String tipo){
        boolean result=false;
        if(tipo.equals("Carro") || tipo.equals("Moto")){
            result= true;
        }
        return result;
    }

Upvotes: 0

Views: 1153

Answers (1)

Plog
Plog

Reputation: 9622

That's because the default return for a mocked method when the return type is the primitive boolean is false.

I dont think you want to mock your Controller since this seems to be the class under test. Just replace

@MockBean
VehicleController vehicleController;

with

@Autowired
VehicleController vehicleController;

Upvotes: 1

Related Questions