How to use #MockBean in Spring boot 1.2.5.RELEASE - java

I am trying to write junit test cases for my Spring boot application written in 1.2.5.RELEASE version
I have read the spring boot documentation https://docs.spring.io/spring-boot/docs/1.2.5.RELEASE/reference/htmlsingle/ and trying to write the test case.
Now I want to mock a class which is already a #Primary class in original application.
see the issue here - How to mock a class that been annotated with Primary
On further read I understand that I need to use #MockBean to mock a class which is already a primary in application. But the problem is the spring boot version which I use does not support #MockBean.
I have tried overriding spring-boot-starter-test version alone with 2.1.7.RELEASE(without changing the spring boot parent version as I am not supposed to change the application spring boot version which has many impacts). Now I am able to use #MockBean, but on running the test case, it ends in exception.
java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationUtils.getRepeatableAnnotations
How can I use #MockBean in spring boot 1.2.5.RELEASE?
Is there any workaround without using #MockBean?

Related

How to disable springSecurityFilterChain in test case?

Hi I have a spring project that has spring security to authenticate users using OAUTH provider, and I have to use Spring Cloud Contract to build up a mock server for consumer testing.
repo: https://github.com/Isaacwhyuenac/spring-cloud-contract-poc/blob/main/order-service/src/test/java/com/example/producer/BaseClass.java
When I run ./gradlew clean :order-service:contractTest. The following error is thrown
delegate cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
java.lang.IllegalStateException: delegate cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
If you look at my SecurityConfig
https://github.com/Isaacwhyuenac/spring-cloud-contract-poc/blob/main/order-service/src/main/java/com/example/producer/config/SecurityConfig.java
The security filter is already setup. So, how to resolve this error and have my contractTest running properly??
There are some issues on the BaseClass.
If you are using the latest Spring Boot, use #SpringBootTest is enough. No need the #ExtendedWith
Use a Mock env, set webEnvironment = Mock
If you decided to use RestAssuredMockMvc, you should use this mockMvc instead of the standard MockMVC in your tests.
The Mockito.reset in cleanup is no need at all.
In a WebMvc project, exclude the UserDetailsServiceAutoConfiguration and SecurityAutoConfiguration in your test context, check my example.
In a WebFlux project, just exclude ReactiveUserDetailsServiceAutoConfiguration and ReactiveSecurityAutoConfiguration and ReactiveOAuth2ResourceServerAutoConfiguration(if oauth2 resource server is enabled.) on your testing codes.
#SpringBootTest
#ImportAutoConfiguration(exclude = {ReactiveUserDetailsServiceAutoConfiguration.class, ReactiveSecurityAutoConfiguration.class})
class YourTest{
}
Or test web controller.
#WebFluxTest(controller=YourController.calss, excludeAutoConfigurations = {ReactiveUserDetailsServiceAutoConfiguration.class, ReactiveSecurityAutoConfiguration.class})
class YourTest{
}
I have created a simple Microservice sample several years ago, which used Spring Cloud Contract and Pact to implement the CDC pattern in the API testing and verify, check spring-microservice-sample.

How to disable security in integration tests in Spring Boot 2.2.0?

I need to disable spring-security in my integration test. I'm using Spring boot 2.2.0 and in this version, I didn't find some methods to do it.
#EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, SecurityFilterAutoConfiguration.class}) // not working
#EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class, ManagementSecurityAutoConfiguration.class }) // not working too.
how can I do it?
You and I must have been banging our heads against the same thing at the same time it seems.
I was able to make #AutoConfigureMockMvc(secure=false) do exactly what I wanted--disable all security for my unit tests. However, it was immediately marked deprecated in my IDE because I was using Spring Boot 2.1.6. I don't like using deprecated things so I tried and tried to get the exclude attribute to work, but it never did.
I was about to give up on it and suffer with the deprecation warning because at least my unit tests worked with it.
On a whim, I tried upgrading Spring Boot in my project from 2.1.6 to 2.2.2. To my horror, the secure attribute was no longer deprecated--it was removed altogether! I gave the unit test a spin with the attribute gone, but with the exclude attribute populated exactly as you show in your question and it worked!
TL;DR? Upgrade to Spring Boot 2.2.2
you could play this game via profile
#ActiveProfiles(value = "IntegrationTest")
public class myTest {

Spring Boot Unit testing for Mock beans vs Mockito

I am using a spring boot 1.5.4 project where I need a clarification on testing package.
I am seeing Spring Boot comes with #MockBean which is part of org.springframework.boot.test.mock.mockito package.
My question is - Is it similar to Mockito external dependency and I can avoid using Mockito as external dependency as spring boot mock.* package has all features which Mockito has?
As already pointed in Greg's answer , mockito core is already included in spring-boot-starter-test so its already there in your project, no external dependency is needed.
Having said that - mockito's #Mock and Spring's #MockBean are a bit different in a way that #MockBean is basically a #Mock plus #Bean so it does what mockito annotation does plus it works with like a bean too i.e. it places instances in Spring's application context and all other spring bean related concepts come into picture too.
From documentation -
Annotation that can be used to add mocks to a Spring
ApplicationContext. Can be used as a class level annotation or on
fields in either #Configuration classes, or test classes that are
#RunWith the SpringRunner.
Mocks can be registered by type or by bean name. Any existing single
bean of the same type defined in the context will be replaced by the
mock, if no existing bean is defined a new one will be added.
When #MockBean is used on a field, as well as being registered in the
application context, the mock will also be injected into the field.
For non - Spring applications where DI & context are not there, you shoudl use #Mock and for Spring apps , you should use #MockBean.
If you look at the dependencies for spring-boot-starter-test, you'll see that it depends on the Mockito library so it is included for you.

Spring autowire anotation in jmeter

I am trying to run some performance test cases on an spring application using jmeter.
I know that in jmeter, spring annotation would not work, like for example
#ContextConfiguration and #Autowired
which I over came by getting the ApplicationContext and then getting the Bean.
But, is there any other work around because I have a very big Base class which has lot of
#Autowired instance variables and lot of dependencies.
Thanks in Advance.
I'm not familiar with JMeter but I think you can treat your JMeter test cases as a standalone Spring application in which you load the ApplicationContext once then all your Spring Beans will be available.
See this tread: Using Spring 3 autowire in a standalone Java application
Hope this helps.

Good Way to Test Spring Services With Method Level Annotaions

I have a Spring service I want to test that uses the #Validated annotation on one of its method parameters. I would like to test it in the Spring container, but I am not sure if that is the best way. If testing in the container is the best solution for my situation, I would like to know how to run it in the container without loading my complete configuration. Any thoughts?
I ended up using the Spring Test Framework. I am using the standalone and the web application context of the mock MVC to test my annotations.
Spring 3.2.4 Test Framework
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html
Configuration Example
http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers-configuration/

Categories