I am trying to use power mock in scala.
Class I want to test
class Operations {
def write(s: String): Unit = {
val customWriter = new CustomWriter()
customWriter.write(s)
}
}
Class I want to mock
class CustomWriter {
def write(s: String): Unit = {
//do something
}
}
My spec file
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.scalatest.mockito.MockitoSugar.mock
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.scalatest.Matchers._
import org.scalatest.{BeforeAndAfterAll, FunSpec, PrivateMethodTester}
#RunWith(classOf[PowerMockRunner])
#PrepareForTest(Array(classOf[Operations]))
class PowerMockTest extends FunSpec {
describe("Using power mock") {
it("check constructor call") {
val ops = new Operations()
val mocked = mock[CustomWriter]
PowerMockito.whenNew(classOf[CustomWriter]).withNoArguments().thenReturn(mocked)
ops.write("Write Me")
PowerMockito.verifyNew(classOf[CustomWriter]).withNoArguments()
}
}
}
How ever the tests are failing with the stacktrace
java.lang.AssertionError: Wanted but not invoked customWriter();
Actually, there were zero interactions with this mock.
I even tried verifying the method call on mocked object, still no luck
Mockito.verify(mocked).write("Write me")
Please help me understand what am I missing here
Looks like you missing CustomWriter from the #PrepareForTest annotation.
The need to use Powermock can be a sing that you're doing something wrong. Maybe you can change your approach so that you can test it with Mockito.
If you have control over the Operations class, you can maybe pass the CustomWriter instance instead of instantiating it inside the method.
Related
This question is similar to this one but the solution is not working. There is a problem with mockito and Guice
package mock
import com.google.inject.persist.Transactional;
import lombok.NonNull;
import java.util.ArrayList;
import java.util.List;
public class SomeClass {
public int A() {
ArrayList<Object> objects = new ArrayList<>();
return this.B(objects);
}
#Transactional
public int B(#NonNull List<Object> someList) {
return 1;
}
}
I want to unit test A and check the parameters that calls B with. Hence I do:
#Test
void someTest() {
SomeClass obj = GuiceConfig.getInjector()
.getInstance(SomeClass.class);
SomeClass objSpy = Mockito.spy(obj);
ArgumentCaptor<List<Object>> captor =
ArgumentCaptor
.forClass(List.class);
// when(objSpy.B(captor.capture())).thenReturn(1);
doReturn(0).when(objSpy).B(captor.capture());
}
Instead of mocking B, it is calling the actual implementation of B with null and raising a NPE. In the aforementioned question it was suggested that using doReturn would fix this issue, because using when(objSpy.B(captor.capture)).thenReturn(0) would execute the underlying method. Note, if I don't use #Transactional then it works (where as when call fails)
I realize there are many many very similar questions. I've been through all of them and I still cannot make my code work.
I have a service defined in my Spring-Boot application, just like this:
#Service
public class FileStorageService {
private final Path fileStorageLocation;
#Autowired
public FileStorageService(final FileStorageProperties fileStorageProperties) {
//FileStorageProperties is a very simple class that right now just holds one String value
this.fileStorageLocation = Paths.get(fileStorageProperties.getUploadDir())
.toAbsolutePath()
.normalize();
try {
Files.createDirectories(fileStorageLocation);
} catch (IOException e) {
// FileStorageException is my custom, runtime exception
throw new FileStorageException("Failed to create directory for stored files", e);
}
}
}
And I want to test scenario, when directory creation fails and thus I need to mock method Files.createDirectories(). My test class looks like this:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.IOException;
import java.nio.file.Files;
#RunWith(PowerMockRunner.class)
#PrepareForTest({Files.class})
public class FileStorageServiceTest {
private static final String UPLOAD_DIR = "uploadDir";
#Test(expected = FileStorageException.class)
public void some_test() throws IOException {
PowerMockito.mockStatic(Files.class);
PowerMockito.when(Files.createDirectories(Mockito.any())).thenThrow(new IOException());
new FileStorageService(createFileStorageProperties());
}
private FileStorageProperties createFileStorageProperties() {
final FileStorageProperties fileStorageProperties = new FileStorageProperties();
fileStorageProperties.setUploadDir(UPLOAD_DIR);
return fileStorageProperties;
}
}
I believe I followed every step from tutorials and questions I've read.
I use:
#RunWith(PowerMockRunner.class),
#PrepareForTest({Files.class}),
PowerMockito.mockStatic(Files.class),
and PowerMockito.when(Files.createDirectories(Mockito.any())).thenThrow(new IOException());.
Still, no exception is thrown during test and it fails. WIll be super thankful for the help, cause I feel I miss something really simple and just cannot see it.
From: https://github.com/powermock/powermock/wiki/Mock-System
Normally you would prepare the class that contains the static methods (let's call it X) you like to mock but because it's impossible for PowerMock to prepare a system class for testing so another approach has to be taken. So instead of preparing X you prepare the class that calls the static methods in X!
Basically, we mock the class's use of the System class, rather than unmockable System class itself.
#PrepareForTest({Files.class})
An alternative, non-Powermock way to do this without mocking any system class would be to create a helper method, #Spy the original class, and mock that helper method specifically to throw the exception.
when(spy.doSomethingWithSystemClasses()).thenThrow(new Exception("Foo");
The Test case I am writing for:
public class AClassUnderTest {
// This test class has a method call
public Long methodUnderTest() {
// Uses the FinalUtilityClass which contains static final method
FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>);
// I want to mock above call so that test case for my "methodUnderTest" passes
}
}
I have one final class.
public final class FinalUtilityClass {
/**
* Method has 3 parameters
*/
public static final MyBean myStaticFinalMethod(<3-parameters-here>) {
}
}
I have already added below code in my test class:
#RunWith(PowerMockRunner.class)
#PrepareForTest({ FinalUtilityClass.class })
I want to write test case for mocking it.
I want to mock the call of myStaticFinalMethod() so that I can get the expected MyBean instatnce which I can use in further code to pass my test case.
The <3-parameters-here> are Calendar, String, String.
I tried doing:
1)
PowerMockito.mock(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
2)
PowerMockito.mockStatic(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
3)
PowerMockito.spy(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>).thenReturn(new MyBean());
But nothing worked for me. Please suggest what is correct way for mocking static final method in final class.
The following steps are required to mock calls to static methods:
Use the #RunWith(PowerMockRunner.class) annotation at the class-level of the test case.
Use the #PrepareForTest(ClassThatContainsStaticMethod.class) annotation at the class-level of the test case
Use PowerMock.mockStatic(ClassThatContainsStaticMethod.class) to mock all methods of this class
When you follow these steps as documented, your tests should work. And as the OP seems to be confused about PowerMock vs. PowerMockito - that is (more or less) the same thing:
PowerMock and PowerMockito are based on the same technology. They just have different "connectors" to either work with EasyMock or Mockito. So, yes the above example says PowerMock.mockStatic() - but PowerMockito has mockStatic() methods as well. In that sense: the core things (for example regarding preparation with annotations) are the same. See here for example (they are so close that the linked tutorial says "Intro to PowerMock" - although it does introduce PowerMockito.
And as you seem to not believe me - see this example:
package ghostcat.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
final class ClassWithStatic {
public final static int ignoreMethodCall(String a, String b, int c) {
System.out.println("SHOULD NOT SHOW UP: " + a);
return c;
}
}
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassWithStatic.class)
public class MockStaticTest {
#Test
public void test() {
PowerMockito.mockStatic(ClassWithStatic.class);
PowerMockito.when(ClassWithStatic.ignoreMethodCall("a", "b", 5)).thenReturn(42);
org.junit.Assert.assertEquals(ClassWithStatic.ignoreMethodCall("a", "b", 5), 42);
}
}
This test passes; and doesn't print anything. Therefore the final static method gets mocked.
I faced a similar problem, and it cost me a lot of time.
I solved it, based on this Mock system class with PowerMock documentation, when I realized that the #PrepareForTest({ SomeClassToBePreparedForTesting.class }) must receive the class that calls the static methods defined in the final class, so the preparation for the test, in your case, must receive the AClassUnderTest.class.
#RunWith(PowerMockRunner.class)
#PrepareForTest({ AClassUnderTest.class })
The correct way to mock the final class with the target static method is to use the PowerMock.mockStatic(FinalClassThatDefinesStaticMethod.class), in your case the FinalUtilityClass.class.
PowerMockito.mockStatic(FinalUtilityClass.class)
PowerMockito.when(FinalUtilityClass.method(params)).thenReturn(return);
In addition, if you have some issue like: 'It's not possible to mock final methods', you can create the file org/powermock/extensions/configuration.properties and set this config mockito.mock-maker-class=mock-maker-inline like the PowerMockito documentation presents.
To mock a static method powermock giving an exception while expect().
#Test
public void testRegistrarService()
{
mockStatic(IdGenerator.class);
expect(IdGenerator.generateNewId()).andReturn(42L);
long actualId=serTestObj.registerService();
replay(IdGenerator.class);
verify(IdGenerator.class);
assertEquals(42L,actualId);
}
public class ServiceRegistrator
{
public long registerService()
{
long id = IdGenerator.generateNewId();
return id;
}
}
public class IdGenerator
{
public static long generateNewId()
{
return System.currentTimeMillis();
}
}
Exception is:
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521)
at org.easymock.EasyMock.expect(EasyMock.java:499)
at home.powermock.testServiceRegistrator.testRegistrarService(testServiceRegistrator.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
how to mock staic method,while m using powerMock
i'm using intelliJ idea,how to resolve that exception.
Your code is missing the annotation
#PrepareForTest(IdGenerator.class)
In my case I was missing the following method in my test class
#ObjectFactory
/**
* Configure TestNG to use the PowerMock object factory.
*/
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
Once I added it, I got rid of the "no last call on a mock available" error.
You need to put the replay before the actual call to the method.
EDIT: I think part of the problem may be caused because of your imports. Try not to import static powermock and static easymock (I've found that I often confuse myself and forget which one I need to call replay on).
Try running the following code. If it doesn't run correctly, then it may be because of a problem with the particular version of PowerMock/EasyMock/Junit that you have.
TestClass:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
#RunWith(PowerMockRunner.class)
#PrepareForTest(IdGenerator.class)
public class TestClass {
#Test
public void testRegistrarService()
{
ServiceRegistrator serTestObj = new ServiceRegistrator();
PowerMock.mockStatic(IdGenerator.class);
expect(IdGenerator.generateNewId()).andReturn(42L);
PowerMock.replay(IdGenerator.class);
long actualId=serTestObj.registerService();
PowerMock.verify(IdGenerator.class);
assertEquals(42L,actualId);
}
}
IdGenerator:
public class IdGenerator {
public static long generateNewId()
{
return System.currentTimeMillis();
}
}
ServiceRegistrator:
public class ServiceRegistrator {
public long registerService()
{
long id = IdGenerator.generateNewId();
return id;
}
}
This question has been here for a long time but I'll try to give to it an aswer to explain what i did to resolve this problem.
First of all you have to use these two annotations:
#RunWith(PowerMockRunner.class)
This annotation let the current test class know what to use to run his tests, this is useful because we can use PowerMockRunner instead of JUnitRunner
#PrepareForTest(IdGenerator.class)
This annotation is used to prepare the class "IdGenerator" to be used in the test, prepare means that we will be able to mock the static methods as we do to the public methods
After added these two annotations we have to be sure we are using the right packages provided by PowerMock:
1) PowerMock:
Import: import org.powermock.api.easymock.PowerMock;
Use: We will use PowerMock to mock (and not only) our static method with the following code line
PowerMock.mockStatic(IdGenerator.class);
2) EasyMock:
Import: import org.easymock.EasyMock;
Use: We are going to use EasyMock to fake our object to be returned by our static method:
EasyMock.expect(IdGenerator.generateNewId()).andReturn(42L);
These was two examples on what are used PowerMock and EasyMock, and here I'll try to explain the code and what it does:
mockStatic(IdGenerator.class);
//We mock our IdGenerator class which have the static object
expect(IdGenerator.generateNewId()).andReturn(42L);
//We fake our method return object, when we'll call generateNewId()
//method it will return 42L
//With expecting we "record" our this method and we prepare it to be
//changed (it will return our decided value)
replay(IdGenerator.class);
//We go to perform our methods "registered" with the expect method
//inside the IdGenerator class, in this case with replay we just apply
//the changes of the expect to the method generateNewId()
long actualId = serTestObj.registerService();
//We create our object (which inside have a non static method that
//use generateNewId() static method)
verify(IdGenerator.class);
//We verify that the our faked method have been called
assertEquals(42L,actualId);
//We see if the two values are matching
Pay attention because replay must be used before you create the new object (actualId in this example) that will call the static faked methods.
Also do a lot of attention on what you are importing, for a distraction i was using
PowerMockito.mockStatic(className.class);
//from import org.powermock.api.mockito.PowerMockito;
Instead of
PowerMock.mockStatic(className.class);
//from import org.powermock.api.easymock.PowerMock;
I hope that this answer is clear and complete
By the way here i'll refer you to some useful links:
PowerMock Static Documentation on GitHub
Mvn Repository PowerMock Libraries
See you :D
Is it possible in JUnit to assert an object is an instance of a class? For various reasons I have an object in my test that I want to check the type of. Is it a type of Object1 or a type of Object2?
Currently I have:
assertTrue(myObject instanceof Object1);
assertTrue(myObject instanceof Object2);
This works but I was wondering if there is a more expressive way of doing this.
For example something like:
assertObjectIsClass(myObject, Object1);
I could do this:
assertEquals(myObject.class, Object1.getClass());
Is there a specific assert method that allows me to test a type of an object in a more elegant, fluid manner?
You can use the assertThat method and the Matchers that comes with JUnit.
Take a look at this link that describes a little bit about the JUnit Matchers.
Example:
public class BaseClass {
}
public class SubClass extends BaseClass {
}
Test:
import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
/**
* #author maba, 2012-09-13
*/
public class InstanceOfTest {
#Test
public void testInstanceOf() {
SubClass subClass = new SubClass();
assertThat(subClass, instanceOf(BaseClass.class));
}
}
Since assertThat which was the old answer is now deprecated, I am posting the correct solution:
assertTrue(objectUnderTest instanceof TargetObject);
Solution for JUnit 5
The documentation says:
However, JUnit Jupiter’s org.junit.jupiter.Assertions class does not provide an assertThat() method like the one found in JUnit 4’s org.junit.Assert class which accepts a Hamcrest Matcher. Instead, developers are encouraged to use the built-in support for matchers provided by third-party assertion libraries.
Example for Hamcrest:
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.jupiter.api.Test;
class HamcrestAssertionDemo {
#Test
void assertWithHamcrestMatcher() {
SubClass subClass = new SubClass();
assertThat(subClass, instanceOf(BaseClass.class));
}
}
Example for AssertJ:
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
class AssertJDemo {
#Test
void assertWithAssertJ() {
SubClass subClass = new SubClass();
assertThat(subClass).isInstanceOf(BaseClass.class);
}
}
Note that this assumes you want to test behaviors similar to instanceof (which accepts subclasses). If you want exact equal type, I don’t see a better way than asserting the two class to be equal like you mentioned in the question.
Experimental Solution for JUnit 5.8
In Junit 5.8, the experimental assertInstanceOf() method was added, so you don't need Hamcrest or AssertJ anymore. The solution is now as simple as:
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import org.junit.Test;
public class InstanceOfTest {
#Test
public void testInstanceOf() {
SubClass subClass = new SubClass();
assertInstanceOf(BaseClass.class, subClass);
}
}
Solution for JUnit 5 for Kotlin!
Example for Hamcrest:
import org.hamcrest.CoreMatchers
import org.hamcrest.MatcherAssert
import org.junit.jupiter.api.Test
class HamcrestAssertionDemo {
#Test
fun assertWithHamcrestMatcher() {
val subClass = SubClass()
MatcherAssert.assertThat(subClass, CoreMatchers.instanceOf<Any>(BaseClass::class.java))
}
}
Example for AssertJ:
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
class AssertJDemo {
#Test
fun assertWithAssertJ() {
val subClass = SubClass()
assertThat(subClass).isInstanceOf(BaseClass::class.java)
}
}
Solution for JUnit for Kotlin
What worked for me:
assert(obj is ClassName)
for exmaple
assert(obj is User)
NOTE: assert is coming from AssertionsJVM.kt file