N
Observe the current class option NewsDataService - It's not the most trivial task to solve, to try to explain why.In the course of writing this class, you decided to incinerate the entire OBD logic inside the code, reaching a very low level, manually designed. SQL requests. And now your class serves highly specialized objectivesince it can only be created and respond to requests for a species Give me the essence. id♪ and "preserve such a thing."♪To improve this aspect of the code (couplingI'd recommend you read it. http://en.wikipedia.org/wiki/Inversion_of_control http://en.wikipedia.org/wiki/Dependency_injection and http://ru.wikipedia.org/wiki/ORM I can offer three different approaches to testing the current implementation option. NewsDataServicewhich can reasonably be combined in certain proportionsUse it. sandbox-based The approach is to take a eased database, fill it with real values, and write free-mode tests. In fact, if you were writing these tests before writing the code. (following methodologies) TDD), you'd have opened up a lot of minus and inconvenience the use of a written class. Special use for such purposes http://www.dbunit.org/ to facilitate the development of a sandbox database and writing Setup / Teardown methods.Follow the principles IoCInitiate yours. DatabaseConnector elsewhere. NewsDataService Throw it through the designer. That, first of all, takes off. NewsDataService The responsibility for initializing the connection, and secondly, facilitates testing. That's where it is. DatabaseConnector Can be replaced mock- an object that emulates part of the functionality, e.g. any request SELECT I'm going to return the predetermined news pack.This isn't the best approach, because mock- The target should be perfectly simple. (which will require tests)and this fact is very tight in the test. I mean, bluntly speaking, you can't, and you don't have to pass your request. mock'e, so the maximum you can do with it is to separate requests SELECT / UPDATEby returning some sort of predetermined bullshit to them, and, for example, checking that databaseConnector.query I was summoned.The general rule is simple - if the code mock'a raises questions about whether he works properly. for all casesthat's bad. mock♪ Sometimes, naturally, there are situations that have no difficulty. mock'a Don't bother, but in that case, you need to write tests for this one. mockwhich is usually undesirable.Make the generator. SQL individual type SqlQueryProvider<?>♪ In this case, your current code will begin to change about the following:... databaseConnector.query(sqlQueryProvider.getQueryToRetrieveById(...));This approach already allows for separate testing of the generation of queries, and, knowing that requests for requests are generated correctly, steeply in real testing NewsDataService♪ Again for testing NewsDataService in such a case may be useful mock ♪ SqlQueryProvider♪Several references to: http://www.javaranch.com/journal/2003/12/UnitTestingDatabaseCode.html http://www.dallaway.com/acad/dbunit.html https://stackoverflow.com/questions/266370/how-do-i-unit-test-jdbc-code-in-java I always wanted to see the authors of the type councils. Take it. XYZMock♪ Oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh, oh Do it in practice and do it, get it. legacy type code NewsDataService.