【问题标题】:How to AutoWire spring beans when using Mockito and Junit?使用 Mockito 和 Junit 时如何 AutoWire 春豆?
【发布时间】:2016-05-05 14:19:12
【问题描述】:

我正在尝试将我的课程设置为在Junit 中使用。

但是,当我尝试执行以下操作时,出现错误。

当前测试类:

public class PersonServiceTest {

    @Autowired
    @InjectMocks
    PersonService personService;

    @Before
    public void setUp() throws Exception
    {
        MockitoAnnotations.initMocks(this);
        assertThat(PersonService, notNullValue());

    }

    //tests

错误:

org.mockito.exceptions.base.MockitoException: 
Cannot instantiate @InjectMocks field named 'personService'
You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : null

我该如何解决这个问题?

【问题讨论】:

    标签: java spring junit mockito autowired


    【解决方案1】:

    您没有在代码中模拟任何内容。 @InjectMocks 设置一个将注入模拟的类。

    您的代码应如下所示

    public class PersonServiceTest {
    
        @InjectMocks
        PersonService personService;
    
        @Mock
        MockedClass myMock;
    
        @Before
        public void setUp() throws Exception {
            MockitoAnnotations.initMocks(this);
            Mockito.doReturn("Whatever you want returned").when(myMock).mockMethod;
    
    
        }
    
        @Test()
          public void testPerson() {
    
             assertThat(personService.method, "what you expect");
          }
    

    【讨论】:

    • 我不明白,.doReturn 方法是做什么的?
    • 我想在这里你会找到一个关于模拟过程如何进行的很好的解释。如果您在阅读后还有其他问题,我会尽力帮助您。 dzone.com/articles/use-mockito-mock-autowired
    【解决方案2】:

    另一种解决方案是使用@ContextConfiguration 注释和静态内部配置类,如下所示:

    import static org.mockito.Mockito.mock;
    import static org.mockito.Mockito.when;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader = AnnotationConfigContextLoader.class)
    public class PersonServiceTest {
        @Autowired
        PersonService personService;
    
        @Before
        public void setUp() throws Exception {
            when(personService.mockedMethod()).thenReturn("something to return");
        }
    
        @Test
        public void testPerson() {
             assertThat(personService.method(), "what you expect");
        }
    
        @Configuration
        static class ContextConfiguration {
            @Bean
            public PersonService personService() {
                return mock(PersonService.class);
            }
        }
    }
    

    无论如何,您需要模拟您要测试的方法在内部使用的东西,以获得该方法的所需行为。模拟您正在测试的服务是没有意义的。

    【讨论】:

      【解决方案3】:

      您在这里误解了模拟的目的。

      当你像这样模拟一个类时,你假装它好像被注入到你的应用程序中。这意味着你不想注入它!

      解决方案:将您打算注入的任何 bean 设置为 @Mock,并通过 @InjectMocks 将它们注入您的测试类。

      不清楚您要注入的 bean 在哪里,因为您所拥有的只是定义的服务,但是...

      @RunWith(MockitoJUnitRunner.class);
      public class PersonServiceTest {
      
          @Mock
          private ExternalService externalSvc;
      
          @InjectMocks
          PersonService testObj;
      }
      

      【讨论】:

        【解决方案4】:

        如果我没记错的话......经验法则是你不能同时使用这两个......你要么使用 MockitojunitRunner 或 SpringJUnitRunner 运行单元测试用例,你不能同时使用它们。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-12
          • 2011-09-25
          • 1970-01-01
          • 2014-03-19
          • 1970-01-01
          • 2021-11-06
          相关资源
          最近更新 更多