I'm trying to crash through the brick wall between me and Mockito. I've torn my hair out over trying to get correct import static statements for Mockito stuff. You'd think someone would just throw up a table saying that anyInt() comes from org.mockito.Matchers and when() comes from org.mockito.Mockito, etc., but that would be too helpful to newcomers, no?
This sort of thing, especially when mixed in with myriad more import statements ending in asterisks, isn't always very helpful:
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
Yes, I know about and have been trying to use the Eclipse Window -> Preferences-> Java -> Editor-> Content Assist -> Favorites mechanism. It helps, but it doesn't hit the nail on the head.
Any answers to this question would be appreciated.
Many thanks,
Russ
Here's what I've been doing to cope with the situation.
I use global imports on a new test class.
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.Matchers.*;
When you are finished writing your test and need to commit, you just CTRL+SHIFT+O to organize the packages. For example, you may just be left with:
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.anyString;
This allows you to code away without getting 'stuck' trying to find the correct package to import.
The problem is that static imports from Hamcrest and Mockito have similar names, but return Matchers and real values, respectively.
One work-around is to simply copy the Hamcrest and/or Mockito classes and delete/rename the static functions so they are easier to remember and less show up in the auto complete. That's what I did.
Also, when using mocks, I try to avoid assertThat in favor other other assertions and verify, e.g.
assertEquals(1, 1);
verify(someMock).someMethod(eq(1));
instead of
assertThat(1, equalTo(1));
verify(someMock).someMethod(eq(1));
If you remove the classes from your Favorites in Eclipse, and type out the long name e.g. org.hamcrest.Matchers.equalTo and do CTRL+SHIFT+M to 'Add Import' then autocomplete will only show you Hamcrest matchers, not any Mockito matchers. And you can do this the other way so long as you don't mix matchers.
For is()
import static org.hamcrest.CoreMatchers.*;
For assertThat()
import static org.junit.Assert.*;
For when() and verify()
import static org.mockito.Mockito.*;
My imports
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
And it works
import static org.mockito.Matchers.anyInt;
e.g.
when(listMock.get(anyInt())).thenReturn("stackoverflow");
Related
So, I included Junit5 to my maven project and it kind of works, but anyway when I try to import assertions to a test I can only put org.junit.jupiter.api.Assertions.* without beign able to specify some precise assertion and in the code I have to write for example Assertions.assertThrows().
Why can't I pick any specific assertion from the list?
What could be the problem?
If you only want to import a single method, you have to use a static import, like this:
import static org.junit.jupiter.api.Assertions.assertThrows;
Then you can simply use it like:
assertThrows(Exception.class, () -> ...)
You can import specific assertions by using a static import:
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
This works for me in eclipse…
This seems like it shouldn't compile and run as Object does not have a fail() method. At compile time is something funky happening? (I am using NetBeans):
import static org.junit.Assert.*;
import org.junit.Test;
public class Test {
#Test
public void hello() {
fail();
}
}
Regards,
Guido
Your import static line imports all static members of the Assert class into the static namespace of your compilation unit. The fail() call refers to Assert.fail().
The confusion you are experiencing regarding where fail() is defined is precisely why I don't usually recommend using import static. In my own code, I usually import the class and use it to invoke the static methods:
import org.junit.Assert;
import org.junit.Test;
public class Test {
#Test
public void hello() {
Assert.fail();
}
}
Much more readable.
However, as JB Nizet points out, it is fairly common practice to use import static for JUnit's assertions; when you write and read enough JUnit tests, knowing where the assertion methods come from will become second nature.
This is perfectly correct and it will run and compile - I already checked using eclipse.
The reason is the static import:
import static org.junit.Assert.*;
that adds all the static fields or methods from the org.junit.Assert class - hence including fail() method.
Nevertheless a problem that might occur is the fact that the name of your test class is the same as the name of the annotation
#Test
hence it will generate an error:
The import org.junit.Test conflicts with a type defined in the same file
This error is coming because your classname and annotation name are same(Test).Change your class name to 'Test1' or other than Test.
I am using mockito-all-1.9.5-rc1.jar and powermock-mockito-1.4.12-full.jar.
When I run this simple unit test for mocking final method in non-final class.
import static org.junit.Assert.assertEquals;
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;
#RunWith(PowerMockRunner.class)
#PrepareForTest(ABC.class)
public class ABCTest {
#Test
public void finalCouldBeMock() {
final ABC abc = PowerMockito.mock(ABC.class);
PowerMockito.when(abc.myMethod()).thenReturn("toto");
assertEquals("toto", abc.myMethod());
}
}
When I ran it, I got
java.lang.NoClassDefFoundError: org/mockito/internal/MockitoInvocationHandler
Caused by: java.lang.ClassNotFoundException: org.mockito.internal.MockitoInvocationHandler
When I search fo class MockitoInvocationHandler in mockito-all-1.9.5-rc1.jar and powermock-mockito-1.4.12-full.jar. I couldn't find any.
Need help with this issue! Thank you
Mockito 1.9.5-rc1 had to be refactored internally to allow third party mock maker. MockitoInvocationHandler was part of the Mockito's internals (as the package name suggests) up to Mockito 1.9.0.
Due to these changes, current some older version Powermock releases as of today are not compatible with the latest Mockito release.
Another reason to avoid mocking/stubbing finals or statics ;)
Hope that helps
Cheers,
My code compiles fine in Eclipse, but when I try to compile from the commandline (via our ruby-based buildr system), I get this error message:
static import only from classes and interfaces
Suggesting that static import of public static fields is not permitted. What should I look for to help diagnose this problem? How can I fix it?
Update:
per #Ted's request, the constant declaration in the referenced file:
public static final String NULL = "<NULL>";
and the (bowdlerized) reference in the referring file:
import static my.path.MyClass.NULL;
My guess is that Eclipse and buildr are using either different Java compiler versions or different compiler flags. There's a bug in the Java 7 compiler (bug ID: 715906) that generates this error when you statically import specific fields. The work-around is to use a wildcard static import. So instead of:
import static pkg.Class.staticField;
do this:
import static pkg.Class.*;
Late answer but I just got a similar issue and figured it out. I'll post in case it helps anyone else who finds this page...
I got a similar error when, after a big merge and refactor, I accidentally put a test class into src/main/java instead of src/test/java. Since the JUnit dependency was scope=tests, it didn't work in pure maven. Maybe you are having the same issue
I also had this error and my issue turned out to be a wayward static import of a junit 4 package in my test source file.
I had the following:
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
I removed the import static org.junit.Assert.fail; (no idea how I managed to get that in there in the first place) and all is now working.
I accidentally set test directory as source. And Test sources were considered as source files.
sourceSets.main.java.srcDirs 'src'
| -- src
| -- main
| -- test
Fix:
sourceSets.main.java.srcDirs 'src/main'
Some how same solution mentioned by #m-watson
I have replaced
import static org.junit.Assert.assertThrows;
With
import static org.junit.jupiter.api.Assertions.assertThrows;
and it worked
The error, Netbeans gives me, is:
static import only from classes and interfaces
which is somehow strange for me, as this:
import org.lwjgl.opengl.GL11;
works fine while this:
import static org.lwjgl.opengl.GL11;
doesn't. Why is it not working for me?
BTW, GL11 is a class and I don't know why but Netbeans, when importing statically, thinks opengl is the class I want to import.
You wanna write:
import static org.lwjgl.opengl.GL11.*;
You are importing the members of the class, thus the * at the end.
Static import allows you to import static fields of other class. For example you can say
import static java.awt.Color.RED;
And then use RED in your class without mentioning class where it is defined.