Migrate to JUnit5
This commit is contained in:
@ -34,8 +34,13 @@
|
||||
<artifactId>twin</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -22,7 +22,7 @@
|
||||
*/
|
||||
package com.iluwatar.twin;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -22,8 +22,8 @@
|
||||
*/
|
||||
package com.iluwatar.twin;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
@ -33,9 +33,10 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -48,12 +49,12 @@ public class BallItemTest {
|
||||
|
||||
private InMemoryAppender appender;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
appender = new InMemoryAppender();
|
||||
}
|
||||
|
||||
@After
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
@ -22,10 +22,12 @@
|
||||
*/
|
||||
package com.iluwatar.twin;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.lang.Thread.UncaughtExceptionHandler;
|
||||
import static java.lang.Thread.sleep;
|
||||
import static java.time.Duration.ofMillis;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeout;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -44,68 +46,74 @@ public class BallThreadTest {
|
||||
/**
|
||||
* Verify if the {@link BallThread} can be resumed
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
@Test
|
||||
public void testSuspend() throws Exception {
|
||||
final BallThread ballThread = new BallThread();
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final BallThread ballThread = new BallThread();
|
||||
|
||||
final BallItem ballItem = mock(BallItem.class);
|
||||
ballThread.setTwin(ballItem);
|
||||
final BallItem ballItem = mock(BallItem.class);
|
||||
ballThread.setTwin(ballItem);
|
||||
|
||||
ballThread.start();
|
||||
ballThread.start();
|
||||
|
||||
verify(ballItem, timeout(2000).atLeastOnce()).draw();
|
||||
verify(ballItem, timeout(2000).atLeastOnce()).move();
|
||||
ballThread.suspendMe();
|
||||
verify(ballItem, timeout(2000).atLeastOnce()).draw();
|
||||
verify(ballItem, timeout(2000).atLeastOnce()).move();
|
||||
ballThread.suspendMe();
|
||||
|
||||
sleep(1000);
|
||||
sleep(1000);
|
||||
|
||||
ballThread.stopMe();
|
||||
ballThread.join();
|
||||
ballThread.stopMe();
|
||||
ballThread.join();
|
||||
|
||||
verifyNoMoreInteractions(ballItem);
|
||||
verifyNoMoreInteractions(ballItem);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the {@link BallThread} can be resumed
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
@Test
|
||||
public void testResume() throws Exception {
|
||||
final BallThread ballThread = new BallThread();
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final BallThread ballThread = new BallThread();
|
||||
|
||||
final BallItem ballItem = mock(BallItem.class);
|
||||
ballThread.setTwin(ballItem);
|
||||
final BallItem ballItem = mock(BallItem.class);
|
||||
ballThread.setTwin(ballItem);
|
||||
|
||||
ballThread.suspendMe();
|
||||
ballThread.start();
|
||||
ballThread.suspendMe();
|
||||
ballThread.start();
|
||||
|
||||
sleep(1000);
|
||||
sleep(1000);
|
||||
|
||||
verifyZeroInteractions(ballItem);
|
||||
verifyZeroInteractions(ballItem);
|
||||
|
||||
ballThread.resumeMe();
|
||||
verify(ballItem, timeout(2000).atLeastOnce()).draw();
|
||||
verify(ballItem, timeout(2000).atLeastOnce()).move();
|
||||
ballThread.resumeMe();
|
||||
verify(ballItem, timeout(2000).atLeastOnce()).draw();
|
||||
verify(ballItem, timeout(2000).atLeastOnce()).move();
|
||||
|
||||
ballThread.stopMe();
|
||||
ballThread.join();
|
||||
ballThread.stopMe();
|
||||
ballThread.join();
|
||||
|
||||
verifyNoMoreInteractions(ballItem);
|
||||
verifyNoMoreInteractions(ballItem);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the {@link BallThread} is interruptible
|
||||
*/
|
||||
@Test(timeout = 5000)
|
||||
@Test
|
||||
public void testInterrupt() throws Exception {
|
||||
final BallThread ballThread = new BallThread();
|
||||
final UncaughtExceptionHandler exceptionHandler = mock(UncaughtExceptionHandler.class);
|
||||
ballThread.setUncaughtExceptionHandler(exceptionHandler);
|
||||
ballThread.setTwin(mock(BallItem.class));
|
||||
ballThread.start();
|
||||
ballThread.interrupt();
|
||||
ballThread.join();
|
||||
assertTimeout(ofMillis(5000), () -> {
|
||||
final BallThread ballThread = new BallThread();
|
||||
final UncaughtExceptionHandler exceptionHandler = mock(UncaughtExceptionHandler.class);
|
||||
ballThread.setUncaughtExceptionHandler(exceptionHandler);
|
||||
ballThread.setTwin(mock(BallItem.class));
|
||||
ballThread.start();
|
||||
ballThread.interrupt();
|
||||
ballThread.join();
|
||||
|
||||
verify(exceptionHandler).uncaughtException(eq(ballThread), any(RuntimeException.class));
|
||||
verifyNoMoreInteractions(exceptionHandler);
|
||||
verify(exceptionHandler).uncaughtException(eq(ballThread), any(RuntimeException.class));
|
||||
verifyNoMoreInteractions(exceptionHandler);
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user