I've used a separated bean configuration file for my beans instead of using spring-boot annotation like("component", "Service" or, ...) in the application:
#SpringBootTest
#ContextConfiguration(loader = AnnotationConfigContextLoader.class)
class CheckActiveWorkerTest {
#Autowired
UserJpaRepository userJpaRepository;
#Test
void execute_AddUser_exception() {
userJpaRepository.get(....);
.....
}
}
The UserJpaRepository is an interface and it has #Repository.
When I run this test, the userJpaRepository will be null and the spring boot test runner could not find its bean. Eventually, this test raises a NullPointerException!
How can I use the JpaRepositories during the tests?
Related
Consider the following basic Spring Boot application:
#SpringBootApplication
#ComponentScan(basePackages = "webmvctestproblem.foo")
public class Application {
public static void main(String[] args) {
run(Application.class, args);
}
}
It contains only two other beans. One controller:
#RestController
class Greeter {
#GetMapping("/")
String greet() {
return "Hello, world!";
}
}
And one configuration in webmvctestproblem.foo containing a DataSource dependency:
#Configuration
class Bar {
#Autowired
private DataSource dataSource;
}
Running the application normally (through gradlew bootrun, e.g.) succeeds. Thus, confirming that the app is configured correctly under normal conditions.
However, running the following test causes a runtime error because Spring still attempts to resolve the data source bean dependency on the configuration class:
#RunWith(SpringRunner.class)
#WebMvcTest
public class GreeterTest {
#Test
public void test() throws Exception {
}
}
Of course, there isn't one to resolve because the test is a #WebMvcTest that is designed to create only MVC-related beans.
How do I get rid of the error? I have already tried excluding the configuration class using the excludeFilters attribute of the existing #WebMvcTest annotation and a new #ComponentScan annotation on the test itself. I don't want to resort to turning it into an integration test with #SpringBootTest.
(The project is also available on GitHub for convenience.)
If the DataSource is not mandatory for the test run, simply mock the DataSource with #MockBean in the test class.
#RunWith(SpringRunner.class)
#WebMvcTest
public class GreeterTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private DataSource dataSource;
#Test
public void shouldGreet() throws Exception {
mockMvc
.perform(get("/"))
.andExpect(content().string("Hello, world!"));
}
}
Spring will automatically create a Mock for DataSource and inject it into the running test application context.
Based on your source code it works.
(Btw: Your source code has a minor issue. The Greeter controller class is in the base package but the component scan only scans on the "foo" package. So there will be no Greeter controller on the test run if this isn't fixed.)
#WebMvcTest creates a "slice" of all the beans relevant to WebMvc Testing (Controllers, Json conversion related stuff and so forth).
You can examine the defaults in org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTypeExcludeFilter
In order to find which beans are actually supposed to be Run Spring must resolve them somehow, right?
So spring test tries to understand what should be loaded and what not by passing through these filters.
Now, if you mark anything with #Configuration spring "knows" that this is the place where the place should be found. So it will load the configuration and then will check which beans defined in this configuration must actually be loaded. However the object of configuration itself must be loaded anyway.
Now the process of loading the configuration object includes injecting stuff into these configurations - this is lifecycle of object creation of spring.
And this is a source of mistake here:
#Configuration
class Bar {
#Autowired
private DataSource dataSource;
}
Spring loads Bar and tries as a part of loading this object to autowire the data source. This fails since the DataSource Bean itself is excluded by filters.
Now in terms of solution:
First of all, why do you need this DataSource to be autowired in the Configuration object? Probably you have the bean that uses it, lets call it "MyDao", otherwise I don't see a point of such a construction, since #Configuration-s are basically a place to define bean and you shouldn't put business logic there (if you do - ask a separate question and me/our colleagues will try to help and suggest better implementation).
So I assume you have something like this:
public class MyDao {
private final DataSource dataSource;
public MyDao(DataSource dataSource) {
this.dataSource = dataSource;
}
}
#Configuration
class Bar {
#Autowired
private DataSource dataSource;
#Bean
public MyDao myDao() {
return new MyDao(dataSource);
}
}
In this case however you can rewrite the configuration in a different way:
#Configuration
class Bar {
// Note, that now there is no autowired datasource and I inject the parameter in the bean instead - so that the DataSource will be required only if Spring will have to create that MyDao bean (which it won't obviously)
#Bean
public MyDao myDao(DataSource dataSource) {
return new MyDao(dataSource);
}
}
Now the Bar object will still be created - as I've explained above, but it beans including MyDao of course won't be created, problem solved!
The solution with #Autowired(required=false) provided by #Anish B. should also work - spring will attempt to autowire but won't fail because the data source is unavailable, however you should think whether its an appropriate way to deal with this issue, your decision...
Before you can #Autowire the DataSource bean you need to define the DataSource in some config class or in the properties file. Something like this
spring.datasource.url = jdbc:mysql://localhost/abc
spring.datasource.name=testme
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.jpa.database=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
Or
#Configuration
public class JpaConfig {
#Bean
public DataSource getDataSource()
{
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:file:C:/temp/test");
dataSourceBuilder.username("sa");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
You should use
#AutoConfigureMockMvc
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#RunWith(SpringRunner.class)
on your test class, then you can inject
#Autowired
private MockMvc mockMvc;
and use it in test
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andReturn();
i suggest you read the documentation about testing. You can test a spring boot application in 100s of different ways.
WebMvcTest
as suggested by the documentation try, defining what controller class that you want to test in the #WebMvcTest annotation.
#RunWith(SpringRunner.class)
#WebMvcTest(Greeter.class)
public class GreeterTest {
#Autowired
private MockMvc mockMvc;
#Test
public void shouldGreet() throws Exception {
mockMvc
.perform(get("/"))
.andExpect(content().string("Hello, world!"));
}
}
#RunWith(SpringRunner.class)
#SpringBootTest(classes=MyApplication.class)
#TestPropertySource(locations = "classpath:test-application.properties")
#WebAppConfiguration
#RestClientTest(Controller.class)
public class MyIntegrationTest {
}
when I run this I get the following error
java.lang.IllegalStateException: Configuration error: found multiple declarations of #BootstrapWith for test class
The root cause is you are using both #SpringBootTest and #RestClientTest together.
Because it causes the conflict about #BootstrapWith annotation. You can see the image below.
Tip: If you are using JUnit 4, don’t forget to also add
#RunWith(SpringRunner.class) to your test, otherwise the annotations
will be ignored. If you are using JUnit 5, there’s no need to add the
equivalent #ExtendWith(SpringExtension.class) as #SpringBootTest and
the other #…Test annotations are already annotated with it.
Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing
If your test case is simple, you can refer to the example using #RestClientTest here.
In my case, I define some complicated configurations related to the tested service (Ex: jpaAuditingHandler, jpaMappingContext, ...). That's why I use #SpringBootTest to auto-configure.
This is my code sample:
#SpringBootTest
class MyTest {
#Autowired
private MyProperties myProperties;
#Autowired
private MyService myService;
private MockRestServiceServer server;
#Autowired
#Qualifier("restTemplateBean1")
private RestTemplate restTemplate;
#BeforeEach
void setUp() {
server = MockRestServiceServer.createServer(restTemplate);
}
#Test
void testCallRestServiceSuccess() throws Exception {
this.server.expect(requestTo(myProperties.getUrl())).andRespond(withStatus(HttpStatus.OK));
boolean result = myService.callRestService();
assertThat(result).isTrue();
}
}
Some other references:
MockRestResponseCreators javadoc: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/web/client/response/MockRestResponseCreators.html
https://www.baeldung.com/restclienttest-in-spring-boot
It looks like you have at least one too many Spring test annotations on your test.
What exactly do you want to test? If it is, indeed, just a RestClientTest, then this should work:
#RunWith(SpringRunner.class)
#TestPropertySource(locations = "classpath:test-application.properties")
#RestClientTest(YourRestClient.class)
public class MyIntegrationTest {
}
Or, go with just #SpringBootTest, but I don't know exactly what you want to do here.
I got a multi module project where not every module is actually an application but a lot of them are libs. Those libs are doing the major work and I want to test them where they are implemented. The current dependencies of the libs:
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
In the main source is a class with #Configuration and a single bean:
#Bean public String testString() { return "A Test String"; }
I got 2 test classes:
#RunWith(SpringRunner.class)
#SpringBootTest
#ActiveProfiles({"default", "test"})
public class Test1 {
#Test
public void conextLoaded() {
}
}
-
#RunWith(SpringRunner.class)
#SpringBootTest
#ActiveProfiles({"default", "test"})
public class Test2 {
#Autowired
private String testString;
#Test
public void conextLoaded() {
}
}
The first test works. The second does not. There is not #SpringBootApplication anywhere in that project so in the same package as the Tests I added a test configuration:
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan("com.to.config")
public class LibTestConfiguration {
}
And it does not work. Same for classes that are #Service. They are not in the context. How can I make it behave like a normal Spring boot application without it actually beeing one and load the configs and contexts from the configurations files I need? The default and test profile share most of their properties (for now) and I want them to be loaded like I would start a tomcat.
I switched to JUnit 5 and made it kinda work... So if you want to test Database stuff:
#DataMongoTest
#ExtendWith(SpringExtension.class)
#ActiveProfiles({"default", "test"})
class BasicMongoTest { ... }
Lets you autowire all repositories and mongo template
Initializes with apllicaton.yml config
Does NOT initialize or configure interceptors
Full application context test if you have a class with #SpringBootApplication in your classpath (Can be an empty test main in your test context)
#SpringBootTest
#ExtendWith(SpringExtension.class)
#ActiveProfiles({"default", "test"})
public class FullContextTest { ... }
Initializes the full context with all configs and beans
Should not be done if not necessary as it loads all the application context and kinda defeats the purpose of unit tests to only activate whats needed.
Test only specific components and configs:
#SpringBootTest(classes = {Config1.class, Component1.class})
#EnableConfigurationProperties
#ExtendWith(SpringExtension.class)
#ActiveProfiles({"default", "test"})
public class SpecificComponentsTest { ... }
Initializes the context with only the Config1 and Component1 classes. Component1 and all beans in Config1 can be autowired.
Trying to #Autowired a RestTemplate in Spring Boot 2.1.4 using RestTemplateBuilder.
When i run junit tests i get an error when trying to autowired RestTemplate.
I saw here: How to autowire RestTemplate using annotations
It seems that RestTemplateBuilder is better so i would like to use that.
This is the configuration file :
#Configuration
public class Beans {
#Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
This is the test class:
#RunWith(SpringRunner.class)
#SpringBootTest
#ContextConfiguration(classes = Beans.class)
public class AppTest extends TestCase {
#Autowired
private RestTemplate restTemplate;
}
The error is :
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method restTemplate in beanDeclerations.Beans required a bean of type 'org.springframework.boot.web.client.RestTemplateBuilder' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.boot.web.client.RestTemplateBuilder' in your configuration.
I edited out other autowired that work.
What am i missing here?
After searching the web i found out that spring auto wired RestTemplateBuilder, why isn't doing so here?
EDIT:
I ended up using #RestClientTest() and had to move RestTemplateBuilder Bean to the main class for now, I'll move it later to a different place.
Thanks for the help.
The RestTemplateBuilder should be available through an autocofiguration (see: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.java). I think this config is missing because of your #ContextConfiguration. You have some possibilities. Try to add the AutoConfig for the RestTemplateBuilder to your ContextConfiguration. Second one is to make a TestConfiguration and create your own RestTemplateBuilder or directly a RestTemplate. The third one is don't inject the RestTemplate - build it by hand in your test. You can also remove the #ContextConfiguration-Annotation but this will result in a test which loads every config you have defined in your project.
There's also a RestTestTemplate (https://www.baeldung.com/spring-boot-testresttemplate) which is designed for tests.
#RunWith(SpringRunner.class)
#SpringBootTest
#ContextConfiguration(classes = {TestConfig.class, RestTemplateAutoConfiguration.class})
public class SandboxApplicationTests {
#Autowired
RestTemplate restTemplate;
#Test
public void contextLoads() {
}
}
The snippet above works for me. Without the RestTemplateAutoConfiguration in the ContextConfiguration the RestTemplate can't be created because of the missing RestTemplateBuilder-Bean
I had the same issue (in an Spring Boot 2.5.5 application).
To solve it I had to add #AutoConfigureWebClient on my test class.
So my test looks like this :
#AutoConfigureWebClient
#WebMvcTest(controllers = TeamController.class)
public class TeamControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private FooService fooService;
#Test
public void findAllFoos() throws Exception {
mockMvc.perform(get("/foos"))
.andExpect(status().isOk());
}
}
But I read somewhere it may be considered as a bug that should be fixed by Spring Team (maybe will this annotation be directly added in #WebMvcTest ?).
I think this way: the test starts and stops faster than when you use #SpringBootTest (because that last one makes your application start entirely).
For more info : https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureWebClient.html
I am using Spring's #Transactional in my DAO's service class. The application has beans configured in XML(s) and via annotations like #Service.
My test case configuration looks like below:
#RunWith(SpringJunit4ClassRunner.class)
#ContextHierarchy({
#ContextConfiguration(locations = {"classpath:spring/spring.xml"}),
#ContextConfiguration(classes = {Service.class})
})
#ComponentScan(...)
public class TestRunner {
#Autowired
private Service service;
#Test
public void testSave() {
service.save(...);
}
}
I have below configured in XML:
<tx:annotation-driven transaction-manager="txmManager"/>
<!-- bean for hibernate5 transaction manager using sessionfactory -->
The service class looks as below:
#Service
public class Service {
#Autowired
private DAO dao;
#Transactional
public Entity save(Entity entity) {
dao.save(entity);
}
}
Now it all works perfectly during source context run but when I run the test cases, the transaction is never invoked. Note: there is no NPE on calling Service.class's method.
I tweaked around and noticed that if I create a bean for Service.class in my (test) spring xml file, the test cases work as expected, i.e., the transaction manager is called and the in-memory database is updated.
I expected SpringJunit4ClassRunner to create a MergedContext and the transaction-manager configuration to be automatically kicked in upon Service#save call.
Any thoughts on what am I missing here?
Hibernate - 5.2.6.Final,
Spring - 4.2.0.RELEASE,
Spring-test - 4.1.6.RELEASE