2015-07-10 28 views
0

如何在此junit测试中使用模拟对象? 我需要在junit中实现模拟对象。 需要关于此代码的帮助。如何在此junit中使用模拟对象

public class TicketTest { 
public static List<Ticket> tickList = new ArrayList<Ticket>(); 


@Before 
public void setup() throws Exception { 
    MockitoAnnotations.initMocks(this); 

} 

@Test 
public void objectValueSetting() throws Exception { 
    Ticket ticket = new Ticket(); 
    ticket.setDescription("helllo"); 
    ticket.setEmail("[email protected]"); 
    tickList.add(ticket); 

} 

@Test 
public void valueGetting() throws Exception { 

    Ticket ticket = tickList.get(0); 
    Assert.assertNotNull(ticket); 
    Assert.assertNotNull(ticket.getDescription()); 
    Assert.assertNotNull(ticket.getEmail()); 

} 

}

+1

为什么你需要嘲笑任何东西?你想要测试什么?你的问题目前还不清楚。请阅读http://tinyurl.com/stack-hints –

回答

1

你想怎样嘲笑? (我无法评论。所以在这里写吧) 如果你想嘲笑票务类,则代码看起来像

` @Test 公共无效valueGetting()抛出异常{

Ticket mock= Mockito.mock(Ticket.class); 
    when(mock.getDescription()).thenReturn("hello"); 
    when(mock.getEmail()).thenReturn("[email protected]"); 
    Assert.assertNotNull(mock); 
    Assert.assertNotNull(mock.getDescription()); 
    Assert.assertNotNull(mock.getEmail()); 

} 

` PS:我正在使用mockito。

+0

它显示此错误当(字符串)未定义为类型TicketTest @AarYem –

+0

的方法请检查您是否已完成静态导入即'import static org.mockito。 Mockito。*;'这样你可以使用方法 – AarYem