Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In Java I have the abstract class Place, with two concrete subclasses Area and Level; a Level must have a parent Area. But in trying to make unit tests for Level, I don't want to have create a full-fledged instance of Area. I see two ways of dealing with this:
1) Create an interface IPlace, extended by interface IArea, which Place and Area implement. Then create a MockArea class which implements IArea, and pass that to Level when testing it.
2) Use a mocking framework which will automatically create mock objects for me.
Which way is better? Or is there a third way to do it?
You're not giving us the reason why you don't want to create a full-fledged Area, but lets assume it does something difficult to test, like connect to a DB or read a file or something. Those are dependencies that it has. Dependency Injection is the answer.
For example, let's say Area does this in its constructor:
public Area() {
//get db connection
//do something with db connection
}
Now when you create a Level, it'll connect to a DB. Here's how you'd rewrite the constructor to use Dependency Injection:
public Area(Connection con) {
//do something with db connection
}
Now, when you create a Level, you can give it a fake Connection and are able to test your Level.
Now you can use a mocking framework to make a fake Connection. I recommend Mockito.
As you've written it, I'd suggest using a mocking framework.
Dependency Injection is great. Using it lets your classes state in an obvious way what types of things they need to interact with. If done properly, the need for mocked objects is often unavoidable. Get used to working with a mocking framework. I like Mockito personally.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am pretty new to Javafx so saying already sorry if this may be an obvious question. I am using a Main class to do some housekeeping and pre-load the content from two json files so that everytime the app needs to get data, it does not need to read the file. Seems to me being the most efficient way. Then whenever the business logic needs the data it can call the static list and it's right there. But it seems completely against the entire OOP paradigm and it is already giving me a headache when trying to mock this for testing. So what is the common way of doing it out there?
1) How to pre-load a database at the very start of the program without it being hard later to access?
2) How to stay OOP and make that pre-load object persistent over the entire run of the app?
The data in the json are strings that need to be compared against incoming data over TCP in real time. Thus the need to be efficient. Sorry if this has been answered already but I can't seem to find a fitting strategy or patters that will solve this.
public static List<DrefData> database;
public static List<LayoutData> layout;
public static void main(String[] args) {
Initialize.logReportLevel("trace");
// load databases
DrefDataIO io = new DrefDataIO();
LayoutDataIO lio = new LayoutDataIO();
database = io.loadDatabase();
layout = lio.loadLayoutDatabase();
// open main window
MainWindow.main(args);
}
}
What you are trying to implement is a simple cache, an in memory representation of your database which is fast to access:
You need to implement the Singleton Pattern. This will ensure you only have an instance of your database object accessible to everyone in the same context. This will make it way easier to access since you will have a single object dedicated for that instead of it being in the Main class.
Although this solves both of your problems, it violates the single responsibility principle and will still be tricky to mock since a singleton must have the constructor private so it is only called by a named constructor once (not a Java expert but look at PowerMock i think it allows it).
Also, do you change your json in runtime? or is it a read-only
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 months ago.
Improve this question
I have a Product class, and Builder classes, one of them can build that product subclass getting data from database, another builder can build that product getting data from other sources.
So far I have an interface:
public interface ProductDao {
Product buildProduct(RetrieveBy by, String s);
}
an enum with building options:
public enum RetrieveBy {
NAME, TYPE, BRAND
}
I do not know what is the best way to name class that is going to implement the interface and will build the product getting data from database, and other classes that can build that product getting data from other sources(JSON, XMLs, or property files).
Someone suggested me to create just a single class and name it ProductBuilder, but, IMO this violates single responsibility principal.
Thing is: there are no hard rules here, just conventions, and most importantly: that "precedent" that exists in your company/team/project.
In other words: do what everybody else does around you.
My personal style:
I would call the interface ProductBuiler ... DAO means "data access object", and that interface has nothing to do with that (directly)
I would then name the class ProductBuilderImpl for example. Or if you have one implementation per "source", then simply JsonProductBuilder or maybe ProductBuilderForJson.
But as said, the real answer is: there are no universal laws that dictate names. You should use what "feels" good for you and your team.
I dont know if I have sure about your doubt but, a DAO is a Data Access Object basically it isolates the application/business layer from the persistence layer.
If you want to create a builder interface, maybe interface something like ProductBuilder.java, and the implementation ProductBuilderImp.java.
Check this two links
DAO Pattern
Naming Convention
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
All of the examples I've read about DAO classes only show DAO classes with very general methods, for example, insertNewCar(), deleteCar(), updateCar(), getCars(), getCarByID(). But can I put more specific methods into a DAO class?
In my case, I need to get all doctors who are having a shift today, and to know that, I have to get all doctors' scedules which have the date equal to today. Right now, I'm getting those doctors by a single method in doctorDAO class called getDoctorsWorkingToday(ArrayList scedulesToday). In this method, I first get all ids of doctors from the arraylist and attact them into a complete sql query. And the rest of the method is just like a normal "get" method: I use the query to get all doctors I want, put them into an arraylist and return it.
It works fine, but is that solution acceptable? Or must I only use general methods in DAO class like getAllSchedules() and getAllDoctors(), and do all the filter stuff in other classes?
As per my understanding , I usually put all db specific code (queries) in DAO's and transaction handling and business logic in service layer. This allows for service methods to invoke methods across multiple dao's and keep it all within same transaction. In my opinion, this is allows for better code reuse across dao's.
In your case,
Use 'get' method business logic in Service layer and Query related stuff in dao.
It's upto you, how you are implementing the service and dao. Just for the flow and easy understanding,we are following the standards.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to Mockito and I have started to learn it. But I have some questions. Why do we need to use Mockito? As far as I know it is used to Mock(Create dummy object) and write the test cases before having actual running code. But, what if I want to test my already implemented code to check whether they are functioning properly or not. How would I test it using Mockito?
For instance, I have CRUD methods and I would like to test whether Create is functioning properly by actually inserting data in database using my Create method, similarly for others. Can we attain it using Mockito. If not, then do I need to write different testcases for them without using Mockito?
The Mock is used to each class or service you are using. The class under test should not be Mocked. Lets assume you are connecting to a remote service which is being built by one of your engineering team, and you are not familiar with its internal functionality but you know what requests and response it returns.
In that case, you can create a Mock of that Object, and defines it with set of responses returns in different situations. Each situation should get its own different test and for each response you should check separately the reaction of the code (you are working on).
Another great example is creating a limitation checks. Lets think of exception that might be thrown in some situations.
You can Mock the object that will throw the Exception which is simple(~2-3 line of test code if you are using Mock) and you can check how the code you have written reacts to that Exception. Without the Mock the throwing of an exception might be really complicated thing and not so easy to use if you are not familiar with the small details. And of course the Mock enables you to be on focus of the main functionality you are checking cause it make the checking time very very small. And that is a bless when time to market is a critical thing.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
The code base that I work with has a common set of data-access methods in an abstract base class which is extended by many different entity-specific DAO classes.
As there are currently no tests covering any of this logic, I've started adding tests for a specific entity DAO which covers the common data-access methods as well as the custom methods in that DAO.
Writing tests to cover the exact same common methods for each other entity DAO seems like a waste of time and a maintenance nightmare, so I only have the one so far. On the other hand, having those tests for all entity DAOs may help us catch differences between our schema and entity mappings.
Is there any real benefit to having integration tests of common methods for every entity DAO?
It is ok to write test for all these methods, however here are somethings you should to take into consideration
Time: It will take time to implement all these test. If for some reason you need to allocate development resources else where you should really take this into consideration.
Maintenance: You need to maintain all these test, if you decide to write them.
Redundancy: These method will behave similar in most cases and can be a waste of time to implement in the first place.