【问题标题】:Testing expected method with EasyMock gets IllegalStateException使用 EasyMock 测试预期方法得到 IllegalStateException
【发布时间】:2016-04-07 02:23:26
【问题描述】:

我正在尝试测试服务类以查看它是否调用了存储库的正确方法。存储库只是从 CouchDbRepositorySupport 扩展而来的

服务测试

@RunWith(EasyMockRunner.class)
@SpringApplicationConfiguration(App.class)
public class ServiceTest {

    @Rule
    public EasyMockRule mocks = new EasyMockRule(this);

    @TestSubject
    UserService userService = new UserServiceImpl();

    @Mock
    UserRepository userRepositoryMock;

@Test
    public void testGetUser() {
        User user = new User("Bob","bob87);

        user.setId("bob87"); //username is the id

        userService.getUser(user.getId());

        EasyMock.expect(userRepositoryMock.get(user.getId())).andReturn(user); //the line where the error occurs

        EasyMock.expectLastCall().times(1);

        EasyMock.replay(userRepositoryMock);

        EasyMock.verify(userRepositoryMock);
    }
}

但是我得到一个 IllegalStateException

java.lang.IllegalStateException:缺少行为定义 前面的方法调用: CompanyRepository.get("Optis") 用法是: 期望(a.foo()).andXXX()

【问题讨论】:

    标签: java testing easymock


    【解决方案1】:

    您需要告诉您的模拟要做什么并调用 replay() 调用调用此模拟的服务之前:

    public void testGetUser() {
        User user = new User("Bob","bob87);
        user.setId("bob87"); //username is the id
    
        EasyMock.expect(userRepositoryMock.get(user.getId()))
                .andReturn(user); 
        EasyMock.expectLastCall().times(1);
        EasyMock.replay(userRepositoryMock);
    
        userService.getUser(user.getId());
    
        EasyMock.verify(userRepositoryMock);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-27
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多