Junit测试案例

时间:2015-06-03 11:14:45

标签: junit

我想为下面的方法编写junit" createEmailTripEvent()",所以我试图通过使用EasyMock来模拟EmailTrip界面但是无法继续。

是否可以使用EasyMock进行模拟界面,或者我们只需要使用Mockito? 如果Mockito是唯一的选择,那么任何人都可以解释我们如何使用?

请参阅以下课程&接口

private EmailTrip emailTrip;

public ModelAndView createEmailTripEvent (HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    emailTrip.createEmailTripEvent(Code, emailRequest);
    //Need to write junit for above method
}

public interface EmailTrip {
    void createEmailTripEvent(String code, TripEmailRequest request)
            throws Exception;
}

public class EmailTripImpl implements EmailTrip { }

1 个答案:

答案 0 :(得分:0)

您应该在maven pom中包含以下依赖项(如果不使用maven,请紧急考虑这样做......)。否则,将EasyMock依赖项包含在类路径中。

<dependency>
  <groupId>org.easymock</groupId>
  <artifactId>easymock</artifactId>
  <version>3.3.1</version>
  <scope>test</scope>
</dependency>

之后你应该能够创建你的测试类。

import static org.easymock.EasyMock.*;
import org.easymock.*;
import org.junit.Rule;
import org.junit.Test;

public class MyTinyTest extends EasyMockSupport {
  @Rule
  public EasyMockRule rule = new EasyMockRule(this);

  @Mock
  private EmailTrip emailTrip;

  @TestSubject
  private final ServletClass servlet = new ServletClass(emailTrip);

  @Test
  public void testCreateEmailTripEvent() {
    // you should know how to create an email request instance!
    EmailTripRequest emailRequest = createRequest();
    // tell EasyMock what behavior is expected...
    emailTrip.createEmailTripEvent("code", emailRequest);

    // start testing...
    replayAll();
    // excute method to test...
    ModelAndView result = servlet.createEmailTripEvent(servletRequest, servletResponse);
    // verify if expected behavior occured.
    verifyAll();

    // check other assertions concerning the method result...
  }
}