У меня есть один класс обслуживания, который я хочу смоделировать, но во время выполнения теста я получаю причину: 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 void test() throws Exception {
when(ts.decrypt("encryptedText")).thenReturn("decryptedText")
Request req = Request.builder().name("name123").build();
//written performPost method in some other class
ResultActions action = performPost("/test", req);
action.andExpect(status().isOk());
}
}
public class IntegrationTest {
protected final Gson mapper = new Gson();
private MockMvc mvc;
@Autowired
private WebApplicationContext context;
public ObjectMapper objectMapper = new ObjectMapper();
@Before
public void setup() {
this.mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build();
}}
Когда я вызываю метод расшифровки сервиса Thirdparty, он должен вернуть мне decryptedText в виде строки. Но получая дубликат ошибки определения макета