I am working with unit testing for an MVC application using Visual Studio's test project.
I need to call the Application_start() method from my TestInitialise unit test method, because Application_Start() initializes some global values that are required for my app.
I have also copied necessary info from web.config to app.config of my test app.
How can I do this?
In order to call the Application_Start method you will first need an instance of your application which is very difficult to have in a unit test. So externalize everything that you have in this method into a separate static method which you could invoke in your unit test.
Related
I am looking how to mock autofac Resolve method. I am using NUnit and NSubstitute for unit tests.
I have a Business method which calls Repository method, and inside business method, i am using
_container.Resolve< IRepository>().GetData()==> here _container is IComponentContext.
Instead of calling real db, i want to mock repository method, but i am not able to do that, every time this line tries to call real db.
In my unit tests, I am trying to mock like this....
private IRepository _Repository;
_repository = Substitute.For< IRepository>();
_repository.GetData().Returns("abc");
Don't try to mock dependency injection. Instead of mocking DI, register your mock with Autofac.
This may mean that you need to modify your code so it allows you to add registrations to the container in a test scenario. In ASP.NET Core they have a notion of ConfigureTestServices for example that allows you to put registrations in a method that only get run in a test scenario.
Another way to do it is to use a custom Startup class where all the methods are virtual so you can override ConfigureServices or ConfigureContainer to register test overrides.
Unfortunately, exactly how you do this is so very application specific that there's not "one way to do it" or a "best practice." You're going to have to do some searching and looking at your own app to figure it out.
However, the takeaway here is: Don't try to mock DI. Use real DI with mocks inside the container.
I am running some tests in django but they depend on a response from an outside service. For instance,
I might create a customer and wish to acknowledge it has been created in the outside service.
Once I am done with testing I want to remove any test customers from the outside service.
Ideally, there would be a method similar to setUp() that runs after all tests have completed.
Does anything like this exist?
You can make use of either unittest.TestCase.tearDown or unittest.TestCase.tearDownClass
tearDown(...) is the method gets called immediately after the test method has been called and the result recorded.
but, the tearDownClass(...) is gets called after tests in an individual class have run. That is, once per test class.
IMO, using tearDownClass(...) method is more appropriate since you may not need to check/acknoledge the external service after search test cases of the same class
So Django's testing framework uses a Python standard library module, unittest. This is where the setUp() method comes from.
This library contains another method tearDown() that is called immediately after the tests are run. More info can be found here
I'm using ShrinkWrap to start Jetty server in my integration tests.
Problem:
When I start my test jetty-server and than make mockup of my controller - mockup doesn't work!
I suggest that the reason is different classloaders: JMockit - AppClassLoader, Jetty - WebAppClassLoader.
Question:
How to make mocking works fine?
P.S.
I've googled that -javaagent:jmockit.jar option may help. But it doesn't. Is it necessary for maven project based on 1.7 jdk?
ADDITION:
I've written demo to illustrate my problem. You can find it by the reference.
About my demo:
Except of ten stokes of code, it is identical to those project.
I've only added JMockit and a single mock to illustrate the problem.
You should see JettyDeploymentIntegrationUnitTestCase.requestWebapp method: in those method we make mock which doesn't work.
You can check that Jetty & JMockit loads classes by siblings classloaders, so JMockit simply doesn't see Jetty's classes
URLClassLoader
|
|-Launcher$AppClassLoader
|-WebAppClassLoader
The JUnit test in the example project is attempting to mock the ForwardingServlet class. But, in this scenario with an embedded Jetty web server, there are actually two instances of this class, both loaded in the same JVM but through different classloaders.
The first instance of the class is loaded by the regular classloader, through which classes are loaded from the thread that starts the JUnit test runner (AppClassLoader). So, when ForwardingServlet appears in test code, it is the one defined in this classloader. This is the class given to JMockit to mock, which is exactly what happens.
But then, a copy of ForwardingServlet is loaded inside the deployed web app (from the ".class" file in the file system, so not affected by the mocking as applied by JMockit, which is in-memory only), using Jetty's WebAppClassLoader. This class is never seen by JMockit.
There are two possible solutions to this issue:
Somehow get the class object loaded by WebAppClassLoader and then mock it by calling the MockUp(Class) constructor.
Configure the Jetty server so that it does not use a custom classloader for the classes in the web app.
The second solution is the easiest, and can be done simply by adding the following call on the ContextHandler object created from the WebArchive object, before setting the handler into the Jetty Server object:
handler.setClassLoader(ClassLoader.getSystemClassLoader());
I tested this and it worked as expected, with the #Mock doGet(...) method getting executed instead of the real one in ForwardingServlet.
I'm working on open source plugin for grails named grails routing
I have hit the bug which simply defines like that: doWithSpring (which is called before doWithDynamicMethods) initialize and starts up the bean, which immediately run some custom code. This custom code could use dynamic methods which are not yet injected (coz doWithDynamicMethods is not yet called). I know how to fix it, however I'd like to create an integration test which proves my logic is right and hit the bug. Then I'll commit code changes which will pass this integration test. The only thing I need is a way to run some custom initialization of grailsApplication.routeClasses BEFORE doWithSpring is called. However, as I see integration tests are started after spring initialization is done. Is there some way to trick it from integration test? E.g. an annotation similar to #Before and #BeforeClass which runs BEFORE grails spring initilazation take place?
UPDATE 1
I tried to use conf/spring/resources.groovy which for plugins are used only on testing phase. However, I found that it's called after doWithSpring method.
Call order is as the following:
doWithSpring
resources.groovy
doWithDynamicMethods
| Running 1 integration test...
#BeforeClass
| Running 1 integration test... 1 of 1
I have a local class definition and implementation on a particular ABAP object for testing. I am implementing the setup and teardown methods as part of the test. Now, when I right-click on the class in transaction SE80, and click Unit Test, it runs as expected, except that it appears my setup method is being called twice, which results in failures because duplicate data is being created in the database. Has anyone seen anything like this before?
I was using SETUP and TEARDOWN fixtures where I should have been using CLASS_SETUP and CLASS_TEARDOWN fixtures.
The regular SETUP and TEARDOWN fixtures are called before EACH test method, whereas the CLASS_SETUP and CLASS_TEARDOWN fixtures are only called respectively once before running all the test methods in the class and once afterwards.
For more information, read the ABAP documentation about test classes.
The Methods SETUP and TEARDOWN are called every Time before/after an Testmethod is executed.
Maybe you have implemented two Test-Methods, so you got duplicated data.
With the class-methods class_setup and class_teardown you can define Test-Fixture's which are executed before/after every test of the class.
More Information on: SAP Help