I’m removing Powermock from the project I’m currently working on, so I’m trying to rewrite some existing unitary test only with Mockito (mockito-core-2.2.28). When I run the test, I have the following error: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.ExternalpackagePath.Externalclass Mockito cannot mock/spy because : final class I know that this question has already been asked (How to mock a final class
Tag: unit-testing
Handle anonymous class with Generics in Mockito
I am trying to write unit test using Powermockito for following method – I have written test method as – When I run this test I always get it failed saying – It looks like stub is not working as I always get “null” for this line – Is it because anonymous instantiation of TypeReference class? Answer Yes, it’s because
JUnit – How many files for testing a single class?
Let’s say I have a class MyClass.java and we have to write a tests for it. Some of them will execute once, some of them will execute multiple times. I prepared two test classes – MyClassTestSingle.java for single tests and MyClassTestMultiple.java for tests with @Parametrized annotation. I wanted to run them from a single file so there is another class
Mocking a singleton with mockito
I need to test some legacy code, which uses a singleton in a a method call. The purpose of the test is to ensure that the clas sunder test makes a call to singletons method. I have seen similar questions on SO, but all the answers require other dependencies (different test frameworks) – I’m unfortunately limited to using Mockito and
Eclipse JUnit 5 support
Currently, JUnit 5 is just out with a “stable” version. IntelliJ supports JUnit 5 according to the Website. My question is if eclipse is supporting JUnit 5 as well, and if not when it is going to be supported. With supported I mean if I can run JUnit 5 tests without the need for a @RunWith(PlatformRunner.class) annotation. EDIT October 2017:
mock resttemplate to test a service as restFul client
I have a service class, written in spring, with some methods. One of this acts as a restful consumer like below: I want to use Mockito to mock my service, but every method that interacts with restful server instance a new RestTemplate. I’ve to create a static class to Inject it into my service? Answer One of the benefits from
Set value to mocked object but get null
I have a simple class Foo to be mocked: In my unit test code, I mock it by using Mockito. I set name in mocked object, but when I call getter to get the name it returns null. Is it a Mockito specific issue or is it an convention that mocked object can’t set value? Why is that? What is
Mockito and ReloadableResourceBundleMessageSource doesn’t go well together?
I have this simple test, it isn’t even a test as I’m simply trying to mock the messagesource. I’m getting this error: Can anyone else verify this behavior? This is a minimum spring boot skeleton test I set up because ReloadableResourceBundleMessageSource didn’t work in another project, so I thought I’ll just try it in the smallest unit possible. This is
How to mock a autowired list of Spring beans?
I’ve read plenty of articles about how to mock Spring’s bean and their autowired fields. But there is nothing I could find about autowired lists of beans. Concrete problem I’ve a class called FormValidatorManager. This class loop through several validators which implements IFormValidator. I would like to test this class. But I can’t find a way to mock validators property.
JAVA best practice unit testing with JUnit
I am currently using the following method and unit test for the method. I think the test could/should be broken down into more tests but I’m not sure how many tests to write for this or what the more important parts are especially considering the method deals with establishing a Connection, using sql queries, etc… All help is appreciated. JAVA