package io.tracee.backend.threadlocalstore;

import org.junit.Before;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

public class ThreadLocalTraceeLoggerTest {

	private final ThreadLocalTraceeLogger unit = new ThreadLocalTraceeLogger(this.getClass());

	@Before
	public void setupMocks() throws Exception {
		// Replacing System.err with a mocked PrintStream
		final Field errField = Whitebox.getField(System.class, "err");
		errField.setAccessible(true);
		// Remove final modifier of System.err
		Field modifiersField = Field.class.getDeclaredField("modifiers");
		modifiersField.setAccessible(true);
		modifiersField.setInt(errField, errField.getModifiers() & ~Modifier.FINAL);
		// Replace with mock
		errField.set(System.class, mock(PrintStream.class));
	}

	@Test
	public void shouldCreateProperStringWithoutMessage() {
		final String logString = unit.buildLogString(ThreadLocalTraceeLogger.LEVEL.WARN, null);
		assertThat(logString, equalTo("WARN - (io.tracee.backend.threadlocalstore.ThreadLocalTraceeLoggerTest) :"));
	}

	@Test
	public void shouldCreateProperStringWithMessage() {
		final String logString = unit.buildLogString(ThreadLocalTraceeLogger.LEVEL.ERROR, "ErrorString");
		assertThat(logString, equalTo("ERROR - (io.tracee.backend.threadlocalstore.ThreadLocalTraceeLoggerTest) :ErrorString"));
	}

	@Test
	public void debugWithoutStacktraceShouldBeDiscarded() throws Exception {
		unit.debug("DebugString");
		verify(System.err, never()).println(anyString());
	}

	@Test
	public void infoWithoutStacktraceShouldBeDiscarded() throws Exception {
		unit.info("InfoString");
		verify(System.err, never()).println(anyString());
	}

	@Test
	public void warnWithoutStacktraceShouldBeDogged() throws Exception {
		unit.warn("WarnString");
		verify(System.err).println(anyString());
	}

	@Test
	public void errorWithoutStacktraceShouldBeDogged() throws Exception {
		unit.error("ErrorString");
		verify(System.err).println(anyString());
	}

	@Test
	public void debugWithStacktraceShouldBeDiscarded() throws Exception {
		unit.debug("DebugString", new Error());
		verify(System.err, never()).println(anyString());
	}

	@Test
	public void infoWithStacktraceShouldBeDiscarded() throws Exception {
		unit.info("InfoString", new Error());
		verify(System.err, never()).println(anyString());
	}

	@Test
	public void warnWithStacktraceShouldBeDogged() throws Exception {
		unit.warn("WarnString", new Error());
		verify(System.err, times(2)).println(anyString());
	}

	@Test
	public void errorWithStacktraceShouldBeDogged() throws Exception {
		unit.error("ErrorString", new Error());
		verify(System.err, times(2)).println(anyString());
	}
}
@@ -2645,32 +2645,69 @@
 ows Exception %7B%0A
+%09%09final Error t = mock(Error.class);%0A
 %09%09unit.warn(%22War
@@ -2712,35 +2712,25 @@
 arnString%22, 
-new Error()
+t
 );%0A%09%09verify(
@@ -2733,39 +2733,44 @@
 ify(System.err, 
-times(2
+atLeastOnce(
 )).println(anySt
@@ -2770,32 +2770,73 @@
 n(anyString());%0A
+%09%09verify(t).printStackTrace(System.err);%0A
 %09%7D%0A%0A%09@Test%0A%09publ
@@ -2896,34 +2896,70 @@
 ception %7B%0A%09%09
-unit.error
+final Error t = mock(Error.class);%0A%09%09unit.warn
 (%22ErrorStrin
@@ -2958,35 +2958,25 @@
 rorString%22, 
-new Error()
+t
 );%0A%09%09verify(
@@ -2991,15 +2991,20 @@
 rr, 
-times(2
+atLeastOnce(
 )).p
@@ -3024,12 +3024,53 @@
 ing());%0A
+%09%09verify(t).printStackTrace(System.err);%0A
 %09%7D%0A%7D

Use different verification method. I think due different JDK-vendors the previous version fails when build with travis (and OpenJDK)