模拟:when() 需要一个必须是 'a method call on a mock' 的参数
Mock: when() requires an argument which has to be 'a method call on a mock'
我正在为我的 REST-API 编写单元测试,但实体创建模拟存在一些问题。我不知道如何模拟 EntityManager。我尝试了下面的示例,但出现错误。
我的控制器测试:
public class MControllerTest {
private MockMvc mockMvc;
@InjectMocks A a;
@InjectMocks B b;
@InjectMocks AController aController;
@InjectMocks private AServiceImpl aServiceImpl;
@Autowired WebApplicationContext webApplicationContext;
@Autowired private FilterChainProxy springSecurityFilterChain;
@Autowired
@InjectMocks
private EntityManagerFactory entityManagerFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilter(springSecurityFilterChain)
.build();
}
@Test
public void postATest() throws Exception {
a.setDDDD("XXX");
EntityManagerFactory entityManagerFactory = webApplicationContext.getBean(EntityManagerFactory.class);
EntityManager entityManager = entityManagerFactory.createEntityManager();
when(this.entityManagerFactory.createEntityManager()).thenReturn(entityManager);
when(aServiceImpl.createEntity(isA(A.class))).thenReturn(a);
b.setCCCC;
a.setMovieTranslations(Arrays.asList(b));
when(aServiceImpl.createEntity(isA(B.class))).thenReturn(a);
mockMvc.perform(post("/path")
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
}
创建实体方法:
public Object createEntity(T t) {
try {
entityManager.persist(t);
return t;
} catch (IllegalArgumentException | EntityExistsException | ...
}
错误日志:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
at com.x.server.controller.MControllerTest.postATest(MControllerTest.java:121)
当我没有在 EntityManager 上注入模拟对象时,我通过 persist 方法得到了一个空指针异常,错误日志如下:
java.lang.NullPointerException: null
at com.x.server.serviceImpl.AManageServiceImpl.createEntity(AManageServiceImpl.java:45)
at com.eza.server.controller.MControllerTest.postATest(MControllerTest.java:123)
有人可以帮助我吗?
解决。我刚刚删除了一些带有 entitymanager 接口的代码并且它可以工作。
public class MControllerTest {
private MockMvc mockMvc;
@InjectMocks A a;
@InjectMocks B b;
@InjectMocks AController aController;
@Autowired WebApplicationContext webApplicationContext;
@Autowired private FilterChainProxy springSecurityFilterChain;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilter(springSecurityFilterChain)
.build();
}
@Test
public void postMovieTest() throws Exception {
a.setDDDD("XXX");
b.setCCCC;
a.setMList(Arrays.asList(b));
mockMvc.perform(post("/path")
.content(asJsonString(a))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
}
asJsonString 是我自己写的转换对象的方法 json
干杯
我正在为我的 REST-API 编写单元测试,但实体创建模拟存在一些问题。我不知道如何模拟 EntityManager。我尝试了下面的示例,但出现错误。
我的控制器测试:
public class MControllerTest {
private MockMvc mockMvc;
@InjectMocks A a;
@InjectMocks B b;
@InjectMocks AController aController;
@InjectMocks private AServiceImpl aServiceImpl;
@Autowired WebApplicationContext webApplicationContext;
@Autowired private FilterChainProxy springSecurityFilterChain;
@Autowired
@InjectMocks
private EntityManagerFactory entityManagerFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilter(springSecurityFilterChain)
.build();
}
@Test
public void postATest() throws Exception {
a.setDDDD("XXX");
EntityManagerFactory entityManagerFactory = webApplicationContext.getBean(EntityManagerFactory.class);
EntityManager entityManager = entityManagerFactory.createEntityManager();
when(this.entityManagerFactory.createEntityManager()).thenReturn(entityManager);
when(aServiceImpl.createEntity(isA(A.class))).thenReturn(a);
b.setCCCC;
a.setMovieTranslations(Arrays.asList(b));
when(aServiceImpl.createEntity(isA(B.class))).thenReturn(a);
mockMvc.perform(post("/path")
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
}
创建实体方法:
public Object createEntity(T t) {
try {
entityManager.persist(t);
return t;
} catch (IllegalArgumentException | EntityExistsException | ...
}
错误日志:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
at com.x.server.controller.MControllerTest.postATest(MControllerTest.java:121)
当我没有在 EntityManager 上注入模拟对象时,我通过 persist 方法得到了一个空指针异常,错误日志如下:
java.lang.NullPointerException: null
at com.x.server.serviceImpl.AManageServiceImpl.createEntity(AManageServiceImpl.java:45)
at com.eza.server.controller.MControllerTest.postATest(MControllerTest.java:123)
有人可以帮助我吗?
解决。我刚刚删除了一些带有 entitymanager 接口的代码并且它可以工作。
public class MControllerTest {
private MockMvc mockMvc;
@InjectMocks A a;
@InjectMocks B b;
@InjectMocks AController aController;
@Autowired WebApplicationContext webApplicationContext;
@Autowired private FilterChainProxy springSecurityFilterChain;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilter(springSecurityFilterChain)
.build();
}
@Test
public void postMovieTest() throws Exception {
a.setDDDD("XXX");
b.setCCCC;
a.setMList(Arrays.asList(b));
mockMvc.perform(post("/path")
.content(asJsonString(a))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
}
asJsonString 是我自己写的转换对象的方法 json
干杯