I have:
public class A extends B {
public ObjectC methodToTest() {
return getSomething();
}
}
/* this class is in other project and compiles in project I want test */
public class B {
public ObjectC getSomething() {
//some stuff calling external WS
}
}
and on test:
#RunWith(MockitoJUnitRunner.class)
public class ATest {
#Mock
B bMock;
#InjectMocks
A classTotest;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
#Test
public void getMethodToTestShouldReturnObjectC() {
Mockito.when(bMock.getSomething()).thenReturn(new ObjectC());
assertEquals(classTotest.methodToTest().getClass, ObjectC.class);
}
}
But when I run test Mockito is calling B (and it fails because calls a ws...)
I read a lot of stuff about this but I can't solve it.
¿How Can I mock getSomething() to return ObjectC?
Mockito.when(bMock.getSomething()).thenReturn(new ObjectC());
This method changes only bMock. It doesn't change any other class B (or A) instances.
Related
I'm very new to writing unit tests for java. I want to write a test for method1:
public ClassA {
public String method1() {
ClassB classInst = ClassB.getInstance()
return classInst.doSomething()
}
}
Here, doSomething in classB connects to some databases and does an operation. This is my test class:
public TestClassA {
#Mock
private ClassB classBInst = mock(ClassB.class)
#InjectMocks
private ClassA classAInst = new ClassA()
#Before
public void setup() {
when(classInst.isClientEnabled()).thenReturn("ReturnStr");
}
#Test
public void testMethod1() {
String result = classAInst.method1();
assertEquals(result, "ReturnStr")
}
}
But the assert is failing because ClassB.doSomething() is not returning the mocked return value. Am I doing something wrong here? What is the correct way to mock classB here? I am using junit4 and mockito here
How can I execute a method once before all tests in all classes start?
I have a program that needs a system property to be set before any test start. Is there any way to do that?
Note: #BeforeClass or #Before are used just for the same test class. In my case, I'm looking for a way to execute a method before all test classes start.
To setup precondition to your test cases you can use something like this -
#Before
public void setUp(){
// Set up you preconditions here
// This piece of code will be executed before any of the test case execute
}
if you need to run that method before the start of all test you should use the annotation #BeforeClass or if you need to execute the same method every time you will execute a test method of that class you must use #Before
f.e
#Before
public void executedBeforeEach() {
//this method will execute before every single test
}
#Test
public void EmptyCollection() {
assertTrue(testList.isEmpty());
}
You can make use of a Test Suite.
The test suite
#RunWith(Suite.class)
#Suite.SuiteClasses({ TestClass.class, Test2Class.class, })
public class TestSuite {
#BeforeClass
public static void setup() {
// the setup
}
}
and, the test classes
public class Test2Class {
#Test
public void test2() {
// some test
}
}
public class TestClass {
#Test
public void test() {
// some test
}
}
Or, you can have a base class which handles the setup
public class TestBase {
#BeforeClass
public static void setup() {
// setup
}
}
and, then the test classes can extend the base class
public class TestClass extends TestBase {
#Test
public void test() {
// some test
}
}
public class Test2Class extends TestBase {
#Test
public void test() {
// some test
}
}
However, this will call the setup method in TestBase for all its subclasses everytime each of them executes.
I want to mock private method, which called from my test method, but instead of mocking, PowerMockito invoke toMockMethod, and I get NPE.
toMockMethod is in the same class.
#RunWith(PowerMockRunner.class)
public class PaymentServiceImplTest {
private IPaymentService paymentService;
#Before
public void init() {
paymentService = PowerMockito.spy(Whitebox.newInstance
(PaymentServiceImpl.class));
MockitoAnnotations.initMocks(this);
}
#Test
public void test() throws Exception {
...
PowerMockito.doReturn(mockedReturn)
.when(paymentService,
"toMockMethod",
arg1, arg2);
}
}
Is it normal situation? What a sense to mock method if it has been invoked?
To enable static or non-public mocking with PowerMock for a class, the class should be added to annotation #PrepareForTest. In your case, it should be:
#RunWith(PowerMockRunner.class)
#PrepareForTest(PaymentServiceImpl.class)
public class PaymentServiceImplTest {
private IPaymentService paymentService;
#Before
public void init() {
paymentService = PowerMockito.spy(Whitebox.newInstance
(PaymentServiceImpl.class));
MockitoAnnotations.initMocks(this);
}
#Test
public void test() throws Exception {
...
PowerMockito.doReturn(mockedReturn)
.when(paymentService,
"toMockMethod",
arg1, arg2);
}
}
I'm gonna leave a second answer for my future self here. There's an alternative problem here. If you're calling Static.method make sure "method" is actually defined in Static and not up the hierarchy.
In my case the code called Static.method, but Static extends from StaticParent, and "method" is actually defined in StaticParent.
#RunWith(PowerMockRunner.class)
#PrepareForTest(StaticParent.class)
public class YourTestClass {
#Before
public init() {
PowerMockito.mockStatic(StaticParent.class);
when(StaticParent.method("")).thenReturn(yourReturnValue);
}
}
public class ClassYoureTesting {
public someMethod() {
Static.method(""); // This returns yourReturnValue
}
I have a class A which calls a static method of an abstract class B which throws some exception. I wanted to test for this exception. I am using junit 4.1, mockito 1.9.5 and powermock 1.6.6. The classes are :
abstract class B {
public static void meth(String str) throws SomeException1, SomeException2,SomeException3 {
//some code
}
}
class A{
public void method() throws SomeException1, SomeException2,SomeException3 {
B.meth1("abc");
}
}
I want to test these exceptions and here is my test class
#RunWith(MockitoJUnitRunner.class)
#PrepareForTest(B.class)
class Test throws Throwable {
public void testException(){
PowerMockito.mockStatic(B.class); //Line 6
when(B.meth(Mockito.any(String.class))).thenThrow(new SomeException1(), new SomeException2(), new SomeException3() );
A obj=new A();
obj.method();
}
}
}
While executing this test case I get an exception
org.powermock.api.mockito.ClassNotPreparedException:
The class B not prepared for test.
To prepare this class, add class to the '#PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or method level.
at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.java:32)
at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.java:38)
at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.java:10)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:56)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:71)
at Test.testException(Test.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Any suggestion as to why am I getting this ?
You need this: #RunWith(PowerMockRunner.class)
You can use powermock to mock static method.
Code example
#RunWith(PowerMockRunner.class)
public class TestStaticMethodExample {
private SomeClass c = new SomeClass ("g", "123");
#PrepareForTest({ SomeStatic.class })
#Test
public void stubStaticMethod() {
PowerMockito.mockStatic(SomeStatic.class);
PowerMockito.when(SomeStatic.getSummary()).thenReturn(new
SomeClass("t", 9999));
}
}
I am trying to unit test a method, which has different branches depending upon the value of an object that is created inside it. The below code demonstrates it.
public class AClass {
public void method2() {
//Some code goes here
}
public void method1(BClass bObject) {
C_Class cObject = bObject.someMethodThatReturnsC();
if(cObject != null) {
method2();
method2();
}
}}
Below is the TestClass:
public class AClassTest {
#InjectMocks
AClass AClassSpy;
#Mock
BClass b_objectMock;
#Mock
C_Class c_objectMock;
#BeforeMethod
public void beforeMethod() {
AClassSpy = spy(new AClass());
MockitoAnnotations.initMocks(this);
}
public void method1Test () {
doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC());
AClassSpy.method1(b_objectMock);
verify(AClassSpy, times(2).method2();
}
}
However, it always FAILS, since the c_objectMock is always null. What do I do to tell Mockito to not to return a null object?
It works good, just use #Before annotation from junit, not #BeforeMethod, and mark your test method like #Test and remove second bracket from
doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC())<-this one;
and add bracket at verify:
verify(AClassSpy, times(2)<-here.method2();
And just take care of your code!
This should work:
public class AClassTest {
#InjectMocks
private AClass AClassSpy;
#Mock
private BClass b_objectMock;
#Mock
private C_Class c_objectMock;
#Before
public void beforeMethod() {
AClassSpy = spy(new AClass());
MockitoAnnotations.initMocks(this);
}
#Test
public void method1Test() {
doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC();
AClassSpy.method1(b_objectMock);
verify(AClassSpy, times(2)).method2();
}
}
Instead of before method you can use annotation #RunWith. It looks clearly:
#RunWith(MockitoJUnitRunner.class)
public class AClassTest {
#Spy
#InjectMocks
private AClass AClassSpy;
#Mock
private BClass b_objectMock;
#Mock
private C_Class c_objectMock;
#Test
public void method1Test() {
doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC();
AClassSpy.method1(b_objectMock);
verify(AClassSpy, times(2)).method2();
}
}
You are having this behaviour because you are not mocking property the call to someMethodThatReturnsC.
It should be:
doReturn(c_objectMock).when(b_objectMock).someMethodThatReturnsC();