Spring MockBean注释“原因”;没有定义MockitoPostProcessor类型的限定bean;
我发现如果“旧样式”,如Spring MockBean注释“原因”;没有定义MockitoPostProcessor类型的限定bean;,spring,Spring,我发现如果“旧样式”,如RunWithannotation和SpringApplicationConfigurationannotation,与“新样式”MockBeanannotation混合在一起,它看起来像: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(TestApplication.class) @FixMethodOrder(value = MethodSorters.NAME_ASCE
RunWith
annotation和SpringApplicationConfiguration
annotation,与“新样式”MockBean
annotation混合在一起,它看起来像:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(TestApplication.class)
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class SomeUnitTest {
@InjectMocks
@Autowired
private IWantTestThisService iWantTestThisService;
@MockBean
private DependencyInAboveService dependencyInAboveService;
@Test
public void testSomething() {
// your test
}
}
将出现例外情况:org.springframework.beans.factory.NoSuchBean定义异常:未定义类型为[org.springframework.boot.test.mock.mockito.mockito postprocessor]的合格bean
若要修复此异常,应将代码升级到
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
@FixMethodOrder(value = MethodSorters.NAME_ASCENDING)
public class SomeUnitTest {
@Autowired
private IWantTestThisService iWantTestThisService;
@MockBean
private DependencyInAboveService dependencyInAboveService;
@Test
public void testSomething() {
// your test
}
}
从
@Mock
更改为@MockBean
实际上解决了我的问题。我不明白为什么这个回答被否决了