Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8659803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:05:45+00:00 2026-06-12T16:05:45+00:00

Goal : testing TDD in a typical enterprise Java environment. Context : Frameworks used

  • 0

Goal : testing TDD in a typical enterprise Java environment.

Context :

Frameworks used (even if it’s overkill, I practice project based learning) :

  • DAO : Hibernate
  • Spring IoC
  • Front : Spring MVC + Twitter Boostrap (if possible)
  • TDD : JUnit
  • DB : PostgreSQL

My project is a simple billing system which would help freelancers create, edit and print/send bills to customers.

Once my project is created and configured, I don’t know where to start.
Let’s say my first my first feature is to create a bill with a unique number and a title.

Question : What should I test first ?

  • the Domain layer with a createBill(String title) method which would generate a unique Serial Number ? I’d mock the DB layer.
  • the UI first, mocking the service layer ? I don’t know how to do it.

Thanks in advance for your answers,

Cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:05:47+00:00Added an answer on June 12, 2026 at 4:05 pm

    Start with a test 🙂

    What does your system do?

    public class BillingSystemTest {
        @Test
        public void generatesBills() {
            Bill bill = new BillingSystem().generate()
            assertNotNull(bill)
        }
    }
    

    First test complete!

    Make it pass, move on to the next test…

    @Test
    public void generatesAnInvoiceNumberForEachBill() {
        Bill bill = new BillingSystem().generate()
        assertEquals(1, bill.getNumber())
    }
    
    // ...and the next
    @Test
    public void generatesUniqueInvoiceNumbersForEachBill() {
        BillingSystem bs = new BillingSystem()
        assertEquals(1, bs.generate().getNumber())
        assertEquals(2, bs.generate().getNumber())
    }
    
    @Test
    public void generatesAnInvoiceSubjectWhenNoneIsSpecified() {
        Bill bill = new BillingSystem().generate()
        assertEquals("Invoice #1 from ACME Corp.", bill.getSubject())
    }
    
    @Test
    public void allowsForCustomSubjectsOnBills() {
        Bill bill = new BillingSystem().generate("Custom subject")
        assertEquals("Custom subject", bill.getSubject())
    }
    

    I’ve obviously skipped the refactoring steps here, but now that you have the tests, and the code that goes with it, you need to evaluate it for more opportunity. I’m imagining the code looking something like this.

    public class BillingSystem {
        private nextInvoiceNumber = 1;
    
        public Bill generate() {
            return generate("Invoice #" + nextInvoiceNumber + " from ACME Corp.");
        }
    
        public Bill generate(String subject) {
            Bill bill = new Bill(nextInvoiceNumber, subject)
            nextInvoiceNumber++
            return bill;
        }
    }
    

    Looking at this code, it seems ok, but may violate the Single Responsibility Principle (SRP). Here the BillingSystem generating a bill as well as managing the invoice number. This is an opportunity for refactoring. After the refactoring, your design may look something like this:

    public class BillingSystem {
        private InvoiceNumbering invoiceNumbering = new InvoiceNumbering()
    
        public Bill generate() {
            return generate("Invoice #" + invoiceNumbering.peekNext() + " from ACME Corp.");
        }
    
        public Bill generate(String subject) {
            Bill bill = new Bill(invoiceNumbering.generateNext(), subject)
            nextInvoiceNumber++
            return bill;
        }
    }
    

    Your design is better and your tests all pass. Next thing to do is refactor out the tests to remove the implementation details from them. They may end up looking something like:

    @Test
    public void generatesBills() {
        Bill bill = new BillingSystem().generate()
        assertNotNull(bill)
    }
    
    @Test
    public void generatesAnInvoiceNumberForEachBill() {
        // Using hand rolled mocks
        MockInvoiceNumbering in = new MockInvoiceNumbering()
        in.generateNextShouldReturn(4)
    
        Bill bill = new BillingSystem(in).generate()
        assertEquals(4, bill.getNumber())
    }
    
    @Test
    public void generatesUniqueInvoiceNumbersForEachBill() {
        MockInvoiceNumbering in = new MockInvoiceNumbering()
    
        BillingSystem bs = new BillingSystem(in)
    
        bs.generate();
        bs.generate();
    
        assertEquals(2, in.numberOfTimesGenerateNextWasCalled)
    }
    
    @Test
    public void generatesAnInvoiceSubjectWhenNoneIsSpecified() {
        Bill bill = new BillingSystem().generate()
        assertEquals("Invoice #1 from ACME Corp.", bill.getSubject())
    }
    
    @Test
    public void allowsForCustomSubjectsOnBills() {
        Bill bill = new BillingSystem().generate("Custom subject")
        assertEquals("Custom subject", bill.getSubject())
    }
    

    As a part of this refactoring, you would likely create some tests around your InvoiceNumbering class.

    Hope that’s enough of a start. Left a lot out. 🙂

    Hope that helps!

    Brandon

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to integrate testing into my development environment (as post-build step). I
I'm developing a testing suite based on Selenium 2. The goal is to test
Goal I am building an Eclipse plugin targeting the 3.7 environment and would like
Goal: to create a percentage column based off the values of calculated columns. Here's
My goal is writing a ContentProvider without an Activity. For testing I wrote a
I would like to build my own load testing tool in Java with the
I'm working on a project for a data structures class. The goal is to
I'm using Maven 3.0.3. For our project integration testing, we require a virtual frame
I have an iPhone project that uses GHUnit to conduct unit testing. Recently, I've
I have set up a maven project to use the jasmin-maven-plugin for testing my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.