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));
}
}
Related
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:
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.
I have a static method inside a class as follows:
public class config {
public static boolean isProcess() {
return false;
}
}
I am trying to mock the method isProcess as follows:
#RunWith(PowerMockRunner.class)
#PrepareForTest (config.class)
public class testConfig {
public static void main(String[] args) {
PowerMock.mockStatic(config.class);
expect(config.isProcess()).andReturn(true);
PowerMock.replay(config.class);
PowerMock.verify(config.class);
}
}
When I run the testConfig class, I get the following exception
Exception in thread "main" java.lang.IllegalStateException: no last
call on a mock available at
org.easymock.EasyMock.getControlForLastCall(EasyMock.java:466) at
org.easymock.EasyMock.expect(EasyMock.java:444) at
testConfig.main(testConfig.java:18)
Can some one help with this please?
I am writing JUnit for a class that references a legacy class via constructor. The legacy class is in a third party jar, so I can't refactor it to make life easier....
This is the class being tested...
public MyClass {
public String methodToTest(String param) {
LegacyClass legacy = new LegacyClass(param);
*..... etc ........*
}
}
This is what I am trying to do in the mockito JUnit.
public MyClassTest {
#Test
public void testMethodToTest() throws Exception {
LegacyClass legacyMock = mock(LegacyClass.class);
when(*the LegacyClass constructor with param is called*).thenReturn(legacyMock);
*.... etc.....*
}
}
Any ideas on how I can do this ?????
Make a builder for the LegacyClass:
public class LegacyClassBuilder {
public LegacyClass build(String param) {
return new LegacyClass(param);
}
}
That way your class can be tested so it creates the LegacyClass with correct parameter.
public MyClass {
private LegacyClassBuilder builder;
public setBuilder(LegacyClassBuilder builder) {
this.builder = builder;
}
public String methodToTest(String param) {
LegacyClass legacy = this.builder.build(param);
... etc
}
}
The test will look something like this:
// ARRANGE
LegacyClassBuilder mockBuilder = mock(LegacyClassBuilder.class);
LegacyClass mockLegacy = mock(LegacyClass.class);
when(mockBuilder.build(anyString()).thenReturn(mockLegacy);
MyClass sut = new MyClass();
sut.setBuilder(mockBuilder);
String expectedParam = "LOLCAT";
// ACT
sut.methodToTest(expectedParam);
// ASSERT
verify(mockBuilder).build(expectedParam);
If LegacyClass happens to be final then you need to create non-final wrapper for LegacyClass that MyClass will use.
You can use PowerMockito framework:
import static org.powermock.api.mockito.PowerMockito.*;
whenNew(LegacyClass.class)
.withParameterTypes(String.class)
.withArguments(Matchers.any(String.class))
.thenReturn(new MockedLegacyClass(param));
Then write your MockedLegacyClass implementation according to your test needs.
I believe it is not possible to mock constructors using Mockito. Instead, I will suggest the following approach:
public MyClass {
public String methodToTest(String param) {
if(legacy== null){
//when junit runs, you will get mocked object (not null), hence don't initialize
LegacyClass legacy = new LegacyClass(param);
}
*..... etc ........*
}
}
public MyClassTest {
#InjectMock
Myclass myclass; // inject mock the real testable class
#Mock
LegacyClass legacy
#Test
public void testMethodToTest() throws Exception {
// when(legacy-constructor).thenReturn() ---not Required
// instead directly mock the required methods using mocked legacy object
when(legacy.getSomething(Mockito.any(String.class))).thenReturn(null);
*.... etc.....*
}
}