I am testing my Quarkus application, and I would like to ignore some of my tests.
The test class is annotated with #io.quarkus.test.junit.QuarkusTest, and each method with #org.junit.jupiter.api.Test
I'm trying to use the #org.junit.Ignore annotation, but it's not working, the ignored tests are executed anyway.
This is the code:
#QuarkusTest
#TestHTTPEndpoint(MyResource::class)
class MyResourceTest {
#Test
#Ignore
fun `to be ignored`() {
assertTrue(false)
}
}
Does anyone know how can I achieve this?
You can use #Disabled annotation
You can also conditionally run tests with assumptions.
I have inherited a complex JUnit 4 test class which uses
#RunWith(MockitoJUnitRunner.class)
Because of a change in implementation I need to use some features from PowerMock to mock static and private methods. But when I try to switch at class level to
#RunWith(PowerMockRunner.class)
#PrepareForTest(MyClass.class)
I get lots of errors because Mockito annotations are not being properly handled.
The question is: can I keep the #RunWith(MockitoJUnitRunner.class) annotation and do programmatically the same as #PrepareForTest(MyClass.class) just when I need it for a class?
I have the following simple code. I have a class (TestClass) and I want to test "someMethod". There is an external static method which is called by my "someMethod".
I want to Powermock that static method to return me some dummy object.
I have the #PrepareForTest(ExternalClass.class) in the begining, but when I execute it gives the error:
The class ExternalClass 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.
Please help me to point out what is wrong with the way I have used #PrepareForTest
#RunWith(PowerMockRunner.class)
#PrepareForTest(ExternalClass.class)
public class xyzTest {
#Mock
private RestTemplate restTemplate;
#Mock
private TestClass testClass;
#BeforeClass
private void setUpBeforeClass() {
MockitoAnnotations.initMocks(this);
}
#Test
public void testSuccessCase() {
Boolean mockResponse = true;
ResponseEntity<Boolean> response = new ResponseEntity<Boolean>(mockResponse, HttpStatus.OK);
SomeClass someClass = new SomeClass("test", "1.0.0", "someUrl", "someMetaData");
PowerMockito.mockStatic(ExternalClass.class);
Mockito.when(restTemplate.postForEntity(any(String.class), any(String.class), eq(Boolean.class))).thenReturn(response);
Mockito.when(ExternalClass.getSomeClass(any(String.class))).thenReturn(someClass);
Boolean result = testClass.someMethod("test");
Assert.isTrue(result);
Mockito.verify(restTemplate, times(1)).postForObject(any(String.class), any(String.class), any());
}
}
Make sure you add #RunWith(PowerMockRunner.class) to the top of your class as well.
::edit:: two years later...
Don't ever use PowerMockito, you shouldn't need to.
If you do need to, you have most likely broken the SOLID principles and your design is wrong.
Fix your design instead.
As with the last answer, my problem was also mixing the Test annotation from TestNG instead of Junit Test.
import org.junit.Test; // works
import org.testng.annotations.Test // did not work
Very abstruse error and I spent more than 5 hrs debugging :(
For those trying to get this working with Junit 5, If your using the powermock-module-junit4 beta release which claims to be compatible with 4+, the library will still not recognize:
import org.junit.jupiter.api.Test;
and it will throw a:
org.powermock.api.mockito.ClassNotPreparedException
when #PrepareForTest is applied on the class you want to static mock. If you want to use PowerMock, you will have to go back to Junit 4 or create a MockWrapper for your static method at this time.
PowerMock 2.0: Github Roadmap
While the top-rated answer here is correct without a doubt, this does not answer the question of why is that needed; or, for example, why the same thing would not work with adding #RunWith(MockitoJUnitRunner.class).
The thing is PowerMockRunner uses instrumentation API under the hood, via
javassist library, this allows to alter the classes, like remove final or mock static (non-compile time constants).
In the process of modifying (instrumenting) a certain class, they add an interface to that, called PowerMockModified. It is a marker interface that denotes that a certain byte-code instrumentation took place. Later in the code, they simply check if the class that you use in #PrepareForTest was actually instrumented in some way or not, via such a method:
private boolean isModifiedByPowerMock() {
return PowerMockModified.class.isAssignableFrom(this.type);
}
In turns out that PowerMockRunner does some instrumentation, while MockitoJUnitRunner does not; thus the error you get.
I had the same error, resolved this by adding
#Rule
public PowerMockRule rule = new PowerMockRule();
inside the test class.
If above answers don't work try extends PowerMockTestCase. This trick worked for me.
Example:
public class xyzTest extends PowerMockTestCase
check if import org.junit.Test; package has imported and not that api jupiter one.
I had the same error but resolved it. My problem was that I included powermock-module-junit4 but included my test annotation from TestNG instead of Junit.
I had the same error. I was using TestNG to run the tests. I had to use the following method to fix the above issue.
#ObjectFactory
public IObjectFactory getObjectFactory() {
return new PowerMockObjectFactory();
}
For testNG there are 2 options as follows :
Using ObjectFactory as below:
#ObjectFactory
public IObjectFactory getObjectFactory() {
return new PowerMockObjectFactory();
}
Test class extending extends org.powermock.modules.testng.PowerMockTestCase
My gradle was using Junit 5.
test {
useJUnitPlatform()
}
I was able to debug this. By having breakpoints in PowerMockRunner methods.
It was not invoked. Moreover JUnit 5 is not supported with PowerMockito.
Looks like JUnit5 runs without #ExtendWith.
Make sure you are using powermock2. I had this problem when I was using powermock.
Use
import org.powermock2.api.mockito.PowerMockito;
I know that if I need to use a precise implementation TestExecutionListener, it will prevent the loading of the default TestExecutionListeners.
If my test class is like
#RunWith(SpringJUnit4ClassRunner.class)
#TestExecutionListeners({MyCustomTestExecutionListener.class})
#ContextConfiguration(locations = { "classpath:test-ApplicationContext.xml" })
public class CabinetMembershipServiceImplTest {
...
}
MyCustomTestExecutionListener will be the only loaded Listener and it makes my tests execution fail.
When I launch my tests whitout specifying any TestExecutionListener and I dig in Spring's logs, I can find :
getDefaultTestExecutionListenerClassNames :
Loaded default TestExecutionListener class names from location [META-INF/spring.factories]:
[org.springframework.test.context.web.ServletTestExecutionListener,
org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener,
org.springframework.test.context.support.DependencyInjectionTestExecutionListener,
org.springframework.test.context.support.DirtiesContextTestExecutionListener,
org.springframework.test.context.transaction.TransactionalTestExecutionListener,
org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
So if I want to add my TestExecutionListener, I need to specify (in addition of the wanted implementation) all these default TestExectionListeners on my test class :
#RunWith(SpringJUnit4ClassRunner.class)
#TestExecutionListeners({
ServletTestExecutionListener.class,
DirtiesContextBeforeModesTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class,
MyCustomTestExecutionListener.class})
#ContextConfiguration(locations = { "classpath:test-ApplicationContext.xml" })
public class CabinetMembershipServiceImplTest {
...
}
Is there a way of just adding one (or more) TestExecutionListener, whithout having to explicitly declare each listener from the default configuration nor "overriding" the default ones ?
import org.springframework.test.context.TestExecutionListeners.MergeMode;
#TestExecutionListeners(value = { MyCustomTestExecutionListener.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
Is there a way of just adding one (or more) TestExecutionListener,
whithout having to explicitly declare each listener from the default
configuration nor "overriding" the default ones?
Yes, this is possible since Spring Framework 4.1 and is clearly documented both in the Javadoc for #TestExecutionListeners and in the Merging TestExecutionListeners section of the reference manual. The following example is taken directly from the reference manual.
#ContextConfiguration
#TestExecutionListeners(
listeners = MyCustomTestExecutionListener.class,
mergeMode = MERGE_WITH_DEFAULTS
)
public class MyTest {
// class body...
}
Regards,
Sam (author of the Spring TestContext Framework)
All my unit tests have the same header with Spring annotations:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:spring/spring-master.xml"})
#TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
#Transactional()
I moved them to base class and all my tests now extend it:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:spring/spring-master.xml"})
#TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
#Transactional()
public class DomainObjectBaseTest {
...
public class SomeTest extends DomainObjectBaseTest {
Now when I'm running all tests I'm getting for the DomainObjectBaseTest:
java.lang.Exception: No runnable methods
My question is how can I avoid it? I can remove #RunWith, but in this case, I'll have to apply the annotation to all other tests. Too many extra code,don't like it.
Probably, as an alternative, I can somehow in Spring group annotations, name it, and refere to the group from my tests, please tell me if it is possible. In this case I'll remove base class at all.
And probably there is a way to tell that this class should not be tested. I'm using JUnit4.
Make your base class abstract. The problem is that I believe it is not abstract, so test runner tries to run it and fails for this (IMHO stupid) reason. But it ignores abstract classes.