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