Initially I was using only Mockito in junits so I was using SpringJUnit4ClassRunner.class in #RunWith annotation ie
#RunWith(SpringJUnit4ClassRunner.class)
due to which spring dependency injection was working fine and was getting a bean through
#Autowired
Someservice someservice ;
But now, I have also integrated PowerMock in it.
So as per doc , I have replaced class mentioned in #RunWith annotation with
#RunWith(PowerMockRunner.class)
but now, someservice is coming out to be null. Is there a way to use both SpringJUnit4ClassRunner.class and PowerMockRunner.class in #RunWith annotation
I know this thread is old, but it's good to add that since 2014 and this pull request, you can use the #PowerMockRunnerDelegate annotation to "delegate" the run context to SpringJUnit4ClassRunner (or any other runner really).
Above code would look like :
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
#PrepareForTest(X.class);
public class MyTest {
// Tests goes here
...
}
With this annotation, you don't need the PowerMock rule anymore !
You have to use the PowerMockRule.
#RunWith(SpringJUnit4ClassRunner.class)
#PrepareForTest(X.class)
public class MyTest {
#Rule
public PowerMockRule rule = new PowerMockRule();
// Tests goes here
...
}
For a full example of the Spring Integration Test with PowerMock and Mockito, you could checkout this maven project.
svn co http://powermock.googlecode.com/svn/tags/powermock-1.4.12/examples/spring-mockito/
cd spring-mockito/
Look at the dependecies to powermock.
less pom.xml
and then run the test
mvn test
and you should get the following test results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
Related
I am trying to executes some tests using JUnit 5 Test Suite. Based on the documentation I found, I used #RunWith and #SelectPackages (from the dependency junit-platform-runner).
Before I started the tests defined in the #SelectPackages, I wanted to load some properties file in such a setup method. I tried using #BeforeAll, #ClassRule and #Rule, but those setup methods are only called in any test methods in the annotated class (RemoteTests). But the setup methods are not called before the executions of the test classes defined in the #SelectPackages (com.test.packages).
#RunWith(JUnitPlatform.class)
#SelectPackages("com.test.packages")
public class RemoteTests {
#Rule // I tried also #ClassRule and #BeforeAll here
public void setup() {
PropertiesLoader.loadProperties("remote.properties");
}
}
Are there any setup annotations for my purpose?
I'm still new to Spring and have looked at similar questions on stack but couldn't identify an answer from what I've read.
I'm receiving an NPE when I call my applicationContext variable, which means the bean must not have been created or injected correctly. Here is the relevant portion of my program.
#SpringBootTest
#ContextConfiguration(classes = TestConfig.class)
public class SampleIT {
#Autowired
private ApplicationContext applicationContext;
#Test
public void sampleMethod() throws Exception {
//NPE below
String[] allBeanNames = applicationContext.getBeanDefinitionNames();
//more logic
I'm trying to get the ApplicationContext instance to debug why other beans are null from my config, so this all must be because of a basic flaw in my understanding of how ApplicationContext is setup and beans from it are injected. Any help is very much appreciated!
Try to add the following annotation to your class SampleIT:
#RunWith(SpringRunner.class)
Answering my own question now. Importing org.junit.jupiter.api.Test instead of org.junit.Test will cause Junit5 to be used. This will allow Junit to work with Spring without the #RunWith(SpringRunner.class) annotation. There are still remnants of Junit4 left in the codebase I'm working in, so this is a quick fix before completely moving to Junit 5.
If you are using Junit in version 4, You have to use #RunWith(SpringRunner.class),
If you are using Junit version 5, You have to use #ExtendWith(SpringExtension.class).
Additionally if you are using Mockito You can use #ExtendWith(MockitoExtension.class) etc.
I highly recommend read documentation about #SpringBootTest annotation in this link
#SpringBootTest
New to spring. I am trying to upgrade spring-boot-starter-parent from v1.3.2 to the latest v2.1.4. Made the POM file change for version upgrade. I have made all the necessary code changes to solve the compilation problems. when I am trying to run the test cases though, Test Classes which have #SpringBootTest annotation are failing with java.lang.IllegalStateException: Failed to load ApplicationContext
I have my application properties(application.properties) file in src/main/resources and the test application.properties file in src/text/resources
I have tried various annotations like
#ContextConfiguration()
#TestPropertySource(locations = "classpath:application.properties")
Still received the same error.
Below is the sample test class created.
package xxx.xx.xx.xxxx.controller
import org.junit.Assert;
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(classes = App.class)
#WebAppConfiguration
public class SampleTest {
//sample test case created to figure out
//test case runs successful when #SpringBootTest is not present
#Test
public void sampleTest1() {
int x= 2+3;
Assert.assertEquals(5, x);
}
}
I expect to able to run the test cases with that annotation.
I want to create integration test with docker before cucumber test start. Inspired by: http://tech.asimio.net/2016/08/04/Integration-Testing-using-Spring-Boot-Postgres-and-Docker.html
But in my case the TestExecutionListener is not started before database initialization. I use Flyway for database migrations and it seems it tries to initialize first. For this a database connection required, which is not available, due the TestExecutionListener not being executed.
These are my classes:
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"json:target/integration-cucumber.json"},
features = "src/test/resources"
)
public class CucumberIntegration {
}
And my test class which I will be extended by the cucumber steps:
#RunWith(SpringRunner.class)
#SpringBootTest(classes = Application.class)
#SpringBootConfiguration
#ContextConfiguration
#TestPropertySource(locations="classpath:application.properties")
#TestExecutionListeners({
DockerizedTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class
})
public class SpringIntegrationTest {
}
When I change #SpringBootTest(classes = Application.class) to #SpringBootTest I see DockerizedTestExecutionListener being executed. Only it fails because it misses configuration from Application.class.
Anyone idea how to this with or without TestExecutionListener?
This will not work because the Cucumber.class JUnit runner doesn't support TestExecutionListeners.
You might think that your test steps are executed by the SpringRunner.class JUnit runner, but they aren't.
The #RunWith(SpringRunner.class) annotation on your SpringIntegrationTest class is basically useless here, since according to your post, your subclasses of SpringIntegrationTest contain Cucumber steps. However the SpringRunner expects JUnit #Test methods. Cucumber steps with #Given, #When, #Then etc. are ignored because SpringRunner doesn't know what to do with them.
What really executes your test steps is the #RunWith(Cucumber.class) annotation in your CucumberIntegration class. The Cucumber runner, however, doesn't give a damn about the TestExecutionListeners annotation that your steps definition class inherits from SpringIntegrationTest because it doesn't support such a feature.
Execution listeners are a Spring-only feature. You won't be able to do what you want to do, at least not as long as you use Cucumber.
I have a maven spring project (latest version) and I want to write some junit tests (latest version).
The issue I have is that my spring beans are autowired, and when I call them from junit test, I get null pointer exceptions, as spring doesn't autowire them.
How can I load the context so that things are autowired?
Have you studied Testing chapter in Spring reference documentation? Here is an example you should start with:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
public class MyTest {
#Resource
private FooService fooService;
// class body...
}
If you are in com.example.MyTest in /src/test/java, you will need /src/test/resources/com/example/MyTest-context.xml - but the exceptions will show you the way.
This is a possibility:
#RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "/applicationContext.xml"
// in the root of the classpath
#ContextConfiguration({"/applicationContext.xml"})
public class MyTest {
// class body...
}
Usually it's a good idea though to have a test-applicationContext for your test infrastructure.
You should use the SpringJUnit4ClassRunner on your test classes, and use #Resource (or #Autowired) on the field in your test class that contains the bean. You should consider having a special test context Spring configuration that uses stubs so that your tests are genuine unit tests, and don't rely on the whole application context.