Skip to content
Advertisement

Tag: unit-testing

Mock final class with Mockito 2

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

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:

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

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

Advertisement