when(kc.getToken()).thenReturn(token);
не работает, и я получаю следующую ошибку от ServiceImplementation:
java.lang.NullPointerException: Cannot invoke "services.KeycloakService.getToken()" because "this.kc" is null
Я использую следующие зависимости:
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mockito:mockito-core:5.14.2'
testImplementation 'org.mockito:mockito-junit-jupiter:5.14.2'
ЧТО Я ПРОБОВАЛ:
- запустите «testMockInjection»: все тесты зеленые.
- запустите testUpdate: я вижу, что код реализации Сервиса останавливается на строке
и выдает вышеупомянутую ошибку
- сообщение в настройке печатается как положено
- Я попробовал использовать шпиона вместо макета, но это снова не сработало. Кроме того, насколько я понимаю, лучше использовать макет, потому что я хочу «подделать» весь сервис и мне нужно заглушить только одну функцию.
- Тестовый импорт – правильный вариант
- Тестовый импорт
- Тестовый импорт
- li>
Я использую @ExtendWith(MockitoExtension.class), @BeforeEach и @InjectMocks, которые работают в других моих тестах.
КОД РЕАЛИЗАЦИИ СЕРВИСА
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
...
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
...
import services.AasService;
import services.KeycloakService;
@Service
public class AasServiceImpl implements AasService{
private final RestTemplate restTemplate;
@Autowired
private KeycloakService kc;
@Value("${ass.server}")
private String aasUri;
@Value("${ass.id}")
private String aasIdShort;
public AasServiceImpl (RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@Override
public void update(String applicationUri, String nodeId, JSONObject value) {
System.out.println(applicationUri);
System.out.println(nodeId);
System.out.println(value);
String submodelElementValueId = applicationUri.replace(":", "_") + nodeId.replace("i=", "");
System.out.println(submodelElementValueId);
if (kc instanceof KeycloakServiceImpl) {
System.out.println("This is original service.");
} else {
System.out.println("This is not the original service.");
//this is the option printed
}
//problematic line is the following
String zaToken = kc.getToken();
//this is not printed in the code
System.out.println(zaToken);
try {
updateValue("DefectDetectionSkill", "Inputs", submodelElementValueId, value, zaToken);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
ТЕСТОВЫЙ КОД:
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
...
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
...
import org.junit.jupiter.api.BeforeEach;
import services.KeycloakService; //this is an interface
@ExtendWith(MockitoExtension.class)
public class AasServiceImplTest {
@Mock
private KeycloakService kc;
@Mock
private RestTemplate restTemplate;
@InjectMocks
private AasServiceImpl aasService;
@BeforeEach
void setUp() {
String token = "mockToken";
when(kc.getToken()).thenReturn(token);
ResponseEntity response= new ResponseEntity("", HttpStatus.OK);
when(restTemplate.exchange(anyString(), any(), any(), eq(String.class))).thenReturn(response);
System.out.println("set up runs");
//this message is printed
}
/*
@Test
void testMockInjection() {
assertNotNull(kc, "KeycloakService mock is not injected!");
assertNotNull(restTemplate, "RestTemplate mock is not injected!");
assertNotNull(aasService, "AasServiceImpl is not initialized!");
}
*/
@Test
void testUpdate() {
//Arrange
String applicationUri = "http://mock_uri";
String nodeId="i=ioanna";
JSONObject value = new JSONObject();
value.put("Value", 1234);
//Act
aasService.update(applicationUri, nodeId , value);
System.out.println("Value: " + value.get("Value"));
//Assert
assertNotNull(kc, "KeycloakService mock is not injected!");
//verify(kc, times(1)).getToken();
//verify(restTemplate, atLeastOnce()).exchange(anyString(), any(), any(), eq(String.class));
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... s-not-work
Мобильная версия