Wednesday, 31 January 2024

Junit annotation

 https://howtodoinjava.com/mockito/mockito-annotations/

sprinboot transactional

 https://blog.stackademic.com/spring-boot-annotation-transactional-explained-with-practical-example-69c9b864dbd1

Unit test cases for private method

 

@Test
@DisplayName("player should get game trophy if he/she meet game trophy criteria")
void testCalculateGameTrophy() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
TrophyCalculationServiceImpl trophyCalculationService = new TrophyCalculationServiceImpl(trophyCalculationDao, oneHuddleMilestoneService);
Method calculateGameTrophyMethod = TrophyCalculationServiceImpl.class.getDeclaredMethod("hasGameTrophyAchievedByPlayer", PlayerReport.class, GameTrophy.class);
calculateGameTrophyMethod.setAccessible(true);
PlayerReport playerReport = PlayerReport.builder().playerId(1).attempt(10).build();
GameTrophy gameTrophy = GameTrophy.builder().gameTrophiesId(1).gameId(1).attempts(9).build();
assertTrue((boolean) calculateGameTrophyMethod.invoke(trophyCalculationService, playerReport, gameTrophy));
}

Monday, 29 January 2024

Observer design pattern example

 https://howtodoinjava.com/design-patterns/behavioral/observer-design-pattern/

 

public interface Subject 
{
    public void attach(Observer o);
    public void detach(Observer o);
    public void notifyUpdate(Message m);
} 
 
import java.util.ArrayList;
import java.util.List;

public class MessagePublisher implements Subject {
    
    private List<Observer> observers = new ArrayList<>();

    @Override
    public void attach(Observer o) {
        observers.add(o);
    }

    @Override
    public void detach(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyUpdate(Message m) {
        for(Observer o: observers) {
            o.update(m);
        }
    }
}
 
public interface Observer 
{
    public void update(Message m);
}
 
public class MessageSubscriberOne implements Observer 
{
    @Override
    public void update(Message m) {
        System.out.println("MessageSubscriberOne :: " + m.getMessageContent());
    }
}
public class MessageSubscriberTwo implements Observer 
{
    @Override
    public void update(Message m) {
        System.out.println("MessageSubscriberTwo :: " + m.getMessageContent());
    }
}
 
public class MessageSubscriberThree implements Observer 
{
    @Override
    public void update(Message m) {
        System.out.println("MessageSubscriberThree :: " + m.getMessageContent());
    }
}
 
public class Message 
{
    final String messageContent;
    
    public Message (String m) {
        this.messageContent = m;
    }

    public String getMessageContent() {
        return messageContent;
    }
}
 
public class Main 
{
    public static void main(String[] args) 
    {
        MessageSubscriberOne s1 = new MessageSubscriberOne();
        MessageSubscriberTwo s2 = new MessageSubscriberTwo();
        MessageSubscriberThree s3 = new MessageSubscriberThree();
        
        MessagePublisher p = new MessagePublisher();
        
        p.attach(s1);
        p.attach(s2);
        
        p.notifyUpdate(new Message("First Message"));   //s1 and s2 will receive the update
        
        p.detach(s1);
        p.attach(s3);
        
        p.notifyUpdate(new Message("Second Message")); //s2 and s3 will receive the update
    }
}
 

 

Wednesday, 10 January 2024

equals vs == comparison

 // import static org.junit.jupiter.api.Assertions.assertEquals;

// import org.junit.jupiter.api.Test;

class Main {

  public static void main(String[] args) {
    CustomClass obj1 = new CustomClass(10);
    CustomClass obj2 = null;

    // Using equals() for custom class objects
    boolean equalsResult = obj1.equals(obj2);
    System.out.println("Using equals() for CustomClass: " + equalsResult);

    // Using == for reference comparison
    boolean doubleEqualsResult = (obj1 == obj2);
    System.out.println("Using == for CustomClass: " + doubleEqualsResult);
  }
}

class CustomClass {
  private Integer value;

  public CustomClass(Integer value) {
    this.value = value;
  }

  @Override
  public boolean equals(Object obj) {
    System.out.println("-----" + this.getClass());
    //System.out.println("------" + obj.getClass());
    if (this == obj)
      return true;
    if (obj == null || getClass() != obj.getClass())
      return false;
    CustomClass that = (CustomClass) obj;
    return value == that.value;
  }
}

Tuesday, 9 January 2024

springboot interview question

 https://rathod-ajay.medium.com/top-60-spring-framework-interview-questions-for-java-developers-2024-contain-all-the-questions-from-f15621f77d2a