Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MockBean doesn't work in Spring boot integration test

I have Spring Integration test where I'm trying to Mock some of my Beans. For some reason although I Mocked them they are NULL. Here is code snippet:

The Bean which I want to Mock

@Component
public class MockWS {

    public String callSoapClient() throws JAXBException{
        return "CallSoapCl";
    }
}

The class where the Bean is used

public class SmDpES2PortImpl implements ES2SmDp {
    @Autowired
    private MockWS mock;

    @Override
    public void es2DownloadProfile(ES2DownloadProfileRequest parameters) {
         try {
            LOG.info("\n\n\n TEST BEAN: " + mock.callSoapClient() + "\n\n");
          }
     }  
}

Spring boot integration test where the Bean has been mocked

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ES2SmDpApplicationTests {

    @MockBean(name="mockWS")
    MockWS mockService;

    @Test
    public void test1Es2DownloadProfile_Sucess() throws MalformedURLException, JAXBException, SOAPException {
        when(mockService.callSoapClient()).thenReturn("CallMockCLient");
    }
}

Output from the build execution: TEST BEAN: null

like image 589
Tsvetoslav Tsvetkov Avatar asked Oct 21 '25 11:10

Tsvetoslav Tsvetkov


1 Answers

In my case the following combination of annotations worked:

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = { ControllerThatIAmTesting.class }) 
@AutoConfigureMockMvc(addFilters = false) // if using MockMvc object

But I had to declare explicitly both Autowired objects that I use in the ControllerThatIAmTesting in the test class with @MockBean annotation - otherwise Spring would complain that it cannot find suitable implementation - incidentally both my interfaces and their implementations are in the same corresponding packages

Also, using @WebMvcTest instead of @SpringBootTest (other suggest it as more specific scenario) resulted in Spring failing to find and initialize some other @Autowired dependencies from my @Configuration classes.

Related posts post1 post2 post3

like image 76
hello_earth Avatar answered Oct 24 '25 02:10

hello_earth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!