小编use*_*163的帖子

Mockito:调用方法里面的方法

我在其中有以下类和方法

public class A extends B implements C{


public void validateTicketGrantingTicket(final TicketGrantingTicket ticketGrantingTicket) throws InvalidTicketException {

    if (ticketGrantingTicket != null)
    {
        if (!ticketGrantingTicket.getHostDomain().equalsIgnoreCase(getServerName()))
        {
            throw new InvalidTicketException();
        }
    }
}


public String getServerName()
{
    String serverName = "";
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

    if (request != null)
    {
        serverName = request.getServerName().toLowerCase();
    }

    return serverName;
}
}
Run Code Online (Sandbox Code Playgroud)

现在我正在写ATest课并嘲笑A班.

public class ATest {
private A a;

@Before
public void init(){

    A = mock(A.class);

    when(A.getServerName()).thenReturn("phoenix.edu.abc");      
}


@Test
public void validateTicketGrantingTicketTest() throws  InvalidTicketException{  
    a …
Run Code Online (Sandbox Code Playgroud)

mockito

2
推荐指数
1
解决办法
4226
查看次数

标签 统计

mockito ×1