5

My UITest project works well with original web server, but I want to replace it with mock server using WireMock.net. I already used it in a non-UITest project successfully. Here's my code in UI Test project:

    [SetUp]
    public void BeforeEachTest()
    {
        app = AppInitializer.StartApp(platform);
    }
    
    [OneTimeSetUp]
    public void InitializerOnce()
    {
        _mockServer = WireMockServer.Start(new WireMockServerSettings()
        {
            Urls = new[] { "http://localhost:12345/"},
            ReadStaticMappings = true
        });
    
        _mockServer.Given(
        Request.Create().WithPath("*").UsingAnyMethod())
            .RespondWith(Response.Create()
        .WithStatusCode(HttpStatusCode.OK).WithBody("Sample response!"));
    }
    
    [OneTimeTearDown]
    public void DisposeOnce()
    {
        _mockServer?.Stop();
    }
    
    [Test]
    public async Task Test()
    {
        app.Tap(c => c.Marked("MyButton"));
    
        await Task.Delay(5000);
    
        //Assert
        Assert.IsTrue(true);
    }
    
    private WireMockServer _mockServer;

And my main Android project has this code:

    <Button AutomationId="MyButton" Text="Action" Clicked="Action_OnClicked"/>

    private async void Action_OnClicked(object sender, EventArgs e)
    {
        try
        {
            var client = new HttpClient();
    
            var response = await client.GetAsync("http://10.0.2.2:12345/test");// is it a correct url???
            var result = await response.Content.ReadAsStringAsync();
    
            await UserDialogs.Instance.AlertAsync($"Status:{response.StatusCode}, result:{result}");
        }
        catch (Exception exception)
        {
            await UserDialogs.Instance.AlertAsync(exception.ToString());
        }
    }

And here's the result of tapping the button:

enter image description here

VahidShir
  • 2,066
  • 2
  • 17
  • 27

1 Answers1

0

In the unit-tests, run WireMock.Net on a random port instead of fixed port. Else you can encounter issues when running these unit-tests on a build-server, there is not 100% guarantee that this port will be free on the OS.

My suggestion would be to start the WireMock.Net without a fixed port. Just like:

var server = WireMockServer.Start();

// Getting the random port on which the WireMock.Net server is running, can be done like:
var port = server.Ports[0];

// Or you can also get the full URL where the WireMock.Net server is running with:
var url = server.Urls[0];

So in your case use:

var url = _mockServer.Urls[0];
var response = await client.GetAsync(url + "/test");
Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121
  • I don't think it's a port issue as I could use WireMock.Net successfully on an integration test on Android emulator and device. – VahidShir Sep 17 '20 at 11:25
  • Are you sure the WireMock.Net server is still running at the moment your press that button? (Like : is WireMock.Net running as standalone, web-api or docker ?) – Stef Heyenrath Sep 21 '20 at 16:14