Created
May 19, 2020 15:47
-
-
Save eiswind/4363b5ccb4f0ac5df96912d5724c01a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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