java 使用 Mockito 和 Junit 时如何 AutoWire spring beans?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37053048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to AutoWire spring beans when using Mockito and Junit?
提问by java123999
I am trying to set up my class to be used in Junit
.
我正在尝试将我的课程设置为在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?
我怎样才能解决这个问题?
采纳答案by Kepa M.
You are not mocking anything in your code. @InjectMocks sets a class where a mock will be injected.
你不是在嘲笑你的代码中的任何东西。@InjectMocks 设置一个将注入模拟的类。
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");
}
回答by Enigo
Another solution is to use @ContextConfiguration
annotation with static inner configuration class like so:
另一种解决方案是使用@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);
}
}
}
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.
无论如何,您需要模拟要测试的方法在内部使用的内容,以获得该方法的所需行为。模拟您正在测试的服务是没有意义的。
回答by Makoto
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
.
解决方案:设置您打算注入的任何 bean @Mock
,然后通过 将它们注入到您的测试类中@InjectMocks
。
It's unclear where the bean you want to inject is since all you have is the service defined, but...
不清楚您要注入的 bean 在哪里,因为您所拥有的只是已定义的服务,但是...
@RunWith(MockitoJUnitRunner.class);
public class PersonServiceTest {
@Mock
private ExternalService externalSvc;
@InjectMocks
PersonService testObj;
}
回答by dev
If I am not mistaken...the thumb rule is you cannot use both together..you either run unit test cases with using MockitojunitRunner or SpringJUnitRunner you cannot use both of them together.
如果我没记错的话......拇指规则是你不能同时使用两者......你要么使用 MockitojunitRunner 或 SpringJUnitRunner 运行单元测试用例,你不能同时使用它们。