As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Should I write unit-test for every public method? For example I have a CRUD service, so I have
create, read, update, delete methods. But when I write test to delete or update methods, I implicitly test read and create methods (for example to test the update method first I need create an entity(create), then retrieve it (read), then update it and then retrieve it again to ensure it changed).
I would write tests for get and create, yes:
It makes it easier to separate behaviour you're deliberately testing from behaviour you're incidentally testing
It makes it easier to test failure conditions and other scenarios that aren't covered by your other methods
It means if you change your implementation of delete / update to not use get / create, you're still testing get / create
Ideally, I'd attempt (without getting too hung up about it) to make the tests set and check the data at the "lower" level - e.g. performing direct in-memory database access. That way you're really testing "If I create an entity, the data in the database looks like {this}" (and likewise when fetching). Just being able to create and then get the same results again is fine in terms of black box testing, but I typically think of unit tests as more white box.
Should I write unit-test for every public method? Yes
Writing a test case for each public method is best thing. To test delete operation you should call create test first and then call delete test method. This way your delete test method will be independent and you can test all possible scenrio.
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I just started learning a functional language (Scala) and one of the claims/recommendations made is, "you should try to use react instead of recieve" method while doing multithreading. To make it clear, react doesn't return any value but recieve does. They have their own reasons to support this recommendation. As Scala works on the JVM. It is making me curious to think if using Callable is a more costly affair than using Runnable in Java?
Does anyone has any experience with the same or comments on this?
Runnable and Callback have the same "performance" as they are just Interfaces.
The two interfaces have slight API differences - a type compatible with the consuming API must be used; that is all.
This has nothing to do with Scala or react vs. recieve in Actors; the question boxes itself into the wrong corner.
Wellll, you're really mixing different concepts here.
The reason to use react instead of receive is that each actor with a receive requires its own thread. So you've got one thread per actor. react on the other hand is handled by a pool of threads that will run that message on that actor and then go on to the next actor and message. (This really only permits you to be reactive--you can't wait for a certain amount of time.)
On the other hand, the Runnable and Callable interfaces are just ways to package up code in Java depending on whether you just want it to do stuff (Runnable) or return a value (Callable). The interfaces themselves don't have any difference in performance, but in order to get a Callable return value back to you there is additional stuff that needs to happen, so if you could write it either way you'd possibly be better off using something that only requires a Runnable. (In practice, this means starting a thread instead of a future, probably.) But the implementation details matter so much that you can't really make any general recommendations on the basis of the interface alone. You need to know how the interface is actually being used in the actual class you're calling.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
We have a policy that all test classes extend a given test class. The base class sets the JUnit runner to use as well as checking other things about the test class, so not subclassing from it means that other things you're doing wrong aren't going to be checked. (The most serious of these is when there is a category indicating that a test is expensive to run, but because the runner isn't being used, the test will run even for the run-fast-tests build target.)
I figure this is going to involve parsing the Java code, but I can only seem to find parsers for class files. Is there a good parser out there somewhere for parsing Java source?
(This could tie into another question I was going to ask about enforcing use of certain Javadoc tags, since such a parser would surely also parse Javadoc.)
Alternatively is there a DRY way to set a JUnit runner without subclassing? The whole subclassing business is really, really inconvenient.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Let's say I'm extending JFileChooser and making an easy-to-use version, which I'm calling SimpleFileChooser.
It is structured such that it can either be DIALOG_TYPE_OPEN or DIALOG_TYPE_SAVE — hence, JFileChooser's showOpenDialog() and showSaveDialog() methods are superfluous. I replace them with a method called showDialog() which returns a boolean, but this is where I find myself in a dilemma:
Should I override the open/save methods and add #Deprecated tags to
them so that the API user knows they've been superseded? Would that
violate the annotation's original purpose?
Or would a notice in the documentation be enough? If so, where should
this notice be placed: in the class summary or above the overridden
methods? Should I even override the methods in the first place?
Thanks in advance.
I think you are actually building a facade, a simplified version of already existing API. Thus instead of inheritance you should use composition. Hide the original JFileChooser inside your new class and provide simpler API.
As a last resort you can provide public JFileChooser getRaw() method to access wrapped object if some other code needs it.
#Deprecated means you should not use that particular class or method anymore as it will be removed in the future. That annotation is designed for that.
So to answer shortly, if you dont want API users to use the method anymore you should use #Deprecated. Because else you will end up with users that still use methods/classes that you remove in future builds and their projects will be broken when they update.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am thinking about externalizing some conditions instead of implementing them in java so that I can easily change them later as needed.
For example, I need to check if certain keys exist in a given map and if the values of certain keys in a map equal to something.
I was thinking about using spring's expression language, but since we are already using velocity templates, I thought maybe it is a good candidate.
Any idea? Thanks.
You can easily use the #if/#else, #foreach and other condition functionality of velocity to do business logic as part of the velocity template rendering.
However I usually try to separate business logic from rendering in velocity for a number of reasons:
Complexity: The Velocity template can become hard to read, especially
if the target output itself requires complex resulting layout. If you
add additional business logic to the mix, it quickly becomes
impossible to read for anybody else (or for yourself after a few
months of not looking at it constantly)
Testability: It's harder to test Velocity templates, there's
better support for unit/integration testing of Java code
Functionality: Velocity is not a full programming language by
design, so you will miss some things sooner or later and a macro
simply is not a function, e.g. variables by default have global
scope, ... You are bound to run into some of these if you make
your templates big and complex.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
First, I understand JUnit is fundamentally a unit testing tool, but I hope I can achieve what I want with JUnit.
The tests I am writing with each test case acting more like a test step (at least conceptually). There are particular blocks of these steps that I would like to make common as they may fit into other work flows (yes, this is functional testing...).
Is what I seek possible?
If I understand you correctly you would like to introduce an interdependence between the unit tests. I think that this is most probably a bad idea as the unit test should be able to be run in any order and/or just one at a time. Introducing some implicit required order in your tests seems like the wrong path to take. If there is some process to follow then each test should be able to perform that process in isolation.
You can derive test classes from common vbase classes and annotate some methods with #Before / #After to be executed before and after each testcase ( this providing common setup / tear down functionality ). You can also reuse some basic methods for recurring tasks.
Though I find mocking ( my favorite tool is jmockit ) more effective than setup methods and common functionlaity. I need such common methods really seldom now.