Questions tagged [junit]
Popular unit testing framework for Java and Scala. The latest version, JUnit 5, supports rich annotation-based and parameterized tests. Consider using in conjunction with the Java or Scala tag to indicate your use case.
26,390
questions
0
votes
2
answers
19
views
In JUnit 5, why are postMethod throwing TransactionSystemException instead return ResponseEntity with bad request?
I'm pretty new to Spring Boot and JUnit, so im trying to build a Rest API with validation contraints like below:
Entity class:
@Entity
public class CareerLevel {
@Id
@GeneratedValue(strategy =...
0
votes
0
answers
16
views
Junit for Rest Controller with Authorization
I am writing junit test for below source code
@Slf4j
@RestController
@RequestMapping("/routeparameter")
public class RouteParameterController {
@Autowired
private ...
0
votes
0
answers
10
views
Is it possible to conditionally use test containers vs a locally running docker container?
Apologies, writing this on mobile.
My companys application uses test containers for multiple images, plus flyway to setup schemas, etc. Our test suite is robust but long running. So I'm looking for ...
0
votes
0
answers
7
views
Test events were not received
I'm trying to unit test some of my viewModels on my modularized project, but in Android Library modules, I receive this Tests events were not received, even the auto generated examples don't work. On ...
0
votes
0
answers
17
views
@ParameterizedTest not working with parameters
I'm creating test cases for an Azure Function. I'd like to use parameterized tests but it refuses to pick up any test that has parameters. I used the official Azure Functions Maven archetype to ...
0
votes
0
answers
8
views
Android JUnit Null check test
I'm trying to become a better tester, but I'm currently having trouble with the implementation. Say I have a list of books, each book has an id & title. I have a function that loops through the ...
0
votes
1
answer
17
views
How to disable junit tests with ConditionalOnProperty?
I conditioned some endpoints in my service with environment variables as a feature flag. For one Controller I put @ConditionalOnProperty("enable.v2.foo") at class level and for another
I put ...
0
votes
2
answers
40
views
Cannot catch value in Java Unit Test
I have the following service method:
public CommandDTO update(UUID uuid, EmployeeRequest request) {
final List<Product> productList = productRepository
....
0
votes
1
answer
29
views
How to get TestClass in main method in maven project?
I have Junit5Runner class which starts Junit5 test programmatically.
It's not a maven project now, it's a simple java project with no any framework.
I need to switch to maven project, but in a maven ...
0
votes
0
answers
14
views
mock retrofit service observable not calling onError method Android Mockito
I am testing this code and I got success in the success response code but I am not able to reach the onError code
internal class MaintenanceStatusResponseHandler {
fun getMaintenanceResponse (...
0
votes
0
answers
14
views
Migrating Rules from Junit4 to Junit5
I have some Junit4 tests. I want to migrate the tests to use Junit5, but there are somethings that I can't really understand how I can change it to Junit5. I am using @Rule annotations and in some ...
0
votes
1
answer
16
views
Counter value is not incrementing while replacing in Junit request
I have created a counter (userCount) in my test plan and I want to pass that incremented counter value in Junit sampler "Constructor String Label" for each thread user like "${userid}${...
0
votes
1
answer
33
views
Force the server datetime to a given value, in order to perform a JUnit test that doesn't have the new date as parameter
How can I simulate a certain datetime inside a unit test of my java application, independent from the real date time of the server, such that the application 'believes' that the current datetime is ...
0
votes
0
answers
10
views
How to update junit test result in to Xray using cypress javascript
I was looking at the documentation of the Xray plugin : https://docs.getxray.app/display/XRAY/Import+Execution+Results+-+REST#ImportExecutionResultsREST-JUnitXMLresultsMultipart
And what I found, is a ...
0
votes
1
answer
21
views
Mock Bean for all JUnit Tests in Spring Boot
In Spring Boot, is there a way to mock a single bean for all existing JUnit tests, without changing the existing test classes (e.g., by adding an annotation or adding inheritance)? Like injecting a ...
0
votes
1
answer
19
views
Mockk: no answer found for a mocked JPA call with partial matching?
I'm trying to write a Junit test case for an update API. The API is using saveAll method of JPA to perform updates. The API is working fine, but when trying to mock the saveAll method it is giving me ...
0
votes
0
answers
17
views
Button click testing
how can I use Junit to test a button click, everything happens in Android studio in java?
I am very bad at Junit testing
public class ActivityMenu extends AppCompatActivity {
@Override
protected void ...
0
votes
0
answers
21
views
JSON decoding error: Invalid UTF-8 start byte 0xb0
I have a problem when I run the Junit for this method:
public ResponseEntity<InputStreamResource> xlsexp(@RequestHeader MultiValueMap<String, String> headerMap,
@...
0
votes
0
answers
10
views
AWS CodeBuild - How to show Error test classes in case of unhandled exception from JUnit5 BeforeAll?
If there is an unhandled exception from JUnit5 BeforeAll like below then AWS Code Build is not able to show the failure test classes.
class ApiImplTest {
@BeforeAll
public static void beforeClass()...
0
votes
0
answers
23
views
Junit how to import file properties to service in test
I am writting unit test and have some values from application.yaml property file. In my test before i can go into service method values are imported from test/resources, but when go into service im ...
0
votes
1
answer
20
views
Mock Runtime.getRuntime() - Spring Boot - JUnit
I tried to mock my Runtime.getRuntime() with the following code :
PowerMockito.mockStatic(Runtime.class);
PowerMockito.when(Runtime.getRuntime()).thenReturn(mockRuntime);
But I had the following ...
0
votes
0
answers
10
views
How to mock jdbcTemplate.query(query.toString(), resultSet -> {});
My Repository code:
jdbcTemplate.query(query.toString(), resultSet -> {
PartGroup partGroup = new PartGroupMapper().mapRow(resultSet, resultSet.getRow());
partGroup....
-1
votes
0
answers
13
views
Parse DMN file using Saxon and Auto Generate Test Cases for the DMN model using JUnit
A DMN model is designed which consists of a single input=number, and a decision=no. greater than 10. This model provides a decision that whether a given number is greater than 10 or not.Here our task ...
0
votes
1
answer
19
views
Unit test using junit and mockito for getObject method in AmazonS3
I have method which uses s3.getObject to get S3Object and writes the contents of the object into a temporary file
@Override
public Optional<String> getObject(String s3BucketName, String ...
0
votes
1
answer
29
views
how to test @ConfigProperty variable of a class
Kindly let me know how we can test below piece of code in junit.
ConfigProperty is reading the values from application.property file. now i have to write an unit test case how i can writ kindly help ...
0
votes
0
answers
10
views
Jenkins: Can I control how junit failures affect the build?
I have a declarative pipeline, and I have two groups of tests:
The failures from 1 group are actual warnings, and they must stop the build.
Failures from the second group are only warnings: ideally I'...
1
vote
1
answer
30
views
AssertNotNull AssertionFailedError: expected: not <null>
I'm testing an app, where a method creates and object, and I'm trying to test that the object actually gets created like this:
@Test
public void testCreateWagerGetsCreated(){
...
1
vote
0
answers
30
views
How to make test file names appear in Gitlab Junit test report?
I installed karma-junit-reporter in an angular project with the following settings:
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/...
0
votes
1
answer
30
views
MockMvc perform Basic Authentificaton Point with custom filters not calling DispatcherServlet
I use @AutoConfigureMockMvc, all no-auth requests and requests, that needed authentication work perfectly (with calling DispatcherServlet)
But when I try to execute auth point (username, password) to ...
-1
votes
0
answers
21
views
How to write junit for void method call of same class in junit?
How to cover below m2() method? In pit report mutant is still there. I searched it is suggest me to use spy for this kind of problem, but I don't know which case it is using.
Class Data{
void M1(){
...
0
votes
0
answers
29
views
JUnit and Mockito test for FileUtils.copyURLToFile
How can I write the unit test using JUnit and Mockito for a method as simple as the following
public class FileUtil {
public void copyURLToFile(String url, String filepath) {
try {
...
1
vote
2
answers
61
views
How to mock a variable and method in Abstract Class?
In a Java (Spring Boot) app, I use JUnit and Mockito and want to test the following service method:
public Page<EmployeeDTO> findAll(EmployeeRequest request, final Sort sort) {
final ...
0
votes
1
answer
35
views
Why we need to mock static class before every test case in same class
I have a test class with two test cases . I am using Junit 4. My these test cases are using static class. So I mocked a static class in @BeforeClass method so that it is only mocked once before the ...
0
votes
0
answers
17
views
How to test by junit5 a project from another project
I would like to create a web service by spring boot with Java learning tasks. I would like users to be able to upload their solutions as java classes to a web service for testing purposes.
I used to ...
2
votes
1
answer
36
views
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a];
I am trying to test an “add” functionality in my service in spring boot.most cases that i saw were different data types, but i don't think that this is the case here.
i will show the the code and ...
-4
votes
0
answers
24
views
cant create a Binary tree test class [closed]
Hi, Im having a problem with creating a JUnit Testclass for my BinaryTree. Can someone solve this issue pls?
0
votes
2
answers
86
views
How to mock an intermediate class in the code flow in Java
I have 3 classes, as shown below, and I want to write a Unit test for the validate() method of class A. I do not want to mock class B. I want to mock class C and always return the same String when ...
0
votes
0
answers
44
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 ...
0
votes
0
answers
25
views
Parallel test execution and growing active connections to mongodb
I have an application with thousands of tests through MockMvc. The tests are running in band, so 1 at a time and whole process takes hours. I decided to try parallel test run with
systemProperties[&...
1
vote
1
answer
28
views
Mockito verify still fails on using all Matchers
I am recently writing a JUnit test and I acknowledged that Mockito either needs all Raw values or using all Matchers but I find the following case still fails with all Matchers, error message is:
...
0
votes
0
answers
8
views
bindBindingsValuesProvidersByContext not found for reference aem unit test
Getting below error when I run a unit test class individually in intellij , if I do mvn clean install -PautoInstallPackage test class passing.
java.lang.RuntimeException: Bind method with name ...
0
votes
0
answers
19
views
How to write test cases for Service layer using Junit 4 and mockito
My Repository layer is returning list object, But in my service layer i am filtering out all other and return a single Object.
when i try to create test cases it is giving org.mockito.exceptions....
0
votes
1
answer
38
views
Shall I mock a bean with 10 methods when I only use one? [closed]
I face some situations similar to the following simplified one:
@Component class ServiceOne {
@Autowired ServiceTwo two;
void act() {
...
two.a();
...
}
}
@...
0
votes
1
answer
25
views
How To Test Login Method In JUnit?
I have been using white box testing with JUnit to test some methods that I have in my DAO Implementation class. I am trying to test the login functionality, which is defined in the Service ...
0
votes
1
answer
33
views
How to write Junit for clone method catch block
My respected java developer friends.
i am trying test and cover the catch block of clone method. I have wasted one week but didn't find any solution to cover catch block. Please help me.
My source ...
0
votes
0
answers
10
views
Quarkus : How to disable/mock Vault for unit testing
I'm currently facing an issue with Quarkus and Vault.
When I launch JUnit tests, Vault trying to start and it failed because Vault configuration is not setup to run locally.
Caused by: java.lang....
0
votes
0
answers
20
views
How can I mock dependencies which aren't interfaces but subclasses?
I'm not sure if my design is simply stupid, or there's a good way to make it work. Consider the following classes:
public abstract class CheckBase {
protected Collector collector;
public void ...
0
votes
1
answer
30
views
How to verify in a unit test that value that is not passed to service or repository?
I am trying to write a unit test for the following method (I use JUnit and Mockito):
public CommandDTO update(UUID uuid, OrderRequest request) {
final List<MenuItem> menuItemList = ...
-1
votes
0
answers
18
views
How to avoid jwt token or ignore authentication when testing HTTP API request?
mvc.perform(MockMvcRequestBuilders.get("/PensionerDetailByAadhaar?aadhaarNumber=123").header("Authorization","Bearer "+token)).andExpect(status().isOk());
Here I need to ...
0
votes
0
answers
34
views
I can't import the containsString CoreMatcher from hamcrest
I'm trying to do the following:
import static org.hamcrest.CoreMatchers.containsString
assertThat(myvar, containsString("string to check"))
Here are my dependencies. I need to support ...