我是Mockito的新手,并且在Spring Data Web Support方面遇到了一些问题。
我的控制器中有以下功能。
/?id=1
这里发生的事情(在实际用例中)被描述为here。
问题是,当我在我的unitTests中使用MockMvc和Mockito时,没有数据库,也没有持久性存储库。我如何让Spring使用mockRepository来替换我选择的MyObject-instance在import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.mine.OrgUnitController;
import com.mine.OrgUnitService;
@RunWith(SpringJUnit4ClassRunner.class)
public class OrgUnitControllerTest2 {
private MockMvc mockMvc;
@Mock
SomeService someService;
@InjectMocks
MyController controller;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void test_delete() throws Exception {
mockMvc.perform(delete("/?id=1")).andExpect(status().isOk());
}
}
中移交的id。
这就是测试的样子:
int isPalindrome(string A) {
string::iterator it;
string::reverse_iterator rit;
it=A.begin();
rit=A.rbegin();
while(it!=A.end() && rit!=A.rend()){
while(rit != A.rend() && !isalnum(*rit)) //if char from the end is not alphanumeric, then increment the reverse iterator till we find the alphanumeric char.
++rit;
while(it != A.end() && !isalnum(*it)) //if char from the start is not alphanumeric, then increment the iterator till we find the alphanumeric char.
++it;
if (it == A.end() || rit == A.rend())
break;
if(tolower(*it)!=tolower(*rit)) //case in-sensitive comparison
return 0;
++it;
++rit;
}
return 1;
}
我真的很感激任何帮助。
答案 0 :(得分:0)
要为给定的ID返回MyObject,您可以使用when(….).thenReturn(….)
。因此,当您在SomeService中使用一个方法来按ID查找MyObject时,您可以将其存根并返回值。
// When the get method is called, it will return MyObject
when(someService.get(id)).thenReturn(new MyObject());
答案 1 :(得分:0)
如果你只使用Mockito
代替Mockito
+ MockMvc
,我认为这会更容易。
你可以这样做:
@RunWith(SpringJUnit4ClassRunner.class)
public class OrgUnitControllerTest2 {
@Mock
SomeService someService;
@InjectMocks
MyController controller;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test_delete() throws Exception {
//given
given(someService.delete(1)).willReturn(true); //or successful response
//when
MyObject response = controller.delete(1);
//then
assertTrue(OK, response.getStatus())
}
}
答案 2 :(得分:0)
这个解决方案非常简单,只花了我2天。 ;)
为了使SpringDataWebSupport工作,使用org.springframework.data.repository.support.DomainClassConverter
是一个ConversionService。接下来要知道的是,MockMvc通过setConversionService()
接受ConversionService。所以剩下要做的就是编写我自己的ConversionService,这实际上非常简单。我像这样延长了DefaultFormattingConversionService
:
public class MockSpringDataWebSupport extends DefaultFormattingConversionService {
/**
* @param converters {@link Converter Converters} that shall be added to this {@link ConversionService}
*/
@SafeVarargs
public MockSpringDataWebSupport(Converter<? extends Object, ? extends Object>... converters) {
for (Converter<? extends Object, ? extends Object> converter : converters) {
this.addConverter(converter);
}
}
}
这时我可以移交任何符合我测试需求的coustom转换器。在这个特殊情况下我用过:
mockMvc = MockMvcBuilders.standaloneSetup(controller)
.setConversionService(new MockSpringDataWebSupport(new Converter<String, MyObject>() {
@Override
public OrgUnit convert(String source) {
return new MyObject();
}
})).build();
它有效。
答案 3 :(得分:0)
您可能需要在setCustomArgumentResolvers
对象中设置MockMvc
,如下所示:
@Before
public void init() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(controller)
.setCustomArgumentResolvers(new HandlerMethodArgumentResolver(){...})
.build();
}
您需要确定哪个类正在实施HandlerMethodArgumentResolver
,然后在上面的init()
方法中使用它。
您可以查看this资源。
答案 4 :(得分:0)
我如何让Spring用我选择的MyObject-instance替换一个micRepository来替换在/?id = 1中传递的id
您可以通过模拟WebConversionService
来做到这一点,这将避免通过实体存储库findById
查找数据库。您还可以设置要使用的实例,而无需从数据库中获取它。有关如何模拟此服务的更多详细信息,请查看this answer。