Kenneth A. Kousen

gunslinger & author
+ Follow
since Sep 18, 2002
Merit badge: grant badges
Biography
I am a Java Champion, Oracle Groundbreaker Ambassador, and a Grails Rock Star. I am the author of the O'Reilly books "Kotlin Cookbook", "Modern Java Recipes", and "Gradle Recipes for Android", and the Manning book "Making Java Groovy", and have recorded over a dozen video courses for the O'Reilly Learning Platform covering topics related to Android, Spring, Java, Groovy, Grails, and Gradle. I am also a regular speaker on the No Fluff, Just Stuff conference tour.

I won the JavaOne Rockstar award three times. My academic background include BS degrees in Mechanical Engineering and Mathematics from M.I.T., an MA and Ph.D. in Aerospace Engineering from Princeton, and an MS in Computer Science from R.P.I. I am currently President of Kousen IT, Inc., based in Connecticut.

Blog: https://kousenit.org
Newsletter, Tales from the jar side: https://kenkousen.substack.com
For More
Marlborough, CT
Cows and Likes
Cows
Total received
16
In last 30 days
0
Total given
0
Likes
Total received
35
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kenneth A. Kousen

There's a big difference between testing a private method and mocking a private method. To test a private method, you use regular Java reflection along with a tool like JUnit. You don't need Mockito for that.

If you want to mock a private method, the question becomes, why? A mock stands in for a dependency of the class you're actually testing, and that class shouldn't be calling private methods in its dependencies at all. It shouldn't even be aware that the dependencies have private methods, by definition.

So if you want to test a private method, that's fine, though risky, because the whole point of making a method private is that its implementation can change at any time without warning. Your test may work, but it will be fragile and may be forcing the developer into a specific implementation.

Mocking a private method, however, is not currently supported and not likely to be in the future for the reasons discussed above.
1 year ago
For the record, Mockito can mock static methods now. It still doesn't mock anything private, because of the basic philosophical objection that the class you're testing shouldn't even be aware that the dependency it needs has private methods. If it does, it certainly shouldn't be calling them.

Over the years, Mockito has added many features it didn't originally agree with, like mocking constructors, static methods, and final methods and classes. I don't see it ever mocking private methods.

I'm still not sure how to answer a question that comes down to, how can I remove these tests that I don't agree with in my code? That's not really the job of an API. That's talking to the other developers about your objections and making a plan as a team on how to handle them.
1 year ago
I think by PowerMockito you actually mean PowerMock, which is an alternative mocking and stubbing framework unrelated to Mockito. If you're asking how to remove mocks and stubs from your code base entirely, I think you're missing the benefits of using a framework to autogenerate fake objects in useful situations, several of which I covered in the two videos I linked to in another recent post.

My goal is more to show people how to use Mockito effectively.

Nevertheless, I wish you good luck in your testing.
1 year ago
I've added another short Mockito video to my YouTube channel, Tales from the jar side, https://www.youtube.com/@talesfromthejarside

How Mockito Mocks Help, at https://youtu.be/McSfk595N1I

There is also another video available:

Why Use Mockito At All?, at https://youtu.be/IRYYL61-GNk

I hope you find them useful,

Ken
1 year ago
I added a pom.xml file to the GitHub repository (https://github.com/kousen/mockitobook), along with a Maven wrapper (mvnw and mvnw.cmd scripts).
1 year ago
I use Spring (and Spring Boot, and Spring Data, ...) all the time as well. One of the nice testing features of Spring is that it includes Mockito as a dependency whenever you make a Spring Boot app, so the framework is already there.

To support Mockito, Spring provides an annotation called . That tells the framework to create a mock of the annotated class and replace any instance in the application context with that mock. That makes your mock available for dependency injection.

One of the tasks involved when you use Mockito is that you need a way to plug your mocks into the class you're testing. In non-Spring apps, you usually provide a constructor argument or a setter for that purpose. With Spring, dependencies are "autowired" in, so that approach doesn't work. That's why Spring created their own annotation.

You can see an example of this in the Spring Boot documentation in this section: https://docs.spring.io/spring-boot/docs/3.0.2/reference/htmlsingle/#features.testing.spring-boot-applications.mocking-beans on Mocking and Spying Beans.

The rest of the concepts in Mockito (what are mocks, stubs, and spies; how do you use them; how do you set expectations, and so on) all work the same way in Spring that they do in non-Spring apps. My book covers those, and while it includes a section on Spring (specifically, Working with the Spring Framework in the chapter called Use Mockito in Special Cases), I mostly focus on the general concepts which work the same everywhere.
1 year ago
Got it. Makes sense. A side benefit may be that an additional reply gives you another entry in the book raffle. If that's true, feel free to reply to this reply. :⁠-⁠)
1 year ago
Hi R.J. --> Thanks for reading the newsletter. You're right, Mockito doesn't include anything specific for asynchronous coding, as you noted. I also use the StepVerifier when I'm using Project Reactor, almost always as part of a Spring app.

Still, Mockito can mock your dependencies, so if your reactive controller depends on a repository, for example, you can still mock that if you need to. For example, if you're testing a controller that returns a returned by a reactive repository, you can still ask Mockito to return one of those using the method to create one.

Hi Marco --> Mockito complements JUnit. Mockito tests are part of JUnit tests, though technically Mockito can work with other testing frameworks if you like. The vast majority are built on JUnit. The JUnit part sets up the class and methods you are testing and expresses the expected results as assertions. If the class you're testing has dependencies of its own, what Mockito can do is generate fake objects to represent those that return the desired values when called. You don't test the fakes. You test a class that depends on them.

If instead of using Mockito you use the real dependencies, that's fine. That's called an integration test (rather than a unit test), and it's a necessary part of making sure the system is working properly. I often think of it this way: if you drive a car to some destination, that's an integration test, or even a full end-to-end, or functional, test. If you get there and back, the test succeeded and you're good. But if something goes wrong, you take the car to a mechanic, and their job is to isolate the problem down to its source. Mockito helps you isolate one component from another, so you can diagnose where the real problems reside.

Hope that helps,

Ken
1 year ago
I don't have a Maven pom in the repository, but the coordinates for the dependencies are the same. I'll probably add a pom now that you mention it. :⁠-⁠)

