小编Cod*_*der的帖子

获取 java.lang.IllegalStateException:在测试用例中使用 @MockBean 时重复模拟定义

我有一个要模拟的服务类,但是在运行测试时,我的原因是: java.lang.IllegalStateException: Duplicate mock definition [MockDefinition@482ba4b1 name = '', typeToMock = com.service.ThirdPartyService, extraInterfaces = set[[empty]], answer = RETURNS_DEFAULTS, serializable = false, reset = AFTER]

我尝试在类级别、字段级别使用 @MockBean 创建模拟服务,并使用 @Qualifier 来解决问题

@Service
public class ThirdPartyService{

.......................

    public String decrypt(String encryptedText) { 
    //third party SDK I am using
        return Service.decrypt.apply(encryptedText);
    }
.........
..............
}


    @ComponentScan("com")
    @PropertySource({"classpath:/api.properties", "classpath:/common.properties"})
    @SpringBootConfiguration
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
    @Transactional
    public class TestControllerTest extends IntegrationTest {
    @MockBean
    ThirdPartyService thirdPartyService;
    @Before
    public void initMocks(){
    MockitoAnnotations.initMocks(this);
    }

    @Test
    public …
Run Code Online (Sandbox Code Playgroud)

java-8

7
推荐指数
1
解决办法
4242
查看次数

标签 统计

java-8 ×1