Java 如何模拟一个积欠积欠的电话?

Java 如何模拟一个积欠积欠的电话?,java,spring,spring-mvc,mocking,mockito,Java,Spring,Spring Mvc,Mocking,Mockito,我试图在SpringMVC应用程序上进行一个简单的控制器测试 我有这个考试班 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestContext.class, WebAppConfig.class}) @WebAppConfiguration public class NotificacaoControllerTest { private MockMvc mockMvc; @

我试图在SpringMVC应用程序上进行一个简单的控制器测试

我有这个考试班

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestContext.class, WebAppConfig.class})
@WebAppConfiguration
public class NotificacaoControllerTest {

    private MockMvc mockMvc;

    @Autowired
    private NotificacaoRepository notificacaoRepositoryMock;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setUp() {
        //We have to reset our mock between tests because the mock objects
        //are managed by the Spring container. If we would not reset them,
        //stubbing and verified behavior would "leak" from one test to another.
        Mockito.reset(notificacaoRepositoryMock);

        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void add_NewTodoEntry_ShouldAddTodoEntryAndRenderViewTodoEntryView() throws Exception {
        Notificacao added = new Notificacao(123,"resource","topic", "received", "sent");

        when(NotificacaoRepository.save( added )).thenReturn(added);
我的TestContext类有这个bean

@Bean
public NotificacaoRepository todoService() {
    return Mockito.mock(NotificacaoRepository.class);
}
我的存储库就是这样

public interface NotificacaoRepository extends CrudRepository<Notificacao, Long> {
}
public interface notificacorepository扩展了crudepository{
}
但它甚至没有编译,我一直在得到“无法从最后一行的类型crudepository中对非静态方法save(Notificacao)进行静态引用”(notificacository.save(added))

我在这个链接中的一个例子中看到了这种用法,但他在那里使用了一个服务类,这与Crudepository的用法并不完全相同


我试图找到一个如何测试crudepository实现的示例,但没有找到,因此我认为这应该像其他模拟一样简单,因为save方法不是静态的,所以您必须更改

  when(NotificacaoRepository.save( added )).thenReturn(added);
要将对象用作:

when(notificacaoRepositoryMock.save( added )).thenReturn(added);