简体   繁体   中英

How to AutoWire spring beans when using Mockito and Junit?

I am trying to set up my class to be used in Junit .

However when I try to do the below I get an error.

Current Test Class:

public class PersonServiceTest {

    @Autowired
    @InjectMocks
    PersonService personService;

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

    }

    //tests

Error:

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

How can I fix this?

You are not mocking anything in your code. @InjectMocks sets a class where a mock will be injected.

Your code should look like this

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");
      }

Another solution is to use @ContextConfiguration annotation with static inner configuration class like so:

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);
        }
    }
}

Anyway, you need to mock something that the method you want to test uses inside to get desired behaviour of that method. It doesn't make sense to mock the service you're testing.

You're misunderstanding the purpose of the mock here.

When you mock out a class like this, you are pretending as if it's been injected into your application. That means you don't want to inject it!

The solution to this: set up whatever bean you were intending to inject as @Mock , and inject them into your test class via @InjectMocks .

It's unclear where the bean you want to inject is since all you have is the service defined, but...

@RunWith(MockitoJUnitRunner.class);
public class PersonServiceTest {

    @Mock
    private ExternalService externalSvc;

    @InjectMocks
    PersonService testObj;
}

如果我没记错的话......拇指规则是你不能同时使用两者......你要么使用 MockitojunitRunner 或 SpringJUnitRunner 运行单元测试用例,你不能同时使用它们。

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