Skip to content

Instantly share code, notes, and snippets.

@eiswind
Created May 19, 2020 15:47
Show Gist options
  • Save eiswind/4363b5ccb4f0ac5df96912d5724c01a6 to your computer and use it in GitHub Desktop.
Save eiswind/4363b5ccb4f0ac5df96912d5724c01a6 to your computer and use it in GitHub Desktop.
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.mockito.AdditionalAnswers;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.JpaRepository;
import javax.persistence.Entity;
import javax.persistence.Id;
import static org.mockito.Mockito.*;
@SpringBootTest
class JpaTest {
@Entity
public static class MyEntity{
@Id
Long id;
}
@TestConfiguration
static class TestConfig{
@Primary
@Bean
MyRepository testBean(MyRepository real){
var mock = mock(MyRepository.class,
AdditionalAnswers.delegatesTo(real));
return mock;
}
}
@Autowired
ObjectProvider<MyRepository> repository;
@Test
void verifyFails() {
verify(repository.getIfAvailable(), times(0)).count();
}
}
interface MyRepository extends JpaRepository<JpaTest.MyEntity,Long>{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment