147 questions
0
votes
0
answers
52
views
Maven tests failing when combining File endpoint with rest endpoint due to incompatibility of Servlet class for interception
I have two routes defined in seperate classes.
1.
public class FileRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file:src/main/...
0
votes
1
answer
59
views
Spring Security controller test cases are failing with 403 status code
My test cases are failing in controller class due to spring security.
@PostMapping("/{username}")
@PreAuthorize("hasRole('MEMBER') && #username == authentication....
-1
votes
1
answer
78
views
Why when mocked predicate, return null pointer? [closed]
My predicate returns null pointer when it is executed.
My test code:
public class CompanyRepositoryTest {
@Mock
private EntityManager entityManager;
@Mock
private CriteriaBuilder ...
1
vote
0
answers
334
views
Not able to understand this @Mockbean behavior
Usually I use @MockBean annotation to any JPA repository services and works fine. But I tried to use @MockBean with RestTemplate object which was getting created in the Configuration class and it ...
0
votes
2
answers
193
views
Using InjectMock and MockBean on static vs non static objects
I have a service class which uses a dependency -
MyService
@Service
@ToString
public class MyService{
Dependency dependency;
public MyService(Dependency dependency) {
System.out....
1
vote
0
answers
56
views
Test CompletableFeature.exceptionally(Ex) inside public method
writing test cases for following code but not able to cover exceptionally part of completable feature
CompletableFuture<Optional<A>> aFuture = CompletableFuture.supplyAsync(()->Optional....
0
votes
1
answer
199
views
Mockito and Circular Dependency: How to Invoke Actual Class Implementations
The following classes reference each other crating a circular dependency:
@AllArgsConstructor
@Component
public class A {
private B b;
public String doSomething() {
return "...
1
vote
1
answer
72
views
Mock method which contain functions as well as object as parameters?
Writing Junit unit testcase, I am trying to mock method callservice using Mockito.
public CompletableFuture<String> getInfo(String something)
{
CompletableFuture<String> future = new ...
0
votes
1
answer
1k
views
Getting null pointer exception while using Mockito to mock an interface
I am getting null pointer exception while I am trying to mock
@WebMvcTest(IMnJobManager.class)
public class CMnJobManagerTest {
@Autowired
private MockMvc mockmvc;
@Test
public void ...
0
votes
2
answers
4k
views
Mock object is null in JUnit Test
I design the test in JUnit using Mockito. The problem is in the following Test:
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
@InjectMocks
private UserService userService;
...
1
vote
1
answer
936
views
SpringCloudStream TestChannelBinderConfiguration Junits tests not working with Mockito
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
...
0
votes
1
answer
480
views
Junit mockito to test a public method which internally calls private methods
I have to write Junit Test cases using Mockito.
The method which I have to test is public method but internally it calles few private methods to get some DB data (Using EntityManager and Nativequery ...
0
votes
1
answer
5k
views
MonoJust cannot be returned by toString() toString() should return String (Reactive streams and Mockito)
@ExtendWith(value = SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public abstract class BaseAbstractTest {
@MockBean
protected TokenService tokenService;
@...
0
votes
0
answers
563
views
What to assert in a unit test for Kafka producer void send method
Here is my producer send method that I'm writing a unit test for:
public void send(final ReadingForecastRequest requestStub) {
logger.info("{}::send()", getClass().getCanonicalName())...
0
votes
1
answer
522
views
Java: Spring: How to mock a method inside Autowired object
@Component
public class SomeService {
public void method1() {
int batchSize = method2();
// some operations...
}
public void method2() {
// something
return 10;
}
Test class
@...
1
vote
1
answer
269
views
Why ZonedDateTime.now() throws NoSuchMethodException while running test cases(using mockito)
Lately I am writing junit test cases and I came across ZonedDateTime.now() works partially expected where the method which we try to write test case uses DozerBeanMapper for copying common fields. ...
0
votes
0
answers
115
views
Mockito stubbing exception
I understand that the below error tells either I have to use all mocks or all original values.
Invalid use of argument matchers!
0 matchers expected, 4 recorded:
but for my code, I am using all mocks ...
1
vote
0
answers
331
views
Mockito Junit5 NullPointerException - mocking public interface
Java Class
public class SendNotification {
public NotificationProperty config;
public PostDbClient postDbClient;
public ObjectMapper objectMapper;
public AmazonSNS snsClient;
...
0
votes
1
answer
44
views
Testing Spring service class which uses an Autowired object
I have to test a Spring service which uses an Autowired object, see the following code:
@Service
public class MyService {
@Autowired
ExternalService externalService;
public String ...
0
votes
2
answers
1k
views
How to mock Apache Camel Route before CamelContext is started in CamelSpringBootTest
Maybe a rather uncommon issue, but I'd like to mock certain Camel routes, in the JUnit setup method
@BeforeEach
void setUp() {
}
before the
@Override
public void configure() throws Exception {
}
is ...
0
votes
0
answers
246
views
Null pointer when creating a unit test for JPAQueryFactory
I have this method to update a table in DB, it works correctly, until I create the unit test for it:
public class ReferentialAdapter {
private final JPAQueryFactory queryFactory;
...
1
vote
0
answers
324
views
Spring Boot / Junit5+Mockito: Mock's mockitoInterceptor gets replaced during test?
I have two service classes (there are more, of course, but those two are relevant here), which are in use during an integration test.
For test, I set up a mock (ConfigurationService) and stub two ...
0
votes
1
answer
409
views
spring mockito: when an object containing a list is an argument
The issue is that the list of ids within the pageRequest object can be in any order. Is there a way to specify that below? The test fails since the pageRequest has a list of ids in a different order ...
0
votes
0
answers
24
views
How to mock object created with new keyword using mockito [duplicate]
This is the class structure:
class A {
private B b;
A() {
b = new B();
}
public void foo() {
b.anotherMethod();
}
}
Test class:
class TestA {
@Mock
...
1
vote
2
answers
905
views
Can I use Mockito.any() into then method
I want to use any() inside then method. Here is my test code:
@ExtendWith(MockitoExtension::class)
internal class UserSignInProviderTest {
@Mock
lateinit var authApiClient: AuthApiClient
...
0
votes
1
answer
795
views
Mockito @MockBean wont execute when on Kotlin
I'm super frustrated with a Kotlin/Mockito problem
What I want to accomplish is very simple, I've an AuthorizationFilter on my springboot application and for test purposes I want to mock its behavior ...
1
vote
0
answers
2k
views
How to mock mongodb connection for testing using Mockito in Springboot?
I'm new to testing framework
I have to test a function which have involves storing data from Db into list.
Note: I don't have any POJO Class or Entity class for this.
I'm just connecting to the ...
0
votes
2
answers
3k
views
any() vs any(Class.class) Mockito
I am not able to understand why below two tests are not giving the same result.
@Service
public class SomeManager{
private final SomeDependency someDependency;
@Autowired
public ...
1
vote
1
answer
517
views
Getting InvalidUseOfMatchingException or NullPointerException when all test run together
Getting following exceptions when run all tests together but works as expected when executed individually
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced or misused argument ...
-1
votes
1
answer
577
views
Junit5 test case for controller layer
@GetMapping(value = "/abc")
public ResponseDto<Map<String, FResponseDto>, Void> getFlightDetails(@RequestParam String fIds) {
Map<String, FResponseDto> response = ...
1
vote
0
answers
170
views
When to use and not to use Mockito Argument Matchers. difference between any(), anyString(), Mockito.anyString(), Mockito.any()?
Hi I am very new to mockito framework. Can you please explain in lay man term why we need argument matchers and what is the need of any(), anyString() and what if I use a correct string instead of ...
13
votes
1
answer
7k
views
When to use and not use @Mock annotation, @MockBean annotation, @InjectMock annotation & @Autowired annotation in a spring webflux reactive project
Can you please explain when to use below annotations and when not to use those. I am pretty new to testing frameworks and confused with all the answers in the web.
@Mock
private Resource resource;
@...
0
votes
1
answer
149
views
How to write JUnit 5 test cases for the following code?
This is the method to write the JUnit test cases for
public List<LoanApiCallEntity> getAllApiCallDetails() {
return apiCallDetailsRepository.findAll();
}
Inside the LoanApiCallEntity class ...
0
votes
1
answer
3k
views
Mockito Exception: Underlying exception : java.lang.IllegalArgumentException: Could not create type
What I am trying to Do
I am trying to test my class using cucumber and also make extent report on it. I am getting 2 errors in it 1 related to Mockito which is high priority and 2nd related to extent ...
4
votes
1
answer
786
views
I can't autowire Service class in Spring Boot Test
I created Dao Repository that uses jdbc for working with DB.
I autowired this repository in my Service class.
Then I try to autowire my service class in my test class.
@SpringBootTest
public class ...
1
vote
1
answer
2k
views
Should I write unit test for void methods (includes repository call)? [closed]
I have the following code snippets in my spring boot application:
@Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
public ...
0
votes
1
answer
518
views
java bootstrap mock with ConditionalOnProperty and Autowired member
I have an object that looks like this:
@Service
@ConditionalOnProperty(
name="name",
havingValue = "true"
)
public class MyClass{
@Autowired(required = false)
...
0
votes
0
answers
905
views
Spring Mockito @MockBean fails while stepping through with debugger; works fine when not debugging
I'm getting an error when I step through my test code with @MockBean.thenReturn() in a debugger (Intellij Java 17). It works fine when it is executed outside the debugger though.
Here is a ...
0
votes
1
answer
237
views
Have legit Spring beans injected into a mock bean
Anyone knows how can I have normal spring beans injected into my MVC controller?
@WebMvcTest(TaskController.class) class TasksManagerApplicationTests {
@Spy // WRONG
private TasksRepository taskDao;
@...
2
votes
1
answer
11k
views
Cannot execute any Mockito test. With JDK17 gives "NoSuchMethodException: sun.misc.Unsafe.defineClass(...) "
When upgrading my app from Spring Boot 2.2 with JDK 11 to Spring Boot 2.5.5 with JDK 17, Mockito gives this error:
Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang....
1
vote
1
answer
2k
views
How to Mock service consisting both Autowired and RequiredArgsConstructor in JUnit 5?
@RequiredArgsConstructor
public class GService {
@Autowired
private GRepository repository;
private final List<GRule> rules;
And my test class is like this;
@ExtendWith(...
1
vote
1
answer
3k
views
How to mock a method of Aspect class?
I have added a custom annotation on a method which is present inside a controller class.
I have implemented Aspect class which is considering custom annotation as a point-cut. Once the execution ...
2
votes
2
answers
2k
views
Mockito Throwing NullpointerException while calling doCallRealMethod
Am using Mockito version 3.6.28 for Junit testing. Am getting Nullpointer Exception while calling the real method on the object. Its because of the dependency on the target object is not injected ...
0
votes
1
answer
478
views
Mockito stub method that has many arguments
I wanted to stub a method that has 6 arguments without having to write any() each time
So instead of
when( service.method(any() , any() , any() , .. ).thenReturn( new ArrayList<>() ) ;
is it ...
0
votes
5
answers
12k
views
NullPointerException when invoking method with Mockito in Service test
The main problem is that the convertToMultipartImage method in the AzureCloudStorageService class is returning null unexpectedly during testing, despite the mocking setup seeming correct. This ...
0
votes
1
answer
1k
views
Trying to verify a method call from abstract class on a mocked service but I get a npe
I stuck since a couple of hours on a test method.
I tried to reproduce a similar situation. I have a service who extend a abstract service with a utility method like this:
public class MyService ...
2
votes
0
answers
351
views
Spring How to write Junit4 about Websocket
I know how to test websocket, like use WebSocketKing or Postman(2021/5/20), and it works good
I have no idea about writing websocket in unit test
usually, I use "SpringJUnit4ClassRunner" and ...
0
votes
0
answers
194
views
JUnit4 with Mockito for unit testing
public class DgiQtyAction extends DispatchAction {
private final Logger mLog = Logger.getLogger(this.getClass());
public ActionForward fnDgiQty(ActionMapping mapping, ActionForm form,
...
0
votes
1
answer
819
views
I want to test to my controller method which consume=MediaType.MULTIPART_FORM_DATA_VALUE only
I am using mockMvc for testing. How I can send PaymentOrderDto object value in mockMVc.perform which I can receive at controller method.
Like PaymentOrderDto.setMid("m1") etc.
@PostMapping(...
0
votes
1
answer
598
views
How to fix unit test Missing Method Invocation Exception?
When I run the test it shows the given error which I gave below.
It shows Missing Method Invocation Exception Error. But I use that method in that test.
I use Mockito to inject mockito.
If anyone can ...