How do I test if i got notified [closed] - java

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.

Related

Testing static methods using Mockito and JUnit [closed]

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

intellij println declaration [closed]

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 trying to use ''sout'' keyword in intellij for calling system.out.println() quickly but it seems like doesnt work.
Looks like, I have some problems in settings sections.
Does anyone know the way the quick way problem
It Looks like this:
I solved the problem it was all about defining println() inside a method.
I solved it. The solution is that declaring the println() method within class or
inside another method as well.
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
int fun()
{
return 20;
}
}
Yeah just use it inside in any method, like this:
According to Intellij support question
Make sure you are inside the method where this completion makes sense, not just inside the class.

How can we decide whether or not to provide default empty implementation (Defender Method) for an interface method? [closed]

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
Currently, I have an interface which looks like this
public interface SortInfoDialogListener {
void onSortInfoSelected(SortInfo sortInfo);
void onSortInfoDialogDismiss(DialogInterface dialog);
}
Since most of the implementors are not interested in onSortInfoDialogDismiss, I was wondering, should I make it as an empty defender method?
public interface SortInfoDialogListener {
void onSortInfoSelected(SortInfo sortInfo);
default void onSortInfoDialogDismiss(DialogInterface dialog) {
}
}
I was wondering, what else factors I should consider, before making an interface function having an empty defender method?
I try to find answers in Effective Java 3 Item 20 and Item 21. But, still can't derive a concrete conclusion yet.
SOLID principles states to segregate interfaces as per the requirement. Currently i could see onSortInfoDialogDismiss method is not required for many implementors.
Try to extract onSortInfoDialogDismiss() to another interface, so it could be implemented on demand.
keep current interface with onSortInfoSelected() alone, as below.
public interface SortInfoDialogListener {
void onSortInfoSelected(SortInfo sortInfo);
}
Hope this helps.

Where to store java stream helper methods [closed]

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

Synchronization on Java Method [closed]

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 9 years ago.
Improve this question
I have a public class called ProcessOrder. Within this class I have a private method called Login(), which logs into another company's system. I want to be able to initiated as many ProcessOrder objects as I want, but the Login() process to be synchronized, which means all the ProcessOrder objects need to wait inline to process Login().
What's the best practice to design my code?
Thanks,
You could use a synchronized block, specifying the object on which to synchronize on (here, the ProcessOrder class):
public class ProcessOrder {
private void login() {
synchronized(ProcessOrder.class) {
// one at a time, please
}
}
}

Categories