How do you call a WCF service from a test runner? - wcf

I am getting into Selenium WebDriver to write black box integration tests. I'm using MSTest (for now) as a test runner. There's a point where I want to call a WCF service to support the work. I get an error:
Could not find default endpoint element that references contract 'Abc.AbcService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
I realize that calling external services has no business inside a unit test, so I want to emphasize that I'm only using MSTest as a convenient temporary home for automated ui tests, which simulate real life situations of someone using the entire application, front-to-back, using a website driver in tandem with web services.
I guess the problem has to do with finding the .config file when the application is the test runner. Or maybe it is a general problem of having a config file for code in a dll that is hosted in process that you don't control.

If you are calling the WCF service from your test project, you can just add a app.config file with the service configuration in you test project. The configuration will be used to configure the services when called (the app.config file will be deployed with your test dll).

Related

Getting coverage using OpenCover for Selenium tests

The background:
We have a project starting a service that gets controlled from the web interface GUI.
We're not using a specific (from a commercial point of view) web server, but an in-house created wrapper around the windows service that manages all the web interface interactions.
What we have:
Now we've started using Selenium & MSTest for testing the web interface and we're trying to get a coverage for these kind of tests, and OpenCover seemed to do the deal. The problem is that is not (or we're doing something different or wrong).
The only code coverage that I'm not getting is the one for the method used to start the windows service and all others that get called in the process (since I have all access to all the PDBs too), but afterwards nothing is covered, based on the action that take place from the Selenium's interaction with the browser.
Any hints/ideas or maybe other tools that are able to do the job (if even possible) are appreciated.
If you're running an ASP.net app, you're going to need to attach OpenCover to IIS or IISExpress to get accurate code coverage with selenium. That makes it a little hard to use MSTest with. You may want to consider moving as much logic into your services, and write unit tests against those.
Here's a quick example hot to attach open cover to IIS
OpenCover.Console.exe -target:C:\Windows\System32\inetsrv\w3wp.exe -targetargs:-debug
-targetdir:C:\Inetpub\wwwwoot\MyWebApp\bin\ -filter:+[] -register:user

SpringBoot: How to reload application.property file in few second without restart application?

I am working on a java web service application which is using SpringBoot framework. I put properties into the application.properties file parallel to the jar. Afterward, I restart the application and new properties are picked by web service.
Now my requirement is, my web service keep reload the property file after few time (lets say 5000ms) and I do not want to restart the web service. Is there any way in SpringBoot to achieve this?
Thanks,
Harsh
If you are using spring cloud starter dependencies, you will get a /refresh endpoint for this purpose to POST to. It refreshes the configuration from property files without interrupting the service.

How to startup and use WCF service in another project

I created a WCF Service Application project in VS 2012. Implmenented the service.
here is what it looks like:
Now I have a fundamental issue to deal with. I created a new Test project to create more TDD and also some TAD tests. The TAD (Tests after Implementation) tests will test the service implementation while my TDD test will test reliant on mocking my WCF Servcie Interface.
But now going back to my TAD tests which need to test using an implementation of the service. I take that is I'm adding a service reference and using the clientProxy in my unit tests for TAD.
So I add a service reference to a new C# Project. I then continue to create some tests such as:
[TestMethod]
public void Get_CalledViaClientProxy_ReturnsNonNullList()
{
var serviceClient = new CarsClient();
// Act
Cars[] events = serviceClient.Get();
// Assert
Assert.IsNotNull(events);
}
When I run this it fails because the actual service is not running. So what I've done is go back to the actual Service WCF project, right-click the service (.svc) and choose to open it in the browser which will also start the service under IIS Express. Then my tests run fine.
So from that, how do I make this more automated. If another dev downloads this code, those tests should run and the service should somehow be started. I tried to set my service project as the startup project but all that does is opens the browser to show me the file system for that project.
How do people run the service for testing other than in IIS? and make it so it just works for devs? If I can't get my service to run simply but pressing F5 somehow and still able to go back into my tests project to try and run unit tests then this is pointless.
When I simply try to set my "WCF Service Application" project to be the startup project, instead of just starting the service and sitting there with a console window, it opens a web page for the service. Do I have the wrong WCF project type? I noticed that I have a web tab because I've got a "Serviced Application" project, should I be using something else if I want to simply start up my project in VS and continue coding in other projects that utilize this running service?
At a previous company I was at, we all put a "-d" in the WCF service project which when you ran the project it would start the service and a console window would run and just sit there, then you knew the service was running while you continued on writing TAD unit tests against it
e.g.:
however again, my WCF project is a "WCF Application Service" so I don't have that exact debug tab in mine so not sure what to do and then how we were able to have this format of tabs in the WCF service at other places I've been. Maybe it's just a plain C# project, not a WCF project where we were putting the -d but then how was it starting the WCF service if that was the case? Not sure if it was a plain vanilla C# project that we put that -d in but I sure do not have this same tab format in my WCF Service Application project.
Update #2
Ok I just to see the diff, added a "WCF Service Library" project along side my "WCF Service Application" project. The tab now has that debug and now when I launch it it does start the service and the test client comes up because the VS template automatically has /client:"WcfTestClient.exe" in for the command line properties of the debug tab in the project properties.
Since this service is going to serve as a service API that will be used cross applications, maybe I shouldn't be using a "WCF Service Application" and should be using a "WCF Service Library" type of WCF project template.
Refer your wcf project from the same solution as your test project.
Right click solution -> set startup projects
choose multiple startup projects and change the action of your wcfservice from none to start or start without debugging. Play and test :)
Or start project without debugging, wcf service will be running and you can continue coding/building and testing.
EDIT:
Here is what you also could do;
Change your wcf project to wcf service library, make sure this project have the following config...
under debug:
Start action: Start project
Start options: command line arguments: /client:"WcfTestClient.exe"
under wcf options:
Check "start wcf service host when debugging another project in the same solution"
Under solution -> startup projects, make sure you have a single startup project.
This way you can debug your other projects in the solution, visual studio will ensure that your wcf service is started.
You can host your service in local IIS automatically by configuring project settings from Visual Studio. Then whenever you compile your WCF service, IIS hosted instance will be updated and ready to serve automatically.
Right click your WCF Service project -> Properties -> Web -> Use Local IIS Web Server and click Create Virtual Directory. Before, do not forget to turn on your IIS services feature on your machine if not.
Additionally, why dont you test your concrete service implementation only just by referencing it, you dont need to run a service and connect it to test your business.
Usually you put WCF service logic in some other project, let say "WCF Login Library" and you test functions on that. You don't need wcf service to test those method calls. You can then use only one method to test the connection with WCF (that's usually done manually by me).
Isn't that simpler approach?

