Comments (7)
Hi
Are you sure its related to springmock?
I've just hacked something like this:
@Repository
public class DemoRepository {
public String message() {
return "hello";
}
}
@Service
public class DemoService {
public final DemoRepository demoRepository;
public DemoService(DemoRepository demoRepository) {
this.demoRepository = demoRepository;
}
public String getMessage() {
return demoRepository.message();
}
}
@RestController
@RequestMapping("/hello")
public class DemoController {
private final DemoService demoService;
public DemoController(DemoService demoService) {
this.demoService = demoService;
}
@RequestMapping
public DemoMessage message() {
return new DemoMessage(demoService.getMessage());
}
public static class DemoMessage {
private final String message;
public DemoMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
}
and the test:
@WebMvcTest(DemoController)
class DemoControllerTest extends Specification {
@Autowired
MockMvc mockMvc
@AutowiredMock
DemoService demoService
def "should work 1"() {
given:
demoService.getMessage() >> "mock1"
when:
final request = mockMvc.perform(MockMvcRequestBuilders.get("/hello"))
then:
request
.andExpect(status().isOk())
.andExpect(jsonPath('$.message', is("mock1")))
}
def "should work 2"() {
given:
demoService.getMessage() >> "mock2"
when:
final request = mockMvc.perform(MockMvcRequestBuilders.get("/hello"))
then:
request
.andExpect(status().isOk())
.andExpect(jsonPath('$.message', is("mock2")))
}
}
and it works for me :)
dependencies I've used:
compile('org.springframework.boot:spring-boot-starter-web')
testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.12'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'org.spockframework', name: 'spock-core', version: '1.1-groovy-2.4'
testCompile group: 'org.spockframework', name: 'spock-spring', version: '1.1-groovy-2.4'
testCompile group: 'com.pchudzik.springmock', name: 'springmock-spock', version: '1.0.0'
are you using sprock 1.1 and spring-boot 1.4.4 spring 4.3.6?
Can you provide failing test case? If not maybe you'll find few minutes to debug and investigate what's going on inside com.pchudzik.springmock.spock.spring.MockAttachingTestExecutionListener.tryToGetBean (try to evaluate applicationContext.getBean(beanName) is it returning null? can you tell why?)
Maybe you have both junit and spock tests?
from springmock.
Hmm, strange. I will look at this again next week and let you know about details.
from springmock.
I can confirm that it is working on fresh (simplest) spring-boot project.
After debugging I've noticed test is failing when trying to obtain bean instance with name "privilegeEvaluator" which is related to Spring Security. After disabling security in test:
@WebMvcTest(controllers = DemoController, secure = false)
class DemoControllerTest extends Specification {
everything works properly.
from springmock.
Leaving it open then. Will investigate what's going on.
Does your service by any chance depends on something related to security (some kind of principal, or something which might trigger spring security?) Or in general depends on something?
I'm asking because maybe for some reason spring is trying to populate dependencies. Dependency processing should be ignored (SkipSpockBeansPostProcessing) but who knows maybe custom order should be added to this post processor or something. If you feel like it you can debug it and see what's going on and if SkipSpockBeansPostProcessing is called during getBean execution :)
Just out of curiosity can you check if @MockBean from springBoot works? I'm guessing it does :)
from springmock.
I've just checked it works just fine with spring security - https://github.com/pchudzik/springmock-spock-with-security-test but I might be missing something there.
@tlenczyk could you please check what you are doing differently in your project so that's not working. If you have time I'd really appreciate PR to springmock-spock-with-security-test which break it :)
For what exactly you are using privilegeEvaluator?
Do you have your own implementation of this class?
Do you have custom implementation of PermissionEvaluator?
Have you checked is @MockBean is working (you can replace AutowiredMock with MockBean and use mockito to mock stuff)?
from springmock.
One more idea to check. Instead of walking whole context hierarchy take advantage of doubleregistry and try to use it to find doubles which are registered com.pchudzik.springmock.spock.spring.MockAttachingTestExecutionListener#beforeTestMethod
Don't walk whole context and fetch all definitions. Search for definitions with names already defined in doubleregistry. It should be enough (check if and how FactoryBeans are registered in the double registry).
When scanning only doubles which are registered and known to be mocks then early beans initialization might be avoided (NPE might be avoided if I will not touch security infrastructure while attaching mocks)
from springmock.
@tlenczyk please check latest springmock snapshot version. Since I wasn't able to reproduce the issue I'be been guessing what might be causing this issue but I think I might have come up with the solution to avoid it in the first place :)
Please verify and if it is still failing then reopen this.
from springmock.
Related Issues (17)
- Figure out unified testing infrastructure/interfaces to ensure that mocking library works HOT 1
- Add support for different groovy versions when using spock HOT 1
- allow to create mocks without injecting them into specification HOT 2
- add support for additional configuration HOT 1
- support for java7 HOT 1
- configure automated deployment HOT 1
- spy should be distinguished from mock if they are both of the same class HOT 3
- inject mocks by name HOT 1
- Determine beanName just like what `@MockBean` do HOT 4
- Non-spock tests failing with IllegalArgumentException HOT 3
- mockito spies are not resetted HOT 1
- Check if it's working with spring5 HOT 1
- check if it works with spring data repositories HOT 1
- Null Pointer exception when names don't match up HOT 2
- Allow to leave spring proxy as it is when creating spies HOT 2
- Support new default value of spring boot 2.1 of spring.main.allow-bean-definition-overriding HOT 16
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from springmock.