I want to implements tests in Spring Boot (Spring Boot 1.4.2.RELEASE), so I am checking this example
https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
but not able to find the given method anywhere in this package
import static org.mockito.Mockito.*
On the bottom of the article there is a link to the github repo with the full source of the test. There you can see the following line:
import static org.mockito.BDDMockito.given;
Related
I've written an interceptor to generate service logs for a SpringBoot Java Rest API. I have the code below to define the custom WebMvcConfigurer:
package com.gem.common.interceptors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
#Configuration
public class InterceptorConfig implements WebMvcConfigurer {
#Autowired
LoggerInterceptor logInterceptor;
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor);
}
}
I'd like to use this InterceptorConfig across different modules. Can I package it and use it or do I need to define it in each module?
I suppose with "other modules" you are asking if you could make that code available to other spring boot applications too?
If that's the case - then: yes you can package it in a jar and add it as a dependency to all your other modules. I'll post the way to do this just below, however - just to warn you - if it's just for that simple class, the solution is not going to be worth it.
In short, what you'd need to do is to create your own external artifact (this usually is done via maven or gradle. You create a new maven project for your shared code. It will need to depend on some base libraries so that you have your #Configuration annotation available. Put your class as described in that project, and create a file src/main/resources/META-INF/spring.factories file. There you'll need to point to that class.
Then you build that project and upload the resulting jar to a package repository. Once that's done, you can add it as a dependency to your project. At startup, Spring boot will find the spring.factories file and automatically include the classes that are mentioned there in its initialization.
Please also note, that this is just a high level explanation and you will need more details. Spring has good documentation on this use case and they also have a demo project to show this extension mechanism.
We have a Spring MVC project with multiple Maven modules. We package it into an EAR and deploy it to a WildFly server.
I am trying to do a single time job on project start up. Thus, I thought about the CommandLineRunner interface, the project would compile and run but the commandLineRunner run method wouldn't run.
I guess it's because we are using an MVC Spring project and not a SpringBoot one with its own embedded server.
Can you suggest any ways to implement such a concept in Spring MVC ?
Thanks.
You can do something like that:
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
#Component
public class StartupExec implements {
#EventListener(ContextRefreshedEvent.class)
public void contextRefreshedEvent() {
// do whatever you need here
}
}
This is from this answer.
Now that Alfresco integration tests of custom modules are run using Docker, I wonder how to make additional Spring beans available in this context and how to access existing Spring beans in test classes.
Until Alfresco 5.x, I used to annotate the test class with
#ContextConfiguration("classpath:alfresco/application-context.xml")
This made the Spring context available. To make Spring beans from this context available in the test class, I annotated members like this:
#Autowired
#Qualifier("authenticationComponent")
private AuthenticationComponent authenticationComponent;
In addition I was able to define additional Spring beans in src/test/resources/alfresco/extension/test-context.xml.
Is this the approach to use when writing integration tests for 6.x and Docker?
At least the annotation org.springframework.test.context.ContextConfiguration is no longer included in a module build using the Maven 4.0.0 SDK archetype.
This blog post talks about the above mentioned annotations. But the dependencies pulled in by the pom.xml created from the SDK 4 archetype don't include these annotations.
A different approach seems to be to only use
#RunWith(value = AlfrescoTestRunner.class)
on the integration test class. But how do I get the Spring beans like nodeService injected into it? And how do I declare and make available additional Spring beans which are part of my custom module and required by the integration test to succeed?
You can get the Spring context via AlfrescoTestRunner as follows:
#Before
public void setUp() {
this.nodeService = (NodeService) super.getApplicationContext().getBean("nodeService");
}
I do the same with custom beans:
super.getApplicationContext().getBean(MyType.class);
Since the integration tests run in the repository, all of the Spring context is automatically available.
Note that your test class needs to extend AbstractAlfrescoIT for this to work.
An example class may look like this:
package nl.open.mystuff;
import org.alfresco.rad.test.AbstractAlfrescoIT;
import org.alfresco.rad.test.AlfrescoTestRunner;
import org.alfresco.service.cmr.repository.NodeService;
#RunWith(value = AlfrescoTestRunner.class)
public class MyCustomIT extends AbstractAlfrescoIT {
private NodeService nodeService;
private MyType myType;
#Before
public void setUp() {
this.nodeService = (NodeService) super.getApplicationContext().getBean("NodeService");
this.myType = super.getApplicationContext().getBean(MyType.class);
}
}
In Alfresco SDK 3, you can even add your own Spring XML files under src/test/resources/alfresco/extension/*-context.xml. I imagine this still works, but I haven't tried it with SDK 4 myself.
Now i am learning spring boot,but i find a question,i don't know why this happen.
this is structure of my code, SampleController.java at src/main/java as spring boot reference says spring boot reference. notice "package controller",if there are no package message, i can't run the code, the error message is:
but if i add package message,even this is a wrong package message, the code can run.
so, i want to know this is my mistake or spring boot reference mistake?
You can't just put springboot Main class right into the main/java directory, and even in any java project you're not recommended that way. Put all these stuff in a package instead.
When a class does not include a package declaration, it is considered to be in the “default package”. The use of the “default package” is generally discouraged and should be avoided. It can cause particular problems for Spring Boot applications that use the #ComponentScan, #EntityScan, or #SpringBootApplication annotations, since every class from every jar is read.
I'm trying to use a spring getting started project, and I have a problem I cannot figure out: when I move the restController from the default "hello" package to another one (say com.mydomain.controllers) I get a 404 error page. Any ideas how to solve this?
PS: I'm using intellij + gradle
In this case, this controller is located in a package that's not scanned by Spring. In the Application class:
package hello;
#SpringBootApplication
public class Application {
//...
}
The #SpringApplication annotation is a convenience annotation - see the full explanation in the guide itself.
If you want to scan other locations and customize more your configuration, you can change your application class to this:
package hello;
#Configuration
#EnableAutoConfiguration
#ComponentScan({"hello", "com.mydomain.controllers"})
public class Application {
//...
}