In our SmartGWT web application, we pass our domain objects from the server to the client and back (via GWT serialization). To show/edit the data at client side in a DynamicForm or a GridList, we have to convert it into a Record (or ListGridRecord) and after editing back into our domain objects.
I would like to write a unit test for this conversion method, but a straightforward attempt in JUnit fails, since the record's getAttribute and setAttribute methods are implemented by JSOHelper.getAttribute/JSOHelper.setAttribute, which are static methods declared as native and implemented by JSNI in JavaScript, thus only usable on the client side, when compiled to JavaScript.
We get an UnsatisfiedLinkError when using these methods from JUnit, as the native methods are not implemented there.
Any ideas how I could test these classes?
These critical methods could be easily implemented by a simple HashMap (or maybe a LinkedHashMap, if the attribute order is important) - actually a JavaScript object is about that, if only looking at the data part, not the methods. Thus I just think about providing an alternative implementation of some selected SmartGWT classes (mainly JSOHelper) with Java implementations instead of the JavaScript ones.
But am I really the first one who has this problem? Am I simply too stupid to find the existing solution?
If you have used a MVP or MVC pattern in your code, just mock the view code with something like mockito and test all the rest of the application. To test the view code you will need to use something like Selinium I don't think gwtTestCase would work with smartGWT since it is just a gwt wrapper around js code.
Related
I implemented an api controller that will be used by part of another system, rather than users directly. However, I want to provide a unit test for it. I started looking at MOQ and then I realized my particular case is a little more complex. The code works, but as I said, Im trying to make a test for it, without (ideally) writing any data to the Db.
The structure of the classes look like this
api controller
|__MyCustomClass (injected via startup along with configuration)
|__UtilityClass (method: ImportSomeDataFromaFolder)
|__MydataRepositoryClass
|__CustomDerivedDbContext
(override savechanges etc so as to capture EF errors)
Note:
- The return value of the api method is a complex JSON object.
- Id like to have a test that avoids actually writing to the Db
- I am creating a custom DbContext (CustomDerivedContext) and overriding savechanges, so as to capture EF entities that change via in a list, eg. List<EntityEntry>
- The method ImportSomeDataFromaFolder, after parsing the data into POCO objects and sending them to the Repository for persisting to the Db, then moves the file to a different folder. When testing, i'd rather this didnt happen, but rather just load a file to parse.
There are 3 primary things to test:
(1) Does the data in the file get loaded into POCO objects
(2) Do the POCO objects get translated correctly to EF model entities
(3) Does the api return a JSON object that contains the expected results
Or, am I making things more complicated than what should be done for a unit test. I want to write a test against the api controller, but its the CustomDerivedDbContext that seems I want to use a fake here, since I could then remove the step that actually calls the underlying DbContext savechanges.
Sounds like you have tight coupling to implementation concerns that make unit testing your subject in isolation difficult.
That should be seen as a code smell and an indication that the current design choice need to be reviewed and refactored where possible.
If unit testing the API controller then ideally all you should need is a mock of the explicitly injected abstractions.
The API controller need not know anything about the dependencies of its dependencies if proper separation of concerns are followed.
I am using LinqToSQL in my project.As pert of my unit testing, I am trying to mock datacontext which is part of another assembly which is referenced in the ASP.NET WEB API project.
Based on the following URL:
http://weblogs.asp.net/rashid/100-unit-testable-linq-to-sql-repository
I tried to create a partial class for the datacontext but somehow it is not working.
Can anyone help me to know how to mock the datacontext class which is part of another referenced assembly.
Right from what I can gather from skim reading that article is that it's mostly call assertion and there are no tests around queryable results. IMO these tests are pretty meaningless and more over when you start putting in meaningful tests it wont work because Linq-to-sql is not mockable, DbContext is not mockable, can't use pocos, etc. This article was written in 2009 and Linq-to-sql is not a current product.
How big is the project? If at all possible move to EF6 and utilise either IDbSet to create a FakeDbSet or Mock the DbSets themselves and alot of the methods are virtual now. Have a look at this Q&A:
How are people unit testing with Entity Framework 6, should you bother?
If you search for Unit testing EF6 you find what your're trying to achieve much easier.
If you really can't change the Ling2Sql then use a façade pattern and compose you DbContext inside this. The façade would expose the DbSet are IQueryable and wrap up any method calls. This means though that you would have to create methods and properties that are virtual and then you can mock the façade. Alternatively you could also create an interface for the facade which you can also use for mocking. This would make any complex queries you write completely testable but you are creating code that calls other code. You would also have to watch the lifestyle of the façade as this is newing up the DbContext. This pattern is used to get round testing where AppSettings are involved, it's the same thing really. You just creating something that is mockable as a wrapper for something that isn't.
I'm working on a project that has client and server side. And I'm writing a "pre-check-in" tool that will validate a lot of our communication between client and server.
I already have unit tests on both sides, now I really want to test the integration between both.
Like a real client connection to the server and vice-versa.
I really want to go unit testing on this, but I'm having a really hard time figuring out how I can initialize the MMVMCross framework and my view model classes.
In another thread I've asked for help on my "console app" that runs the tests but it is also really hard to initialize the framework and it makes me loose the coolness of unit testing with Visual Studio and reSharper.
My view models use SQLite and HttpClient with async/await.
For instance: I can't find a way to instantiate a view model that would need this interfaces:
IChatService, IMvxMessenger, IDataService, ISettingsService
Some from the framework some from my own code.
I known and I'm trying to register my ones, on a TestFixtureSetUp, but off course this fails, as the MVVM base subsystem (ioc?) is not setup yet.
Some above services, like IDataService for instance, also needs ISQLiteConnectionFactory, IMvxMessenger, ISettingsService.
I know unit testing is supposed to be fast, but my idea is to put all this tests in a new Category, that I would run only before my check-ins and my buddy, server developer, would run before his check-ins.
What would be the best approach here?
Any hint, suggestions, things to investigate/study would help at this point, as I'm practically stuck on this one.
Sergio
For instance: I can't find a way to instantiate a view model that would need this interfaces:
IChatService, IMvxMessenger, IDataService, ISettingsService
Generally this would be done using Mock implementations and then calling the ViewModel constructor directly with those Mocks.
I'm having a really hard time figuring out how I can initialize the MMVMCross framework
In order to initialise the IoC part of the framework you can use the MvxIoCSupportingTest helper class -https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Test/Cirrious.MvvmCross.Test.Core/MvxIoCSupportingTest.cs
If you then need additional parts of the framework, then generally you should mock these in some way. For example, see how navigation is mocked in these two articles:
http://blog.fire-development.com/2013/06/29/mvvmcross-enable-unit-testing/
http://slodge.blogspot.co.uk/2013/06/n29-testing-n1-days-of-mvvmcross.html
If it helps, an example of this type of test is https://github.com/slodge/MvvmCross-Tutorials/blob/master/Sample%20-%20TwitterSearch/TwitterSearch.Test/TwitterViewModelTest.cs#L21
If this answer doesn't have sufficient information, please provide a bit more example code about the tests you are trying to write.
I'm working on a project which I'm really not sure how to unit test. It's an unobtrusive tag based framework for wiring up events between models, views and delegates in a GUI system.
Basically you have one large json file which is used describes all of the events, event handlers and bindings. The user creates their models, views and delegates all of which have no knowledge of the framework. The JSON file is passed to an init() methods, then the framework creates all of the instances needed and takes care of all the bindings, listeners etc.
The problems I have are two fold:
1) There is basically only a single public method in the framework, everything else is communicated through the mark-up in the JSON file. Therefore I have a very small testing surface for what is a large and complicated application.
2) One of the big roles of the application is to instantiate class's if they haven't been instantiated previously and cached. This means that I need real classes in my test code, simple mocks aren't going to cut it.
At the moment I'm considering a couple if solutions. The first is start testing the private methods. The second is to just stub the constructors.
Any one else have any ideas?
1) There is basically only a single
public method in the framework,
everything else is communicated
through the mark-up in the JSON file.
Therefore I have a very small testing
surface for what is a large and
complicated application.
How is that possible? is this entire complicated framework stored in one class? if there are several classes involved, how do they share information without public methods?
A constructor is a public method too, by the way.
Are you just passing around the JSON object? that would couple your framework to the information source too tightly. You should have one class parsing the JSON, and the rest communicating without knowledge of the data source (via testable public methods).
list the features (scenarios, use-cases, whatever you want to call them) of the system, and establish JSON data/frameworks for each feature. These are your unit tests.
I am writing a repository. Fetching objects is done through a DAO. Creating and updating objects is done through a Request object, which is given to a RequestHandler object (a la Command pattern). I didn't write the DAO, Request, or RequestHandler, so I can't modify them.
I'm trying to write a test for this repository. I have mocked out both the DAO and RequestHandler. My goal is to have the mocked RequestHandler simply add the new or updated object to the mocked DAO. This will create the illusion that I'm talking to the DB. This way, I don't have to mock the repository for all the classes that call this repository.
The problem is that the Request object is this gob of string blobs and various alphanumeric codes. I'm pretty sure XML is involved too. It's sort of a mess. Another developer is writing the code to create the Request object based on the objects being stored. And since RequestHandler takes in Requests and not the object I'm storing, it can't update the mocked DAO.
So the question is: do I mock the Request too, or should I wait until the other guy, who is kind of slow, to finish his code before I write the test? Or screw it and mock out the entire repository when testing the classes that call the repository?
BTW, I say "mock" not in the NMock sense, but rather like faking the DB with an in-memory collection.
To test the repository I would suggest that you use test doubles for all of the lower layer objects.
To test the classes that depend on the repository I would suggest that you use test doubles for the repository.
In both cases I mean test doubles created by some mocking library (fakes where that works for the test, stubs where you need to return something to the object under test and mocks if you really have to).
If you are creating an implementation of the DAO using in-memory collections to functionally replace the database in a demo or test system that is different to unit testing the upper layers. I have done something similar so that I can give prototypes to people and concentrate on business objects not the physical model. That isn't for unit testing though.
You may of may not be creating a web application, but you can have a look at the NerdDinner application which uses Repository. It is a free PDF that explains how to create an application using ASP.NET MVC and can be found here: Professional ASP.NET MVC 2.0