Search results
Mock Spring's @Value with ReflectionTestUtils in Unit Tests
DefaultProvisioningService.java
:
@Service
@RequiredArgsConstructor
public class DefaultProvisioningService implements ProvisioningService {
@Value("${asimiotech.some-url}") // Configured in properties file
private String someUrl;
private final UserRepository userRepository;
// ...
}
Usage
DefaultProvisioningServiceTest.java
:
@ExtendWith(MockitoExtension.class)
public class DefaultProvisioningServiceTest {
@Mock
private UserRepository mockUserRepository;
private ProvisioningService provisioningService;
@BeforeEach
public void beforeEach() {
this.service = new DefaultDvdRentalService(this.mockUserRepository);
ReflectionTestUtils.setField(this.service, "someUrl", "http://some-mocked-url.com");
}
// ...
}