Mock the companion object correctly

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2021-10-07 19:08:47 +02:00
parent c854d8c3ae
commit 707841ec29
No known key found for this signature in database
GPG key ID: FECE3A7222C52A4E

View file

@ -36,6 +36,7 @@ import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
@ -66,9 +67,11 @@ public class DoNotDisturbUtilsTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
mockStatic(NextcloudTalkApplication.class); mockStatic(NextcloudTalkApplication.Companion.class);
PowerMockito.when(NextcloudTalkApplication.getSharedApplication()).thenReturn(application); NextcloudTalkApplication.Companion companionMock = PowerMockito.mock(NextcloudTalkApplication.Companion.class);
Whitebox.setInternalState(NextcloudTalkApplication.class,"Companion",companionMock);
PowerMockito.when(NextcloudTalkApplication.Companion.getSharedApplication()).thenReturn(application);
when(application.getApplicationContext()).thenReturn(context); when(application.getApplicationContext()).thenReturn(context);
when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager); when(context.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager);
when(context.getSystemService(Context.AUDIO_SERVICE)).thenReturn(audioManager); when(context.getSystemService(Context.AUDIO_SERVICE)).thenReturn(audioManager);