0

I'm using rest-assured for integration testing on my endpoints. One of the endpoints has a dependency to another resource (it calls another API conditionally). Is there a way for me to stub out the API call so I can write a test for that case?

etang
  • 730
  • 8
  • 23

2 Answers2

0

Lets say your code calls endpointB internally via http, you can stub that api by using https://github.com/katta/fakerest or https://stubby4j.com .So when your code makes a call internally to the another api, it will hit these stubs, which will return a dummy response always. Hope this helps.

Alexander Zagniotov
  • 2,329
  • 1
  • 12
  • 9
vigneshre
  • 124
  • 5
  • They both require running another process while running the JUnit tests right? – etang Sep 20 '13 at 16:32
  • Yes, you have to start them before you run your rest assure test cases. As rest assure does not have mock or stub(as far as i know), we have to do this. – vigneshre Sep 20 '13 at 16:52
0
interface IDataProvider {
 string RetrieveData();
}

class StandardDataProvider : IDataProvider {
 public string RetrieveData(){
  // call to API
 }
}

class Program {
 private IDataProvider _dataProvider;

 public Program(IDataProvider provider = null) {
  _dataProvider = provider ?? new StandardProvider();
 }

 public void MethodToTest(){
  var data = _dataProvider.RetrieveData();
  // do your thing
 }
}

In the tests you can mock the data by creating your own IDataProvider object and working with its data.

class TestDataProvider : IDataProvider {
 public string RetrieveData(){
  return "my own data";
 }
}

class Test {
 [TestMethod]
 public void TestProgram(){
  var obj = new Program(new TestDataProvider);
  var result = obj.MethodToTest();
  // asserts
 }
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170