使用EasyMock测试预期方法会获得IllegalStateException

时间:2016-04-06 17:53:20

标签: java testing easymock

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

扩展而来

ServiceTest

library(tidyr)

df_tidy <- gather(df, measure, value, -year)

ggplot(data = df_tidy, aes(x = year, y = value)) +
  geom_point(size = 3) +
  geom_line() +
  facet_grid(measure ~ ., scales = "free_y")

但是我收到了IllegalStateException

  

java.lang.IllegalStateException:缺少行为定义   前一个方法调用:CompanyRepository.get(“Optis”)用法是:   期望(a.foo())。andXXX()

1 个答案:

答案 0 :(得分:2)

你需要告诉你的模拟做什么,并在调用调用此模拟的服务之前调用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);
}