Questions tagged [junit]
47 questions
25
votes
5 answers
How to drastically improve code coverage?
I'm tasked with getting a legacy application under unit test. First some background about the application: It's a 600k LOC Java RCP code base with these major problems
massive code duplication
no encapsulation, most private data is accessible from…

Peter Kofler
- 865
- 2
- 8
- 15
23
votes
2 answers
Unit testing classes that have online functionality
When unit testing functions of a class that has private functions that require online functionality. How would one go about testing it?
For example:
public class Foo
{
public int methodA()
{
int val = goOnlineToGetVal();
…

Ben Crazed Up Euden
- 333
- 2
- 5
19
votes
7 answers
Should each method have a separate JUnit test class?
I am writing JUnit unit tests for my classes.
Is it better to have a separate class for each method, or have just one test class for every actual class?

viraj
- 491
- 2
- 5
- 10
15
votes
5 answers
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();
}
However, I'm not sure what the best way to write a…

A_B
- 409
- 1
- 3
- 10
11
votes
3 answers
Mocking concrete class - Not recommended
I've just read an excerpt of "Growing Object-Oriented Software" book which explains some reasons why mocking concrete class is not recommended.
Here some sample code of a unit-test for the MusicCentre class:
public class MusicCentreTest {
@Test…

Mik378
- 3,838
- 7
- 33
- 60
10
votes
3 answers
Are manually writing unit tests Proof By Example?
We know that writing JUnit tests demonstrates one particular path through you code.
One of my associates commented:
Manually writing unit tests is Proof By Example.
He was coming from the background of Haskell which has tools like Quickcheck and…

hawkeye
- 4,819
- 3
- 24
- 35
10
votes
1 answer
Testing private methods as protected
I was reading this answer about testing private methods and it mentioned several possibilities:
extract methods as public to another class
make them public
separate the env of test and production so that methods are only public in the test env
I…

sam
- 211
- 1
- 2
- 5
9
votes
4 answers
Test cases do all the work through helper method -- bad practice?
Consider a test suite like this:
public class MyTestSuite {
@Test
public void test_something() {
testHelperMethod("value11", "value12", "value13");
}
@Test
public void test_something_else_meaningful_name() {
…

Attilio
- 477
- 3
- 8
8
votes
1 answer
Writing a valid test case for validating XMLs
How would I write a unit test, say JUnit, for validating an XML file?
I have an application that creates a document as an XML file. In order to validate this XML structure, do I need to create an XML mock object and compare each of its elements…

Mohammad Najar
- 203
- 2
- 6
8
votes
3 answers
What should I mock in tests of an application with service tier and DAO tier?
My classes are following this structure
Service Tier (creates and maps InputDTO to DB Data)
DAO Tier (actually executes DB calls)
When I write service tier JUnit tests, the DAO tier is called, and this expects an actual DB connection and getting…

shinynewbike
- 467
- 4
- 16
7
votes
3 answers
How to test Java web applications
So far I haven't been testing or learning how to test my java web applications.
If you do, what are the tools you need, and is it really necessary to test a web application if doesn't need scaling or it won't be handling big traffics and multi…

OnlineAlien
- 125
- 1
- 1
- 4
7
votes
3 answers
Jenkins without Automated Tests
I know that Jenkins is focused on continous building/testing, monitoring of batch jobs about the project. We have a legacy project which such condition :
1)Has a development team.
2)It has SVN for source code management
3)Some cronjobs for some…

lionme
- 73
- 2
6
votes
1 answer
What is the method to articulate the tradeoffs between short-fast mocked JUnit tests and slower Integration tests?
The assumption is that we're working on a large legacy Java codebase - with active ongoing development.
I had a team member say:
We should use lots of smaller JUnit tests that are focused on one class - because the fast feedback and ease of…

hawkeye
- 4,819
- 3
- 24
- 35
5
votes
4 answers
Should I put UI and logic in separate classes?
I currently work on a hobby project which I use to learn more about Android/Java programming and programming in general. Recently, I decided to integrate jUnit into the project. Just getting it in there wasn't much of a problem, but actually using…

Namnodorel
- 242
- 1
- 3
- 10
4
votes
4 answers
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 {
protected Bar a() { ... }
// ... other…

oneturkmen
- 165
- 1
- 5