Issue
I have a class:
public class MyClass{
MyClass(
@Value("${my.protocol}") String protocol,
@Value("${my.host}") String host,
@Value("${my.port}") int port,
@Autowired MyService myservice) {
........
}
....
}
I then wrote a test that uses Mockito:
@ExtendWith(MockitoExtension.class)
class MyClassTest {
@Mock
private MyService myservice;
@InjectMocks
private MyClass myClass;
....
}
The test failed with this:
org.mockito.exceptions.base.MockitoException:
Cannot instantiate @InjectMocks field named 'myClass'! Cause: the type 'MyClass ' has no default constructor
You haven't provided the instance at field declaration so I tried to construct the instance.
Examples of correct usage of @InjectMocks:
@InjectMocks Service service = new Service();
@InjectMocks Service service;
//and... don't forget about some @Mocks for injection :)
I think this is because I only provided one of the 4 construction parameters, not the other three which has @Value annotation.
Can someone please let me know how I can inject the three @Value construction parameters in order for this to work?
I use Junit 5 and Mockito.
Solution
Annotation-based magic is showy, but it's not the solution to every problem. Just do things the old-fashioned way:
@BeforeEach
void setup() {
this.subject = new MyClass("http", "localhost", 5000, mockService);
}
Answered By - chrylis -cautiouslyoptimistic-
Answer Checked By - Clifford M. (JavaFixing Volunteer)