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 7 days ago.
Improve this question
I have a method that uses a static method defined in a Utils.java. The questions is, is it ok to test a method that uses a static method and how should I do that? I saw that some people uses PowerMockito but from what I understood, that is a code smell.
It is definately okay to test static method, but the thing is
You have to mock the dependent functions,
#Test
void testStaticMethod() {
MockedStatic<YOURSTATIC_CLASS> utilsMocked = Mockito.mockStatic(YOURSTATIC_CLASS.class)
utilsMocked.when(YOURSTATIC_CLASS::name).thenReturn("yourwantedassertable");
assertThat(YOURSTATIC_CLASS.name()).isEqualTo("yourwantedassertable");
}
Yes, it is ok. I advise you to watch this :
https://www.baeldung.com/mockito-mock-static-methods
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I have a class with a method which somtimes uses notifyAll().
class A{
public void method(*some args*) //sometime i call for notifyAll()
}
I want to build a test to see if class A has called for notifyAll()
#Test
public void test(){
A.method
boolean isNotified
}
how can I do it?
Couple of ways
Verify it has been called by checking the effects of potential call - eg some object did change its state as a result and it is verifiable
Spy on instance that is supposed to have method called - you can then check if the method have been called directly
Use observable pattern and add 'observe' behavior to the test case.
probably much more.
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 years ago.
Improve this question
I'm working on a project where we have a method like this:
processEvent(event, context) {
// var result = some processing here
context.setResult(result);
// context's class have nothing to do with processEvent's class
return context;
}
The class SomeClass containing processEvent method has no inheritance relation with context's class (so it's not about a builder pattern here). For me, the context returning is silly because the direct caller would already have it. Are any other more plausible arguments in favor of or against this approach?
It's not uncommon to have a method return one of its parameters' values if it doesn't have any other return value. It enables code like this:
Context context = processEvent(event, new Context(/*...*/));
Whether that's good or bad style is, er, a matter of style. :-) It's not all that common, but I wouldn't say it's uncommon, either.
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 1 year ago.
Improve this question
Where is preferred place to store functions for improve readibility of streams?
For example:
private BinaryOperator<Cash> sumPayments() {
return (payment1, payment2) -> payment1.sum(payment2);
}
I'm not sure if it should be in service class where it's used, but maybe it will be in many places so whats about some extra static Utils? or in entity?
Where possible avoid unnecessary helper methods.
In this particular example, the helper method can easily be replaced with a method reference, which is (arguably more) readable:
Cash total = subtotals.stream().reduce(Cash.ZERO, Cash::sum);
In other cases, where you actually do need a helper: think about where you would search for such a helper method in a year, when you have to adapt your now-barely-familiar code to new requirements.
Is it more entity related? Put it in the entity class
Is it more business logic related? Put it into the business logic
Is it generally useful? Put it into a utility class
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 7 years ago.
Improve this question
I have common methods written in different classes, making code duplicate and now to avoid this duplicacy, I am thinking of 2 approaches:
Make static methods in some util class and call them, or
Make a super class and write all these methods in super class and extend each class with this super class.
Definitely, with approach 2, I will loose the ability to extend my class further. So I am thinking to go with approach 1.
Can you please help me in identifying which approach is good and also in suggesting better approach, if you have any?
Creating Utility packages and/or class is a common solution to this problem. Apache Commons is a prime example. I would favour approach 1
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 7 years ago.
Improve this question
Given the following method of a class which doesn't have type T defined:
public <T> T get(java.lang.String name) { /* compiled code */ }
Would it be possible to invoke this, and how?
Yes. Just call it with an explicit type argument:
foo.<Integer>get("something")
I'm not terribly fond of how type arguments are expressed in Java, but they're perfectly doable. See the Java Generics Tutorial for another example.