package ru.yandex.qatools.allure.testng;

import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.testng.annotations.*;
import ru.yandex.qatools.allure.Allure;
import ru.yandex.qatools.allure.events.*;

import java.lang.annotation.Annotation;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.mockito.Mockito.*;

/**
 * @author Kirill Kozlov kozlov.k.e@gmail.com
 *         Date: 02.02.14
 */
public class InternalAllureTestListenerTest {

    private InternalAllureTestListener internalListener;
    private Allure allure;

    @BeforeMethod
    public void setUp() {
        internalListener = new InternalAllureTestListener("suite");
        allure = mock(Allure.class);

        internalListener.setLifecycle(allure);
    }

    @Test
    public void skipTestFireTestCaseStartedEvent() {
        AllureTestResultAdaptor testResult = mock(AllureTestResultAdaptor.class);
        when(testResult.getName()).thenReturn("test");
        when(testResult.getAnnotations()).thenReturn(new Annotation[0]);

        internalListener.onTestSkipped(testResult);

        verify(allure).fire(isA(TestCaseStartedEvent.class));
    }

    @Test
    public void skipTestFiredSkippedEventWithThrowable() {
        AllureTestResultAdaptor testResult = mock(AllureTestResultAdaptor.class);
        when(testResult.getThrowable()).thenReturn(new NullPointerException());
        when(testResult.getName()).thenReturn("test");
        when(testResult.getAnnotations()).thenReturn(new Annotation[0]);

        internalListener.onTestSkipped(testResult);

        ArgumentCaptor<TestCaseEvent> firedEvent = ArgumentCaptor.forClass(TestCaseEvent.class);
        verify(allure).fire(firedEvent.capture());

        assertThat(firedEvent.getValue(), instanceOf(TestCaseSkippedEvent.class));
        TestCaseSkippedEvent skippedEvent = (TestCaseSkippedEvent)firedEvent.getValue();
        assertThat(skippedEvent.getThrowable(), instanceOf(NullPointerException.class));
    }

    @Test
    public void skipTestFiredEventsOrder() {
        AllureTestResultAdaptor testResult = mock(AllureTestResultAdaptor.class);
        when(testResult.getThrowable()).thenReturn(new NullPointerException());
        when(testResult.getName()).thenReturn("test");
        when(testResult.getAnnotations()).thenReturn(new Annotation[0]);

        internalListener.onTestSkipped(testResult);

        InOrder inOrder = inOrder(allure);
        inOrder.verify(allure).fire(isA(TestCaseStartedEvent.class));
        inOrder.verify(allure).fire(isA(TestCaseSkippedEvent.class));
        inOrder.verify(allure).fire(isA(TestCaseFinishedEvent.class));
    }
}

@@ -39,43 +39,8 @@
 g;%0A%0A
-import org.mockito.ArgumentCaptor;%0A
 impo
@@ -63,16 +63,16 @@
 nOrder;%0A
+
 import o
@@ -224,118 +224,8 @@
 n;%0A%0A
-import static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.core.IsInstanceOf.instanceOf;%0A
 impo
@@ -385,16 +385,139 @@
 Test %7B%0A%0A
+    private static final String DEFAULT_SUITE_NAME = %22suite%22;%0A    private static final String DEFAULT_TEST_NAME = %22test%22;%0A%0A
     priv
@@ -695,23 +695,34 @@
 istener(
-%22suite%22
+DEFAULT_SUITE_NAME
 );%0A     
@@ -991,38 +991,49 @@
 e()).thenReturn(
-%22test%22
+DEFAULT_TEST_NAME
 );%0A        when(
@@ -1166,36 +1166,39 @@
 fy(allure).fire(
-isA(
+eq(new 
 TestCaseStartedE
@@ -1193,38 +1193,71 @@
 CaseStartedEvent
-.class
+(DEFAULT_SUITE_NAME, DEFAULT_TEST_NAME)
 ));%0A    %7D%0A%0A    @
@@ -1394,32 +1394,90 @@
 Adaptor.class);%0A
+        Throwable throwable = new NullPointerException();%0A
         when(tes
@@ -1515,34 +1515,17 @@
 urn(
-new NullPointerException()
+throwable
 );%0A 
@@ -1717,406 +1717,82 @@
     
-ArgumentCaptor%3CTestCaseEvent%3E firedEvent = ArgumentCaptor.forClass(TestCaseEvent.class);%0A        verify(allure).fire(firedEvent.capture());%0A%0A        assertThat(firedEvent.getValue(), instanceOf(TestCaseSkippedEvent.class));%0A        TestCaseSkippedEvent skippedEvent = (TestCaseSkippedEvent)firedEvent.getValue();%0A        assertThat(skippedEvent.getThrowable(), instanceOf(NullPointerException.class
+verify(allure).fire(eq(new TestCaseSkippedEvent().withThrowable(throwable)
 ));%0A
@@ -2053,38 +2053,49 @@
 e()).thenReturn(
-%22test%22
+DEFAULT_TEST_NAME
 );%0A        when(

Use eq() matcher for mockito verify