Hope you like the book.
1 year ago
Hi all,

I'm glad to be here this week talking about my new book, Mockito Made Clear. For those who aren't aware of the book, let me give you a few details about it:

- The home page for the book is at https://pragprog.com/titles/mockito/mockito-made-clear/
- The book is part of the "Pragmatic Answers" series, which means: (1) it's ebook only, without any DRM, (2) it's short (about 75 pages or less), and (3) it's inexpensive, at under $10 US.

I teach a lot of software development training courses. That's my day job. Over the years I've found that a lot of students in my Mockito courses (and testing courses in general) are not sure where tools like Mockito fit. Mockito is a Java library that automatically generates mocks, stubs, and spies for you, but many developers don't know what those terms even mean or how they are intended to be used. The Mockito documentation doesn't really help -- the home page is at https://mockito.org, but the actual docs are at https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html, but while they're great for telling you how to use the Mockito API, they don't explain what Mockito is for or how it can help you.

That's why I wrote the book. My intent was to create a short, easy to read primer that shows what Mockito can do, helps you understand some of the more confusing parts that have arisen in the API, and show some basic use cases where it might make your development job easier.

I'll have more to say as the week goes on, but let me end this post with a couple of links:

* The GitHub repository for the book is at https://github.com/kousen/mockitobook. It uses a Gradle build file, with an included Gradle wrapper that I updated to the latest version this morning.
* I've written a series of posts on Medium (because the publisher has a publication there), but if you'd like to see my introductory video on why you might want to use Mockito at all, see https://www.youtube.com/watch?v=IRYYL61-GNk . That's part of my own YouTube channel, Tales from the jar side, at https://www.youtube.com/@talesfromthejarside. I'm planning to add another video in that series this week.
* For the past four years, I've been sending out a free weekly newsletter, also called Tales from the jar side, hosted on Substack: https://kenkousen.substack.com.

I hope some or all of that is helpful for you.

Ken
1 year ago
Thank you very much, and I hope the Java book was helpful :⁠-⁠)
1 year ago
Can a boss provide too much feedback? Sure, especially if they are being a micromanager. The issue is whether you want them to stop or not. To do so, you have to push back, and I talk in a lot of detail about how to do that in a way that does not threaten the loyalty relationship.

Here are a couple of Medium posts about it:

When Your Boss Is A Micromanager
https://medium.com/pragmatic-programmers/micromanagers-ken-kousen-93c1a97f29d7

and

Pushing Back: Lessons from the Prisoner's Dilemma
https://medium.com/pragmatic-programmers/pushing-back-lessons-from-the-prisoners-dilemma-451b948c6f1d

(I'm publishing posts on Medium so I can be part of the Pragmatic Programmer's publication there.)

So it really depends on whether the feedback is welcome or not.
2 years ago
A couple of things:

1. You might want to ask that question in the Jobs Discussion forum but in any topic other than this one, as this topic is ineligible for the drawing for free books. The number of question is still really low, so you have a good chance of winning one.

2. Thanks for appreciating the Kousen IT gag. I still say "Eye-Tee", but my wife pronounces it like I'm a member of the Addams Family.

Ken
2 years ago
It sounds sad, but (1) I took pictures, and (2) I got a story out of it, which I've used several times since.

It's true that most software developers used to buy ebooks directly from the publishers, like Manning or Pragmatic Programmers. But some of that is changing. For example, right before my Modern Java Recipes book came out, O'Reilly got rid of their online bookstore. Now they only supply books to distributors, and instead sell subscriptions to their own online O'Reilly Learning Platform. If you want a physical book, they actually link to Amazon.

It will be interesting to see how that evolves over time. For my current book, HYBHY, the Prag site https://pragprog.com/titles/kkmanage/help-your-boss-help-you/ only sells ebooks. If you want a print version, you go to Amazon https://www.amazon.com/Help-Your-Boss-You-Opportunities/dp/1680508229/ref=sr_1_4 .

Of course, that means when I give out coupon codes, they only work for the ebooks. I have no control over Amazon.

Btw, I'm in the process of preparing an audiobook version, which will be available through Audible (now a division of Amazon, sigh), iTunes, and some others.
2 years ago
On the humor front, when I wrote the Kotlin Cookbook, a non-technical friend of mine asked if it was a cookbook on how to cook kotlins, whatever they were. I think that person was joking, but in response I grabbed a copy of the book at my local Barnes and Noble and moved it over to the Cooking section to see what would happen.

When I revisited it about a week later, it had been returned to the IT section. Oh well.

Incidentally, the only reason my local B&N had those copies is because I asked them to order some. I signed them all and then waited as they sat on the shelf, fermenting, until eventually they got sent back to the publisher.

For HYBHY (my current book), Amazon still lists it as #1 New Release in Business Communication (#2 in Business Ethics) and claims it was released on July 27. If you look at B&N's web site, however, it claims the book doesn't come out until October, though you can pre-order it. That may hurt sales a bit.
2 years ago