Spring @ContextConfiguration

前端 未结 3 2159
甜味超标
甜味超标 2021-02-20 04:56

I am running the next test:

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.bean         


        
3条回答
  •  孤城傲影
    2021-02-20 05:35

    Try

    @ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
    

    Honestly I would step away from the xml and go this route. Change

    @ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" })
    

    to

    @ContextConfiguration(classes = { FloorServiceTestConfig.class })
    

    And create the about class

    @Configuration
    public class FloorServiceTestConfig
    {
        @Bean
        public FloorService floorService()
        {
              return new FloorService();
        }
    }
    

    This way when you need to mock your beans for class you're not testing it looks like below

    @Configuration
    public class FloorServiceTestConfig
    {
        @Bean
        public FloorService floorService()
        {
              return Mockito.mock(FloorService.class);
        }
    }
    

提交回复
热议问题