Java Mockito-另一个对象的验证';s方法在目标类方法内调用
我正在为一个涉及cookie的方法编写一个单元测试。这里是方法Java Mockito-另一个对象的验证';s方法在目标类方法内调用,java,unit-testing,junit,mockito,Java,Unit Testing,Junit,Mockito,我正在为一个涉及cookie的方法编写一个单元测试。这里是方法 public Boolean addPropertyToUserWishlist(HttpServletRequest request, HttpServletResponse response, String propId) throws IOException { String message = ""; try { Cookie[] cookies = request.getCooki
public Boolean addPropertyToUserWishlist(HttpServletRequest request,
HttpServletResponse response, String propId) throws IOException {
String message = "";
try {
Cookie[] cookies = request.getCookies();
if (checkCookieValueExists(cookies, propId)) {
message = "Already added to favourites!";
} else {
if (checkCookieExists(cookies)) {
for (Cookie cookie : cookies) {
if ("favourites".equals(cookie.getName())) {
cookie.setValue(cookie.getValue() + "_" + propId);
cookie.setMaxAge(60 * 60 * 24 * 365 * 5);
response.addCookie(cookie);
message = "Added to Favourites!";
break;
}
}
} else {
Cookie c = new Cookie("favourites", propId);
c.setMaxAge(60 * 60 * 24 * 365 * 5);
response.addCookie(c);
message = "Added to Favourites!";
}
}
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
@Test
public void testAddPropertyToUserWishlist() throws Exception {
System.out.println("addPropertyToUserWishlist");
//decleration
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Cookie c = mock(Cookie.class);
Cookie[] cookies = new Cookie[2];
String propId = "5"; //testing the for property with id 5
WishlistController instance = Mockito.spy(new WishlistController());
// training
Mockito.doReturn(false).when(instance).checkCookieExists(cookies);
Mockito.doReturn(false).when(instance).checkCookieValueExists(cookies, propId);
when(request.getCookies()).thenReturn(cookies);
// testing
Boolean result = instance.addPropertyToUserWishlist(request, response, propId);
//verify(response, times(1)).addCookie(c);
assertTrue(result);
}
下面是该方法的单元测试
public Boolean addPropertyToUserWishlist(HttpServletRequest request,
HttpServletResponse response, String propId) throws IOException {
String message = "";
try {
Cookie[] cookies = request.getCookies();
if (checkCookieValueExists(cookies, propId)) {
message = "Already added to favourites!";
} else {
if (checkCookieExists(cookies)) {
for (Cookie cookie : cookies) {
if ("favourites".equals(cookie.getName())) {
cookie.setValue(cookie.getValue() + "_" + propId);
cookie.setMaxAge(60 * 60 * 24 * 365 * 5);
response.addCookie(cookie);
message = "Added to Favourites!";
break;
}
}
} else {
Cookie c = new Cookie("favourites", propId);
c.setMaxAge(60 * 60 * 24 * 365 * 5);
response.addCookie(c);
message = "Added to Favourites!";
}
}
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
@Test
public void testAddPropertyToUserWishlist() throws Exception {
System.out.println("addPropertyToUserWishlist");
//decleration
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
Cookie c = mock(Cookie.class);
Cookie[] cookies = new Cookie[2];
String propId = "5"; //testing the for property with id 5
WishlistController instance = Mockito.spy(new WishlistController());
// training
Mockito.doReturn(false).when(instance).checkCookieExists(cookies);
Mockito.doReturn(false).when(instance).checkCookieValueExists(cookies, propId);
when(request.getCookies()).thenReturn(cookies);
// testing
Boolean result = instance.addPropertyToUserWishlist(request, response, propId);
//verify(response, times(1)).addCookie(c);
assertTrue(result);
}
我想测试是否创建了cookie。因为我没有在浏览器中执行此代码。那么,我如何验证创建新cookie的构造函数是否被调用,方法
setMaxAge
是否被调用过一次呢?由于cookie
是在被测方法中创建的,因此您实际上没有任何方法控制其行为,因此也没有理由对其进行模拟
相反,如果调用了response.addCookie
并捕获了作为参数传入的cookie,请验证
从那里,断言它的值(如果已知)
//Arrange
//...code removed for brevity
// Act
Boolean result = instance.addPropertyToUserWishlist(request, response, propId);
//Assert
ArgumentCaptor<Cookie> cookieCaptor = ArgumentCaptor.forClass(Cookie.class);
//Same as verify(response, times(1)) and captures the passed argument
verify(response).addCookie(cookieCaptor.capture());
Cookie cookie = cookieCaptor.getValue();
int expectedAge = 60 * 60 * 24 * 365 * 5;
String expectedName = "favourites";
assertEquals(expectedAge, cookie.getMaxAge());
assertEquals(expectedName, cookie.getName());
assertEquals(propId, cookie.getValue());
//排列
//…为简洁起见,删除了代码
//表演
布尔结果=instance.addPropertyToUserWishlist(请求、响应、propId);
//断言
ArgumentCaptor cookieCaptor=ArgumentCaptor.forClass(Cookie.class);
//与verify(response,times(1))相同,并捕获传递的参数
验证(response).addCookie(cookieCaptor.capture());
Cookie Cookie=cookieCaptor.getValue();
int expectedAge=60*60*24*365*5;
字符串expectedName=“收藏夹”;
assertEquals(expectedAge,cookie.getMaxAge());
assertEquals(expectedName,cookie.getName());
assertEquals(propId,cookie.getValue());
参考将cookie创建移动到新方法:
} else {
Cookie c = createNewCookie("favourites", propId);
c.setMaxAge(60 * 60 * 24 * 365 * 5);
...
...
...
protected Cookie createNewCookie(String name, String id){
return new Cookie(name, id);
}
然后在单元测试中验证是否调用了此方法:
@Mock
HttpServletRequest request;
@Mock
HttpServletResponse response;
@BeforeTest
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testAddPropertyToUserWishlist() throws IOException {
WishlistController testedObject = Mockito.spy(new WishlistController());
Boolean outcome = testedObject.addPropertyToUserWishlist(request, response, "propid");
Mockito.verify(testedObject).createNewCookie("favourites","propid");
}
如果要测试在新cookie上完成的操作,可以重写此方法,并通过以下方式将自己的cookie实例注入测试类:
@Mock
Cookie myNewCookie;
@Test
public void testAddPropertyToUserWishlist() throws IOException {
WishlistController testedObject = Mockito.spy(new WishlistController(){
@Override
protected Cookie createNewCookie(String id, String name){
return myNewCookie;
}
});
Boolean outcone = testedObject.addPropertyToUserWishlist(request, response, "propid");
Mockito.verify(testedObject).createNewCookie("favourites","propid");
Mockito.verify(myNewCookie, Mockito.times(1)).setMaxAge(60 * 60 * 24 * 365 * 5);
}
验证是否调用了response.addCookie
,并捕获了在参数中传递的cookie。如果你知道的话,坚持它的价值。非常感谢你,感谢你抽出时间。成功了。