Java 查看org.mockito.exceptions.misusing.NotAMockException的问题
查看spy和auto wire以及Extend(SpringExtension)的问题 参数应该是mock,但为:class org.springframework.data.jpa.repository.support.SimpleParepositoryJava 查看org.mockito.exceptions.misusing.NotAMockException的问题,java,junit,spring-boot-test,Java,Junit,Spring Boot Test,查看spy和auto wire以及Extend(SpringExtension)的问题 参数应该是mock,但为:class org.springframework.data.jpa.repository.support.SimpleParepository 当我使用spring boot:2.4.1时,我看到了上述问题。将TestRepo设为mock并在何时(repo.findById())写入规则。然后返回(某个模拟对象)有没有办法用测试H2数据库测试using spy,您想模拟存储库和测试
当我使用spring boot:2.4.1时,我看到了上述问题。将
TestRepo
设为mock
并在何时(repo.findById())写入规则。然后返回(某个模拟对象)
有没有办法用测试H2数据库测试using spy,您想模拟存储库和测试服务层还是与数据库和测试服务层集成?我们需要与数据库集成并测试服务层?您想实现什么?是单独测试类的单元测试还是包含整个Spring上下文的集成测试?如果您的目标是编写使用Spring测试上下文的集成测试,那么使用@MockBean
替换上下文中的bean。还请参考此内容以了解@Mock
和@MockBean
之间的区别。将TestRepo
设置为Mock
并在(repo.findById())时编写规则。然后返回(某个模拟对象)
是否有任何方法使用测试H2数据库测试spy,您想模拟存储库和测试服务层还是与数据库和测试服务层集成?我们需要与数据库集成并测试服务层?您想实现什么?是单独测试类的单元测试还是包含整个Spring上下文的集成测试?如果您的目标是编写使用Spring测试上下文的集成测试,那么使用@MockBean
替换上下文中的bean。还请参考此内容以了解@Mock
和@MockBean
之间的区别。
@ExtendWith(SpringExtention.class)
@SpringBootTest(classes = TestServer.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
@Transactional
SampleServiceTest {
@Spy
@Autowire
private TestRepo repo;
@Mock
SpecialTest test;
@Mock
SpecialTest1 test1;
@InjectMock
TestServiceTest testService;
@InjectMock
SampleServiceTest sampleServiceTest;
@BeforeEach()
void setup () {
openMocks(this);
when(testService.getId()).thenReturn(test);
when(testService.getId()).thenReturn(test1);
}
@Test
void test () {
}