2

Here I have wrote a simple test case using Junit and Mockito.

import org.jbehave.core.annotations.Given;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import com.test.dao.login.LoginDao;
import com.test.mapping.user.User;
import com.test.service.login.LoginService;
import com.test.service.login.impl.LoginServiceImpl;
import com.test.util.common.Common;

public class UserLoginSteps {
    @Mock
    Common common;

    @Mock
    LoginDao loginDao;

    @InjectMocks
    LoginService loginService =new LoginServiceImpl();

    @BeforeClass
    public static void beforeClass() {
        System.out.println("@BeforeClass");
    }

    @Before
    public void before() {
        System.out.println("@Before");
        MockitoAnnotations.initMocks(this);
    }

    @After
    public void after() {
        System.out.println("@After");
    }

    @AfterClass
    public static void afterClass() {
        System.out.println("@AfterClass");
    }


    @Given("$username username and $password password")
    @Test
    public void checkUser(String username, String password) throws Exception{

        when(common.checkNullAndEmpty("admin")).thenReturn(true);
        when(common.checkNullAndEmpty("password")).thenReturn(true);
        when(loginDao.getUser("admin","password")).thenReturn(new User());

        assertEquals(true,loginService.checkValidUser(username, password));
    }
}

I have initialize the Mock objects inside the before() function. But that function is not triggered out in running the test case.

I am using following dependencies.

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.8.9</version>
    <scope>test</scope>
</dependency>

I have seen similar questions to this scenario. But the following suggestions does not fix the issue.

Is any one can describe why it happen and how to fix this issue it will be great helpful. Thanks in advance.

After-before-not-working-in-testcase

Simple-junit-class-is-not-calling-the-before-method

Why-isnt-my-beforeclass-method-running

Shalika
  • 1,457
  • 2
  • 19
  • 38

1 Answers1

1

You should annotate your class with @RunWith(MockitoJUnitRunner.class) So the MickitoJunitRunner will take care of your Mocks and tests. But it will not work like this together with JBehave. You have to decide if you want to use JBehave or MockitoJUnitRunner.

In JBehave the correct annotations to use are: @BeforeScenario @AfterScenario @BeforeStory @AfterStory Please take a look at jbehave doc: http://jbehave.org/reference/stable/annotations.html

art333
  • 46
  • 4
  • Also you can use `Mockito.initMocks(this);` in the `@Before init();` method. - should not be on `@BeforeClass`! – LenglBoy Apr 09 '18 at 07:16
  • ( BeforeScenario AfterScenario BeforeStory AfterStory) This is what i really looking for.Thank you @ArturB3 – Shalika Apr 09 '18 at 08:30