This is my main code:
@Service
public class MainService {
public String mainMethod() {
SomeService someService = new SomeService("required");
// do Sth.
}
}
@Service
@RequiredArgsConstructor
public class SomeService {
@Autowired
private SomeOtherService someOtherService;
@Notnull
private final String requiredField;
// ...
}
@Service
public class SomeOtherService {
// just a bunch of public methods that might as well be static
}
And this is the test setup:
@RunWith(SpringRunner.class)
public class MainServiceTest {
@InjectMocks
private MainService mainService;
@Test
public void givenSth_whenSth_doSth() {
// test mainService.mainMethod();
}
}
Can you please tell me why someOtherService inside SomeService is null?
I figured out when yo use Spring's injection, you should not use manual instantiation (new ...) at the same time; so maybe new SomeService("required")
is the problem? But then how do I inject field variables into SomeService if not by constructor call? I don't want to use the Builder because requiredField is supposed to be NotNull.