Hi,
I have an interface as follows:
public interface ServiceHandler
{
MyDTO<AddressDTO> getAllCopies(String status, Long id);
......
}
I have a spring component as follows:
public class Publisher
{
private final ServiceHandler serviceHandler;
@Autowired
public Publisher(ServiceHandler serviceHandler)
{
this.serviceHandler = serviceHandler;
}
@Transactional
public void publishNow(Long id)
{
MyDTO<AddressDTO> mydto = serviceHandler.getAllCopies(Constants.MY_STATUS, id);
.......
}
}
My Spock testing groovy class is as follows:
class MySpockTest extends Specification
{
private ServiceHandler serviceHandler = Mock(ServiceHandler)
private Publisher publisher
def setup()
{
publisher = new Publisher(serviceHandler)
}
def 'My Test'()
{
given:
long id = 1L
serviceHandler.getAllCopies(_) >> new MyDTO<AddressDTO>() //This should return MyDTO object but, it is returning NULL when I run the test in Publisher class
when:
publisher.publishNow(id)
then:
1 * serviceHandler.getAllCopies(id)
}
}
As mentioned in my comment above, instead of returning MyDTO object, it is returning null. Somehow, it is not returning the stubbed value.
Can someone help me what is wrong?
Thank you.