This is the my UploadClass. And it uploads a file to the S3 bucket using S3Client.
public class UploadClass{
@Override
public void upload(){
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(length);
PutObjectResult putObjectResult =
this.s3Client.putObject(container, object, data, metadata);
}
}
This is the MyClass and here I have injected the the UploadClass via constructor injection and have used the upload() method of UploadClass.
@Service
public class MyClassServiceImpl implements MyClassService{
private final UploadClass uploadClass;
@Autowired
MyClass(final UploadClass uploadClass) {
this.uploadClass = uploadClass;
}
@Override
public void process() {
......................Some other work ...................
.....................................................
......................................................
this.uploadClass.upload();
}
}
Now I am trying to write Unit tests for the process method in MyClass.And I need to mock the UploadClass using mockito and need to use mock output value for upload() method.This is my MyClassServiceTest class.How can I do this ?
@SpringBootTest
public class MyClassServiceTest{
@Test
void processTest() {
}
}
CodePudding user response:
With given code there is no much to test - only if upload method has been called.
@SpringBootTest
public class MyClassServiceTest{
@Autowired
private MyClassService myService;
@MockBean
private UploadClass uploadClass
@Test
void processTest() {
myService.process();
//do some assertions
//.....
verify(uploadClass,times(1)).upload(); //assert that uplaod has been called
}
This could be done using plain JunitRunner and Mockito. No need for complete app context.