All Questions
Tagged with java unit-testing
106
questions
1
vote
2answers
84 views
Best Practice: Unit test coverage vs. in-method sanity checks [duplicate]
I have a code-coverage requirement of of a certain percentage, and face the following tradeoff:
Should I sacrifice in-method sanity checks and error handling for ease of (unit-) testability?
Lets ...
0
votes
2answers
113 views
Is there any benefit testing only with mocks/fakes/doubles?
Say I want to test the behavior of the GUI while I follow a PassiveView approach. I also use the command pattern to handle the actions of the user. So given a PersonView and a PersonService with a ...
2
votes
3answers
184 views
Extending the class to test it: is this testing approach fine?
I am curious if the following example of testing a class with protected methods is fine.
For example, say you have the following class Foo that has method a() of return type Bar:
class Foo {
...
0
votes
2answers
58 views
How to cover by tests HTTP API wrapping library
As mentioned in title, I don't understand how I supposed to cover by tests code which is just wrap http api.
I guess I can write only unit tests, because wrapped service is paid. Integration tests in ...
-1
votes
1answer
283 views
Java: Splitting a large unit test class
The project (Java/Spring) I currently work on has a rather large unit-test class for one of its services : more than 1000 lines, several Nested class (one per big functionality), some tests without a ...
-1
votes
3answers
207 views
Should methods with business logic be made private? [duplicate]
So I am writing a project using Spring Boot. All of my logic resides in @Service classes. I have separated each service class based on entity. For example - If I have two independent entities A and B, ...
4
votes
1answer
3k views
Should I mock ObjectMapper in my unit tests?
I have different services in a spring application that have a dependency on Jackson ObjectMapper, the unit tests rely on @InjectMocks to inject all the various dependencies to the class that is under ...
2
votes
2answers
248 views
Migrating legacy code with singletons to Dependency injection
I'm working on a larger, older project. Our code is littered with classical singletons, i.e. classes like
public class ABCService {
private static final instance = new ABCService();
public ...
0
votes
1answer
40 views
Should all third party methods that access outside resources (like other databases) be wrapped up?
From the perspective of unit testing, the code under test should obviously not be accessing outside resources so the third party methods need to be mocked. However, it seems like this is poor practice ...
2
votes
4answers
281 views
How do I deal with the fact that I am forced to make helper functions public for testing purposes?
I've encountered several scenarios that require me to mock certain helper methods because they call outside resources. As a result, I'm forced to convert all my helper methods from private into public....
0
votes
2answers
166 views
Design Java Testing class for hierarchical objects
Consider the following POJO structure in my main code. I want to create some testing framework for this kind of hierarchical classes, where the calling test method can specify if they want to modify a ...
2
votes
3answers
897 views
What is the proper logging level for tests?
I raised an issue at my employer concerning our gigantic heaps of testing logs.
I raised the issue because I was spending well over a minute to find the relevant logs and stack-trace.
I stated we ...
0
votes
2answers
100 views
What to test when testing an API? [closed]
When testing an API (with, for example, Java), what parts should I actually be testing when calling methods of my Controller class (e.g. a Spring RestController)?
For example, lets say I've got a ...
4
votes
2answers
2k views
What is recommended way to create test data for unit test cases?
I am new to TDD/unit testing.
I am going to write a complex scheduling algorithm in Java. As this module is a core part of our application and there are number of scenarios in it, I want to write ...
-1
votes
1answer
73 views
Understanding property based testing
I'm reading about property based testing and I'm wondering how can I test this my code using that paradigm.
class Invoice {
private final String id;
private final String companyName;
...
4
votes
1answer
1k views
Wrapping utility classes and injecting them for unit testing purposes
I found that it is so hard to test classes that depend on other utility classes
as java.nio.file.Files. It is also impossible to mock them using the classic unit testing stack (junit,mockito,..) ...
2
votes
4answers
969 views
Creating new constructors to overwrite the existing instance variables for testing purpose
Is it good/bad practice to add more constructors just for test purposes (to mock the DOCs used in my SUT) like this :
public class A {
private B b = new B();
private C c = new C();
public ...
8
votes
3answers
1k views
Philosophy on unit testing daisy-chained methods?
Problem Statement:
I have a class that does some validation and it looks something like this:
func a() {b()}
func b() {c()}
func c() {d()}
func d() {e()}
func e() {return}
This is a simplified view,...
60
votes
7answers
14k views
Code coverage highlights unused methods - what should I do?
I have been tasked to increase code coverage of an existing Java project.
I noticed that the code coverage tool (EclEmma) has highlighted some methods that are never called from anywhere.
My initial ...
1
vote
1answer
85 views
Mocked dependencies verification - Best practices
I have a simple question about best practices in unit test verifications.
Given this example:
@Test
public void methodUnderTest() {
when(mockedDependency.someMethod()).thenReturn(someValue);
...
15
votes
5answers
61k views
Unit testing a void method
In order to fix a bug in an application, I modified a method named postLogin by adding a call to an existing method named getShoppingCart.
Code
protected void postLogin() {
getShoppingCart();
}
...
1
vote
1answer
4k views
Testing a private method in Java [duplicate]
I am new writing good test cases, so please bear with me. Writing a test case for private methods
public Stock getStock(String stockTicker) {
Stock company = new Stock();
...
4
votes
2answers
1k views
Unit Test : Do I need to make an unit test for each class in my project
I have recently tried to to implement unit tests on a Java Web Application my project is built on MVC design architecture and uses Spring & JPA Hibernate and JSF here is a package tree
-src
-...
1
vote
2answers
330 views
Writing basic unit-tests inside the class file
In Python it is common to have "doctests" - simple tests inside the documentation of classes and methods. Their main advantage is that they can easily be executed while editing the code - when I ...
1
vote
1answer
3k views
Unit testing when you have no getters and setters
I'm a student and I'm learning about domain driven design. From my reading, an important part is removing the getters and setters from a class (thus keeping the implementation of that class private). ...
7
votes
4answers
20k views
Testing a function that uses random number generator
I have a method which is a part of the interface I am implementing. This method calls another private method which uses a random number generator to produce output and returns it to the calling method....
1
vote
2answers
921 views
What are the drawbacks of modeling (unit) test fixtures in JSON instead of using ObjectFactories?
When writing java tests for an application, be they unit tests or testing a broader scope, the java community tends to model fixtures in terms of object factories that produce fixtures of a defined ...
1
vote
1answer
363 views
How fast is loading from a jar as opposed to loading from outside the jar? Is it ok to load a file within the jar in a unit test?
My understanding is that a major point of contention with reading a file during a unit test is that it is slow. There may be 1000 unit tests and we want the build to complete quickly so the unit tests ...
4
votes
3answers
1k views
Is this unit test too tightly coupled to implementation?
I believe the existence of this unit test is justified. However, to me, this unit test seems very coupled to the method implementation, though I'm not sure it can be improved upon.
class ItemManager {...
-6
votes
1answer
2k views
Should I use assert(true) in junit tests?
For junit tests where I am just testing if something works correctly, should I use assert(true)? Since it is legacy code, it is difficult to get a good assert statement.
9
votes
1answer
472 views
How to manage non-unit tests in a project?
I have some code in my project I personally call tests that are not unit tests. They are meant to be run and the result has to be evaluated by a human. I did this because I'm making a physics engine ...
-1
votes
1answer
87 views
Unit testing a general purpose database library
Many moons ago I built a utility library that wraps the JDBC libraries with classes that allowed for functional-style approaches. I've used this library for my own purposes for many years. I've long ...
8
votes
2answers
2k views
Integration tests, but how much?
A recent debate within my team made me wonder. The basic topic is that how much and what shall we cover with functional/integration tests (sure, they are not the same but the example is dummy where it ...
6
votes
2answers
622 views
Is it considered a bad practice to create the Object.equals() method for testing purposes only? [duplicate]
Like the title says, is it a bad practice to create/generate the Object.equals() method because I need it in my unit tests, but not in my regular code?
19
votes
1answer
2k views
Does it make sense to measure conditional coverage for Java 8 code?
I'm wondering whether measuring conditional code coverage by current tools for Java are not obsolete since Java 8 came up. With Java 8's Optional and Stream we can often avoid code branches/loops, ...
6
votes
2answers
3k views
Whether and how to test façades
In my application I have quite a few service classes that act as a façade and delegate most calls to one or more underlying manager classes. I've read very different opinions on how to test such ...
8
votes
1answer
1k views
How to unit test custom ClassLoader?
For some reasons, I need a child-first ClassLoader. Such ClassLoader doesn't exist in the JDK, so I'm writing it. Since this is a key component of my use case, I'd like it to be heavily tested. To ...
0
votes
2answers
287 views
How to I best test this method? Do I need to split it up?
Okay, please consider the following method. Let me first tell you that my goals of the method is to determine if file system assets exist. That's to say this is an internal company site where you'd ...
1
vote
3answers
714 views
C# / Java: Should every class have a main method?
I've been learning C# lately to see the other side of the coin (I have a decent amount of Java knowledge already) so I've been reading up on C#, and I came across an article called C# for Java ...
4
votes
3answers
4k views
How can I write unit test case of file format converter utility?
I have created one utility module which converts one file format to another one. File i.e. test.abc will be converted to i.e. test.pqr and internal format of files are totally different. This module ...
3
votes
2answers
9k views
Unit testing / How to validate private fields of a newly created object?
I have a basic unit test (for the sample) that involves this code:
void testShouldCreateACar() {
Car car = someone.createFerrari();
assertTrue(car.name == "Ferrari"); // can't access name since ...
2
votes
4answers
3k views
Is it possible to capture java objects to save them as test resources?
I'm wanting to write unit tests to check that for given database responses, certain objects are create/things are done.
The problem I have, is that when mocking the database response, I have to ...
2
votes
1answer
330 views
Using Spring in Java Project
I've got a question about a correct usage of Spring.
I know that some people use the DI "aggressively" so that they always use spring and completely eliminate the usage of word "new" in the ...
-3
votes
3answers
295 views
What is the most elemental workflow for TDD?
Gradle is such an interesting build tool that it prompted me to look at Spock and JUnit -- which I've never done before. What is the basic workflow with TDD?
My approach has been to do frequent ...
13
votes
5answers
731 views
Broken Old/Legacy Unit Tests
I work for a big company and I'm responsible for a large java application with thousands of junit tests. Since I moved to this role, there have been 200-300 broken tests (likely broken for years). ...
2
votes
2answers
3k views
Testing using mocking, must I mock all dependencies too?
I have the following method to test:
public List<MarkId> getMarkIdList(ICar carDoc) {
ICourseCar courseCarDoc = courseCarRep.get(carDoc);
List<MarkWag> markWagList = ...
14
votes
2answers
21k views
Should I unit test my subclasses or my abstract parent class?
I have a skeletal implementation, as in Item 18 from Effective Java (extended discussion here).
It is an abstract class that provides 2 public methods methodA() and methodB() that call subclasses ...
3
votes
2answers
362 views
Better To Call In Dive Into Submethods Or Pass Around a result
I have a service that does fairly complicated business logic when it receives a request. I have most the functionality isolated into private methods which dive a couple levels down from the method ...
10
votes
2answers
1k views
Can I check the existence of an annotation in an unit test?
I have a java class hierarchy that are formed by an Abstract class and N extensions of it.
In the abstract class I have a method that is annotated with a @Remove annotation. While we won't get any ...
3
votes
1answer
10k views
Should I use a mock or create a new instance of an object in unit tests? [closed]
I have to write a unit test for a method like:
void doSomethingWith(Country country) {...}
There are the following classes:
Interface:
public interface Country {
String getName();
... // and a ...