How to get code coverage on test that needs service to be hosted?

I have a Visual Studio 2012 solution with a test project that has tests that call a class library project which calls a WCF service project (this is a simplified summary of the status, the actual solution is more complex).
From the Test Explorer if I click on 'Debug Selected Tests' the tests then pass but if I 'Run Selected Tests' then they fail as the service is not being hosted.
I want to check my code coverage but the code coverage 'runs' the tests which means the service does not get hosted and thus the tests fail and my code coverage is 0%.
How can I configure the testing process so that the service is hosted when I 'run' tests as well as when I 'debug' tests?
Addendum
After consideration I think that mocking the service will not accomplish the goal. The various components have already been tested in isolation using mocking but now need to be tested to see how they operate together.
They're not unittests as they have an external dependency (another example is a database call) - you are relying on the webservice being up for the test to pass. This isnt a guarantee.
I'd say use a mocking framework here - I use Rhino Mocks but there are plenty out there. That way you can bypass the reliance on the webservice by mocking it.
In addition - I'd look at refactoring the class library you're calling if you can, seperate out the calls etc so you can test parts in isolation.

IISExpress: Unable to automatically step into the server. the remote procedure cannot be debugged

I'm hosing a webservice in IISExpress and can browse to the endpoint using my browser so I know its alive. I'm writing a test application in WinForms and want to call a method on the service.
When I try and step into the call, I get this error.
IISExpress: Unable to automatically step into the server. the remote procedure cannot be debugged.
The webservice is set to be in debug mode, I'm wondering is there some setting which is preventing IISExpress from allowing debugging?
Update:
I'm wondering if this is because the Test app doesn't have any .pdb files loaded into memory as these are stored elsewhere within the webservice folder, NOT the test app bin folder.
I've tried to specify the folder of the bin folder where the app_code.pdb is located for the webservices, but still not luck.
So how is this done? I'm stumped.
Update 2:
Still haven't been able to get this to work after a week of looking into it.
So here are my basic requirements: I need a 1) WinForms app to 2) be able to make calls to different webservices and 3) be able to step into each service call. The services are hosted as sub-applications of a root website using IISExpress. The WinForm test app is making the web service calls, but IISExpress is started via a Nant task. The webservices are old web site type projects, so they have app_code.dll files. I've tried setting symbol paths up to point to the built pdb file, but I then get an error along the lines of:
"A matching symbol file was not found in this folder".
I never thought debugging webservices would be this difficult?!?
1) Reference the DLL of your webservice in your test project.
2) IISExpress is a completely different application then your winforms application, just having the DLLs referenced still won't automatically allow you to step into your web service code. You need to attach to the IISExpress.exe process. When you start your winforms project, Hit Debug...Attach to Process and choose IISExpress.exe. Now you should be able to actually step into your web service code.
I realize this is a late response but it's something I've run into (especially after having stepped away from a WCF project for long periods of time).
Make sure you have your breakpoint set in your "server-side" service class code...and not in your "client-side" calling code. This is so the server side service has the necessary debug info (your breakpoint) compiled into the .pdb file on the server.