question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable Mockito Tests: An Interface for Context

See original GitHub issue

If Context would be an interface instead of a class, it could easily be mocked in unit tests.

My router delegates certain paths to controllers that have methods which only take a Context object. If this would be an interface, I could mock it and only unit test my controller in isolation.

All the test examples I have found in this repository use full integration test that I would like to avoid (at least for my controller unit tests).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:46 (22 by maintainers)

github_iconTop GitHub Comments

5reactions
tipsycommented, Aug 27, 2018

For people arriving here wondering how to do mocking with Mockito

  • If you’re using Kotlin, switch to mockk
  • If you’re using Java, mock HttpServletRequest and HttpServletResponse, then pass them to ContextUtil.init(). The init function is overloaded to take all the dependencies of Context:
Context ctx = ContextUtil.init(req, res);
Context ctx = ContextUtil.init(req, res, "/:param");
Context ctx = ContextUtil.init(req, res, matchedPath, pathParams);
Context ctx = ContextUtil.init(req, res, matchedPath, pathParams, splats);
Context ctx = ContextUtil.init(req, res, matchedPath, pathParams, splats, handlerType);
3reactions
grotecommented, Aug 27, 2018

Yep, thanks for your work @tipsy!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enable Mockito Tests: An Interface for Context #335 - GitHub
So I'm stuck with various solutions: Add PowerMock to lightweight Mockito2 library in tests (somewhat unsatisfying since we'd only need that for ...
Read more >
unit test a interface implementation with mock in Spring Boot
You are running your tests with SpringRunner but for mocks you don't really need spring context. Try following code // Using mockito runner ......
Read more >
Unit tests with Mockito - Tutorial - Vogella.com
This tutorial introduces the usage of Mockito for JUnit tests. ... A mock object is a dummy implementation for an interface or a...
Read more >
Clean Unit Tests with Mockito - Reflectoring
Mockito allows us to create a suitable test double for the CityRepository interface and lets us define the behavior we expect from it....
Read more >
Injecting Mockito Mocks into Spring Beans - Baeldung
We use the @ActiveProfiles annotation to enable the “test” profile and activate the mock configuration we wrote earlier. As a result, Spring ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found