Skip to content

Commit

Permalink
Merge pull request #17 from ozgen/task/fix-unit-tests
Browse files Browse the repository at this point in the history
Fix unit tests
  • Loading branch information
ozgen authored Dec 28, 2023
2 parents bfc8a03 + b9263dc commit f19ee9c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.ozgen.telegrambinancebot.service.TradingSignalService;
import com.ozgen.telegrambinancebot.utils.SyncUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -144,28 +145,33 @@ public void testProcessOpenSellOrders_withEmptyTradingSignalList() {
}

@Test
@Disabled
public void testProcessOpenSellOrders_withException() {
// Arrange
when(this.tradingSignalService.getTradingSignalsAfterDateAndIsProcessIn(any(), argThat(list -> list.contains(ProcessStatus.SELL))))
.thenReturn(List.of(this.tradingSignal));
when(this.botOrderService.getBuyOrders(argThat(list -> list.contains(this.tradingSignal))))
.thenReturn(List.of(this.buyOrder));
MockedStatic<SyncUtil> mockedSyncUtil = mockStatic(SyncUtil.class);
RuntimeException testException = new RuntimeException("Test Exception");
mockedSyncUtil.when(SyncUtil::pauseBetweenOperations).thenThrow(testException);

// Act
this.binanceOpenSellOrderManager.processOpenSellOrders();

// Assert
verify(this.tradingSignalService)
.getTradingSignalsAfterDateAndIsProcessIn(any(), argThat(list -> list.contains(ProcessStatus.SELL)));
verify(this.botOrderService)
.getBuyOrders(argThat(list -> list.contains(this.tradingSignal)));
ArgumentCaptor<NewSellOrderEvent> eventCaptor = ArgumentCaptor.forClass(NewSellOrderEvent.class);
verify(this.publisher)
.publishEvent(eventCaptor.capture());
NewSellOrderEvent newSellOrderEvent = eventCaptor.getValue();
assertEquals(this.buyOrder, newSellOrderEvent.getBuyOrder());
try {
when(this.tradingSignalService.getTradingSignalsAfterDateAndIsProcessIn(any(), argThat(list -> list.contains(ProcessStatus.SELL))))
.thenReturn(List.of(this.tradingSignal));
when(this.botOrderService.getBuyOrders(argThat(list -> list.contains(this.tradingSignal))))
.thenReturn(List.of(this.buyOrder));
RuntimeException testException = new RuntimeException("Test Exception");
mockedSyncUtil.when(SyncUtil::pauseBetweenOperations).thenThrow(testException);

// Act
this.binanceOpenSellOrderManager.processOpenSellOrders();

// Assert
verify(this.tradingSignalService)
.getTradingSignalsAfterDateAndIsProcessIn(any(), argThat(list -> list.contains(ProcessStatus.SELL)));
verify(this.botOrderService)
.getBuyOrders(argThat(list -> list.contains(this.tradingSignal)));
ArgumentCaptor<NewSellOrderEvent> eventCaptor = ArgumentCaptor.forClass(NewSellOrderEvent.class);
verify(this.publisher)
.publishEvent(eventCaptor.capture());
NewSellOrderEvent newSellOrderEvent = eventCaptor.getValue();
assertEquals(this.buyOrder, newSellOrderEvent.getBuyOrder());
} finally {
mockedSyncUtil.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import com.ozgen.telegrambinancebot.service.BotOrderService;
import com.ozgen.telegrambinancebot.service.FutureTradeService;
import com.ozgen.telegrambinancebot.service.TradingSignalService;
import com.ozgen.telegrambinancebot.utils.SyncUtil;
import com.ozgen.telegrambinancebot.utils.TestData;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.verification.VerificationModeFactory;
import org.springframework.context.ApplicationEventPublisher;

import java.util.List;
Expand All @@ -28,6 +31,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -78,6 +82,10 @@ public void testProcessFutureTrades_withInsufficientStatus() throws Exception {
.thenReturn(tradingSignals);
when(this.binanceApiManager.getTickerPrice24(tradingSignal.getSymbol()))
.thenReturn(tickerData);
MockedStatic<SyncUtil> mockedSyncUtil = mockStatic(SyncUtil.class);
mockedSyncUtil.when(SyncUtil::pauseBetweenOperations)
.thenAnswer(invocation -> null);

// Act
this.futureTradeManager.processFutureTrades(TradeStatus.INSUFFICIENT);

Expand All @@ -98,6 +106,7 @@ public void testProcessFutureTrades_withInsufficientStatus() throws Exception {
assertEquals(tradingSignal, newBuyOrderEvent.getTradingSignal());
verify(futureTradeService)
.deleteFutureTrades(futureTrades);
mockedSyncUtil.verify(SyncUtil::pauseBetweenOperations, VerificationModeFactory.times(2));
}

@Test
Expand Down
24 changes: 17 additions & 7 deletions src/test/java/com/ozgen/telegrambinancebot/utils/SyncUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
package com.ozgen.telegrambinancebot.utils;


import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;

public class SyncUtilTest {

@Test
@Disabled
public void testPauseBetweenOperations() {
long startTime = System.currentTimeMillis();
SyncUtil.pauseBetweenOperations();
long endTime = System.currentTimeMillis();

// Check that the time elapsed is approximately 5 seconds
long elapsedTime = endTime - startTime;
assertTrue(elapsedTime >= 4000, "Elapsed time should be at least 5 seconds");
MockedStatic<SyncUtil> mockedSyncUtil = mockStatic(SyncUtil.class);
try {
// Mock behavior
mockedSyncUtil.when(SyncUtil::pauseBetweenOperations).then(invocation -> null);

// Invoke the method that uses the static method
SyncUtil.pauseBetweenOperations();

// Assertions or verifications here
} finally {
mockedSyncUtil.close();
}
}


@Test
public void testPauseBetweenOperationsHandlesInterruption() throws Exception {
Thread testThread = new Thread(SyncUtil::pauseBetweenOperations);
Expand Down

0 comments on commit f19ee9c

Please sign in to comment.