简体   繁体   中英

Mock object method call using Spring Boot and Mockito

I am trying to write a test for this Java SpringBoot's class:

https://github.com/callistaenterprise/blog-microservices/blob/master/microservices/composite/product-composite-service/src/main/java/se/callista/microservices/composite/product/service/ProductCompositeIntegration.java

Specifically, I am trying to "mock" this method call:

URI uri = util.getServiceUrl("product");

I figured out I should "mock" the ServiceUtils object in order to do this. I tried this using the @Mock and @InjectMocks annotations:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProductCompositeServiceApplication.class)
public class ProductCompositeIntegrationTest {

    @InjectMocks
    @Autowired
    private ProductCompositeIntegration productIntegration;

    @Autowired
    private RestTemplate restTemplate;

    @Mock
    private ServiceUtils util;

    private MockRestServiceServer mockServer;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }

    @Test
    public void myTest() {
        Mockito.when(util.getServiceUrl("product")).thenReturn(URI.create("http://localhost:8080/test"));
        ResponseEntity<Iterable<Product>> products = productIntegration.getAllProducts();
    }
}

But this way it still calls the original ServiceUtils object, and not the "mocked" one. Also tried without the @Autowired annotation at the ProductCompositeIntegration , but this results in a NullPointerException .

What am I doing wrong?


My main class looks like this:

@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
public class ProductCompositeServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductCompositeServiceApplication.class, args);
    }
}

The ServiceUtils object that I am trying to mock is specified in a class, annotated with Spring's @Component annotation to inject it into the other classes using @Autowired .

After a lot of trial and error I managed to solve this problem.

I dropped the

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ProductCompositeServiceApplication.class)

annotations aboved the test class.

I marked the class that I was testing with @InjectMocks and the dependencies with @Mock :

public class ProductCompositeIntegrationTest {
    @InjectMocks
    private ProductCompositeIntegration productIntegration;

    @Mock
    private ServiceUtils util;

    private MockRestServiceServer mockServer;

    private RestTemplate restTemplate = new RestTemplate();

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
        mockServer = MockRestServiceServer.createServer(restTemplate);
        productIntegration.setRestTemplate(restTemplate);
    }

    @Test
    public void someTests() {
        when(util.getServiceUrl("product")).thenReturn(URI.create("http://localhost:8080/test"));
        //Test code...
    }
}

I'm not sure if this is the best approach ("the Spring way"), but this worked for me.

This article made it all clear to me: http://rdafbn.blogspot.be/2014/01/testing-spring-components-with-mockito.html

You have to write a FactoryBean like

public class MockitoFactoryBean<T> implements FactoryBean<T> {

 private Class<T> classToBeMocked;

 public MockitoFactoryBean(Class<T> classToBeMocked) {
    this.classToBeMocked = classToBeMocked;
 }


 @Override
 public T getObject() throws Exception {
     return Mockito.mock(classToBeMocked);
 }

 @Override
 public Class<?> getObjectType() {
    return classToBeMocked;
 }

 @Override
 public boolean isSingleton() {
    return true;
 }
}

In your test-context.xml you have to add the following lines.

 <bean id="serviceUtilMock"  class="MockitoFactoryBean">
   <constructor-arg value="your.package.ServiceUtil" />
 </bean>

If you don't use XML configuration, then you have to add the equivalent to above in your Java configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM