Using JUnit for testing classes-
Have a class TestAccess.java
having
static private TestAccess instance = new TestAccess();
public static TestAccess getTestAccess() {
returns instance;
}
For testing a test class A.java
used JMockit to mock the getTestAccess
method
@Mock
TestAccess mockTestaccess;
@Test
public void testMethod() {
new MockUp<TestAccess>() {
@mockit.Mock
public TestAccess getTestAccess() {
return mockTestaccess;
}
};
TestAccess test=TestAccess.getTestAccess();
}
In another class B.java
I don't want to use mock and call
TestAccess.java
as follows
@Test
public void doTest()
{
TestAccess test=TestAccess.getTestAccess();
}
B.java
if run independently, the real TestAccess
instance is returned and works fine.
However during maven run it fails as even in B.java
the
TestAccess.getTestAccess()
return the mock defined in A.java
and not the real instance as expected.
Could anyone guide how can this be resolved.