Monthly Archives: February 2025

Effective Exception Testing in JUnit 5: Boundary Value Testing and AssertThrows

Introduction

In modern software development, ensuring that a program handles exceptions correctly is crucial for building robust applications. Exception testing in JUnit 5 allows developers to verify that their code properly handles error scenarios, improving reliability and maintainability. This blog post explores key techniques such as Testing for Exceptions in JUnit 5, Boundary Value Testing, and using AssertThrows to create effective test cases.

Testing for Exceptions in JUnit 5

JUnit 5 provides a streamlined way to test exceptions in Java applications. Unlike JUnit 4, which required using the expected attribute or @Rule, JUnit 5 introduces Assertions.assertThrows(), offering a more flexible and readable approach.

Example of Exception Testing in JUnit 5

Consider a method that calculates the square root of a number. If a negative number is provided, it should throw an IllegalArgumentException.

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class MathUtilsTest {

    double calculateSquareRoot(double number) {
        if (number < 0) {
            throw new IllegalArgumentException("Number must be non-negative");
        }
        return Math.sqrt(number);
    }

    @Test
    void testCalculateSquareRootException() {
        IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
            calculateSquareRoot(-5);
        });
        assertEquals("Number must be non-negative", exception.getMessage());
    }
}

This test verifies that the method correctly throws an exception when given invalid input, ensuring robustness.

Boundary Value Testing

Boundary Value Testing (BVT) is a technique used to test the limits of input values. It focuses on edge cases, such as minimum and maximum values, where software is most likely to fail.

Example: Boundary Testing for Age Validation

Consider a function that validates a user’s age for registration, allowing only ages between 18 and 65.

boolean isValidAge(int age) {
    return age >= 18 && age <= 65;
}

Boundary tests should check values just inside and just outside the valid range:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class AgeValidatorTest {
    
    @Test
    void testAgeBoundaries() {
        assertTrue(isValidAge(18)); 
        assertTrue(isValidAge(65));  
        assertFalse(isValidAge(17)); 
        assertFalse(isValidAge(66)); 
    }
}

BVT ensures the system correctly distinguishes between valid and invalid inputs.

Testing for Exceptions with AssertThrows

The assertThrows method in JUnit 5 simplifies exception testing, making tests more readable and maintainable. It helps validate that methods correctly handle invalid inputs by throwing the expected exceptions.

Example: Division by Zero Handling

Consider a simple method that performs division:

int divide(int dividend, int divisor) {
    if (divisor == 0) {
        throw new ArithmeticException("Cannot divide by zero");
    }
    return dividend / divisor;
}

We can use assertThrows to verify proper exception handling:

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class DivisionTest {

    @Test
    void testDivideByZero() {
        ArithmeticException exception = assertThrows(ArithmeticException.class, () -> {
            divide(10, 0);
        });
        assertEquals("Cannot divide by zero", exception.getMessage());
    }
}

This ensures that the division method correctly throws an exception when dividing by zero.

Conclusion

Testing exceptions effectively is a vital part of software quality assurance. JUnit 5’s assertThrows method, combined with Boundary Value Testing, enables developers to create thorough test cases that improve the reliability and robustness of applications. By writing well-structured exception tests, developers can prevent unexpected failures and ensure their applications behave as expected under various conditions.

For further reading, check out:

#JUnit5 #ExceptionTesting #AssertThrows #BoundaryValueTesting

From the blog Rick’s Software Journal by RickDjouwe1 and used with permission of the author. All other rights reserved by the author.

On Being a Software Apprentice

 In this post, I’ll be discussing my thoughts primarily on Chapter 1 of Apprenticeship Patterns. I really like the idea of being a “Software Apprentice,” as I think it is a good representation of the attitude I have adopted over the years. I’ve always viewed myself as someone who is constantly learning and evolving in this field, and the apprenticeship model seems to align perfectly with that. It’s not about arriving at some endpoint but about growing, making mistakes, and improving.

What stood out to me the most was the idea of viewing the development process as a journey, not a destination. Chapter 1 introduces the concept of “Software Craftsmanship” as more than just technical skill, but also as a mindset of continuous learning. This was particularly thought-provoking because I think there’s a tendency, especially early in our careers, to think of coding as a task to be completed or a skill to be mastered and then moved beyond while in pursuit of some computer science career. However, the idea that becoming a Software Craftsman is an ongoing process—a commitment to constant improvement and a mindset that thrives on learning—is something that I think is a better description. It shifted my perspective on what it means to be good at coding- it’s not enough to know how to do something today, it’s about always finding a way to improve your code in the future.

In the past, specifically in my career, I’ve often gotten caught up in the day-to-day rush of just getting whatever piece of code done. I’ve rushed to get things coded, with the focus being on completing projects rather than developing deeper expertise or refining my approach to coding. After reading this chapter, I think I’m more likely to focus on the long-term value of writing quality code and the importance of standards. It’s not just about writing code that works; it’s about writing code that lasts, is readable, and contributes to a more sustainable project. I’m going to start to approach my coding tasks with a deeper goal, rather than rushing through them for the sake of completion.

As for the chapters that seem most relevant to me, I’d say Chapter 2 and Chapter 3 are particularly interesting. Chapter 2 addresses the Apprentice phase, which is where I find myself right now. It talks about how to build your foundational skills, seek out learning opportunities, and adopt the mindset of a lifelong learner – things I’m actively working on. Chapter 3, which discusses the Journeyman phase, is also relevant to me because it’s about expanding your skill set and finding ways to deepen your experience. I’m interested in how this phase encourages finding ways to take on more responsibility and become a more well-rounded developer, which is exactly where I want to focus next in my development journey.

From the blog Mr. Lancer 987&#039;s Blog by Mr. Lancer 987 and used with permission of the author. All other rights reserved by the author.

On Being a Software Apprentice

 In this post, I’ll be discussing my thoughts primarily on Chapter 1 of Apprenticeship Patterns. I really like the idea of being a “Software Apprentice,” as I think it is a good representation of the attitude I have adopted over the years. I’ve always viewed myself as someone who is constantly learning and evolving in this field, and the apprenticeship model seems to align perfectly with that. It’s not about arriving at some endpoint but about growing, making mistakes, and improving.

What stood out to me the most was the idea of viewing the development process as a journey, not a destination. Chapter 1 introduces the concept of “Software Craftsmanship” as more than just technical skill, but also as a mindset of continuous learning. This was particularly thought-provoking because I think there’s a tendency, especially early in our careers, to think of coding as a task to be completed or a skill to be mastered and then moved beyond while in pursuit of some computer science career. However, the idea that becoming a Software Craftsman is an ongoing process—a commitment to constant improvement and a mindset that thrives on learning—is something that I think is a better description. It shifted my perspective on what it means to be good at coding- it’s not enough to know how to do something today, it’s about always finding a way to improve your code in the future.

In the past, specifically in my career, I’ve often gotten caught up in the day-to-day rush of just getting whatever piece of code done. I’ve rushed to get things coded, with the focus being on completing projects rather than developing deeper expertise or refining my approach to coding. After reading this chapter, I think I’m more likely to focus on the long-term value of writing quality code and the importance of standards. It’s not just about writing code that works; it’s about writing code that lasts, is readable, and contributes to a more sustainable project. I’m going to start to approach my coding tasks with a deeper goal, rather than rushing through them for the sake of completion.

As for the chapters that seem most relevant to me, I’d say Chapter 2 and Chapter 3 are particularly interesting. Chapter 2 addresses the Apprentice phase, which is where I find myself right now. It talks about how to build your foundational skills, seek out learning opportunities, and adopt the mindset of a lifelong learner – things I’m actively working on. Chapter 3, which discusses the Journeyman phase, is also relevant to me because it’s about expanding your skill set and finding ways to deepen your experience. I’m interested in how this phase encourages finding ways to take on more responsibility and become a more well-rounded developer, which is exactly where I want to focus next in my development journey.

From the blog Mr. Lancer 987&#039;s Blog by Mr. Lancer 987 and used with permission of the author. All other rights reserved by the author.

On Being a Software Apprentice

 In this post, I’ll be discussing my thoughts primarily on Chapter 1 of Apprenticeship Patterns. I really like the idea of being a “Software Apprentice,” as I think it is a good representation of the attitude I have adopted over the years. I’ve always viewed myself as someone who is constantly learning and evolving in this field, and the apprenticeship model seems to align perfectly with that. It’s not about arriving at some endpoint but about growing, making mistakes, and improving.

What stood out to me the most was the idea of viewing the development process as a journey, not a destination. Chapter 1 introduces the concept of “Software Craftsmanship” as more than just technical skill, but also as a mindset of continuous learning. This was particularly thought-provoking because I think there’s a tendency, especially early in our careers, to think of coding as a task to be completed or a skill to be mastered and then moved beyond while in pursuit of some computer science career. However, the idea that becoming a Software Craftsman is an ongoing process—a commitment to constant improvement and a mindset that thrives on learning—is something that I think is a better description. It shifted my perspective on what it means to be good at coding- it’s not enough to know how to do something today, it’s about always finding a way to improve your code in the future.

In the past, specifically in my career, I’ve often gotten caught up in the day-to-day rush of just getting whatever piece of code done. I’ve rushed to get things coded, with the focus being on completing projects rather than developing deeper expertise or refining my approach to coding. After reading this chapter, I think I’m more likely to focus on the long-term value of writing quality code and the importance of standards. It’s not just about writing code that works; it’s about writing code that lasts, is readable, and contributes to a more sustainable project. I’m going to start to approach my coding tasks with a deeper goal, rather than rushing through them for the sake of completion.

As for the chapters that seem most relevant to me, I’d say Chapter 2 and Chapter 3 are particularly interesting. Chapter 2 addresses the Apprentice phase, which is where I find myself right now. It talks about how to build your foundational skills, seek out learning opportunities, and adopt the mindset of a lifelong learner – things I’m actively working on. Chapter 3, which discusses the Journeyman phase, is also relevant to me because it’s about expanding your skill set and finding ways to deepen your experience. I’m interested in how this phase encourages finding ways to take on more responsibility and become a more well-rounded developer, which is exactly where I want to focus next in my development journey.

From the blog Mr. Lancer 987&#039;s Blog by Mr. Lancer 987 and used with permission of the author. All other rights reserved by the author.

On Being a Software Apprentice

 In this post, I’ll be discussing my thoughts primarily on Chapter 1 of Apprenticeship Patterns. I really like the idea of being a “Software Apprentice,” as I think it is a good representation of the attitude I have adopted over the years. I’ve always viewed myself as someone who is constantly learning and evolving in this field, and the apprenticeship model seems to align perfectly with that. It’s not about arriving at some endpoint but about growing, making mistakes, and improving.

What stood out to me the most was the idea of viewing the development process as a journey, not a destination. Chapter 1 introduces the concept of “Software Craftsmanship” as more than just technical skill, but also as a mindset of continuous learning. This was particularly thought-provoking because I think there’s a tendency, especially early in our careers, to think of coding as a task to be completed or a skill to be mastered and then moved beyond while in pursuit of some computer science career. However, the idea that becoming a Software Craftsman is an ongoing process—a commitment to constant improvement and a mindset that thrives on learning—is something that I think is a better description. It shifted my perspective on what it means to be good at coding- it’s not enough to know how to do something today, it’s about always finding a way to improve your code in the future.

In the past, specifically in my career, I’ve often gotten caught up in the day-to-day rush of just getting whatever piece of code done. I’ve rushed to get things coded, with the focus being on completing projects rather than developing deeper expertise or refining my approach to coding. After reading this chapter, I think I’m more likely to focus on the long-term value of writing quality code and the importance of standards. It’s not just about writing code that works; it’s about writing code that lasts, is readable, and contributes to a more sustainable project. I’m going to start to approach my coding tasks with a deeper goal, rather than rushing through them for the sake of completion.

As for the chapters that seem most relevant to me, I’d say Chapter 2 and Chapter 3 are particularly interesting. Chapter 2 addresses the Apprentice phase, which is where I find myself right now. It talks about how to build your foundational skills, seek out learning opportunities, and adopt the mindset of a lifelong learner – things I’m actively working on. Chapter 3, which discusses the Journeyman phase, is also relevant to me because it’s about expanding your skill set and finding ways to deepen your experience. I’m interested in how this phase encourages finding ways to take on more responsibility and become a more well-rounded developer, which is exactly where I want to focus next in my development journey.

From the blog Mr. Lancer 987&#039;s Blog by Mr. Lancer 987 and used with permission of the author. All other rights reserved by the author.

On Being a Software Apprentice

 In this post, I’ll be discussing my thoughts primarily on Chapter 1 of Apprenticeship Patterns. I really like the idea of being a “Software Apprentice,” as I think it is a good representation of the attitude I have adopted over the years. I’ve always viewed myself as someone who is constantly learning and evolving in this field, and the apprenticeship model seems to align perfectly with that. It’s not about arriving at some endpoint but about growing, making mistakes, and improving.

What stood out to me the most was the idea of viewing the development process as a journey, not a destination. Chapter 1 introduces the concept of “Software Craftsmanship” as more than just technical skill, but also as a mindset of continuous learning. This was particularly thought-provoking because I think there’s a tendency, especially early in our careers, to think of coding as a task to be completed or a skill to be mastered and then moved beyond while in pursuit of some computer science career. However, the idea that becoming a Software Craftsman is an ongoing process—a commitment to constant improvement and a mindset that thrives on learning—is something that I think is a better description. It shifted my perspective on what it means to be good at coding- it’s not enough to know how to do something today, it’s about always finding a way to improve your code in the future.

In the past, specifically in my career, I’ve often gotten caught up in the day-to-day rush of just getting whatever piece of code done. I’ve rushed to get things coded, with the focus being on completing projects rather than developing deeper expertise or refining my approach to coding. After reading this chapter, I think I’m more likely to focus on the long-term value of writing quality code and the importance of standards. It’s not just about writing code that works; it’s about writing code that lasts, is readable, and contributes to a more sustainable project. I’m going to start to approach my coding tasks with a deeper goal, rather than rushing through them for the sake of completion.

As for the chapters that seem most relevant to me, I’d say Chapter 2 and Chapter 3 are particularly interesting. Chapter 2 addresses the Apprentice phase, which is where I find myself right now. It talks about how to build your foundational skills, seek out learning opportunities, and adopt the mindset of a lifelong learner – things I’m actively working on. Chapter 3, which discusses the Journeyman phase, is also relevant to me because it’s about expanding your skill set and finding ways to deepen your experience. I’m interested in how this phase encourages finding ways to take on more responsibility and become a more well-rounded developer, which is exactly where I want to focus next in my development journey.

From the blog Mr. Lancer 987&#039;s Blog by Mr. Lancer 987 and used with permission of the author. All other rights reserved by the author.

On Being a Software Apprentice

 In this post, I’ll be discussing my thoughts primarily on Chapter 1 of Apprenticeship Patterns. I really like the idea of being a “Software Apprentice,” as I think it is a good representation of the attitude I have adopted over the years. I’ve always viewed myself as someone who is constantly learning and evolving in this field, and the apprenticeship model seems to align perfectly with that. It’s not about arriving at some endpoint but about growing, making mistakes, and improving.

What stood out to me the most was the idea of viewing the development process as a journey, not a destination. Chapter 1 introduces the concept of “Software Craftsmanship” as more than just technical skill, but also as a mindset of continuous learning. This was particularly thought-provoking because I think there’s a tendency, especially early in our careers, to think of coding as a task to be completed or a skill to be mastered and then moved beyond while in pursuit of some computer science career. However, the idea that becoming a Software Craftsman is an ongoing process—a commitment to constant improvement and a mindset that thrives on learning—is something that I think is a better description. It shifted my perspective on what it means to be good at coding- it’s not enough to know how to do something today, it’s about always finding a way to improve your code in the future.

In the past, specifically in my career, I’ve often gotten caught up in the day-to-day rush of just getting whatever piece of code done. I’ve rushed to get things coded, with the focus being on completing projects rather than developing deeper expertise or refining my approach to coding. After reading this chapter, I think I’m more likely to focus on the long-term value of writing quality code and the importance of standards. It’s not just about writing code that works; it’s about writing code that lasts, is readable, and contributes to a more sustainable project. I’m going to start to approach my coding tasks with a deeper goal, rather than rushing through them for the sake of completion.

As for the chapters that seem most relevant to me, I’d say Chapter 2 and Chapter 3 are particularly interesting. Chapter 2 addresses the Apprentice phase, which is where I find myself right now. It talks about how to build your foundational skills, seek out learning opportunities, and adopt the mindset of a lifelong learner – things I’m actively working on. Chapter 3, which discusses the Journeyman phase, is also relevant to me because it’s about expanding your skill set and finding ways to deepen your experience. I’m interested in how this phase encourages finding ways to take on more responsibility and become a more well-rounded developer, which is exactly where I want to focus next in my development journey.

From the blog Mr. Lancer 987&#039;s Blog by Mr. Lancer 987 and used with permission of the author. All other rights reserved by the author.

On Being a Software Apprentice

 In this post, I’ll be discussing my thoughts primarily on Chapter 1 of Apprenticeship Patterns. I really like the idea of being a “Software Apprentice,” as I think it is a good representation of the attitude I have adopted over the years. I’ve always viewed myself as someone who is constantly learning and evolving in this field, and the apprenticeship model seems to align perfectly with that. It’s not about arriving at some endpoint but about growing, making mistakes, and improving.

What stood out to me the most was the idea of viewing the development process as a journey, not a destination. Chapter 1 introduces the concept of “Software Craftsmanship” as more than just technical skill, but also as a mindset of continuous learning. This was particularly thought-provoking because I think there’s a tendency, especially early in our careers, to think of coding as a task to be completed or a skill to be mastered and then moved beyond while in pursuit of some computer science career. However, the idea that becoming a Software Craftsman is an ongoing process—a commitment to constant improvement and a mindset that thrives on learning—is something that I think is a better description. It shifted my perspective on what it means to be good at coding- it’s not enough to know how to do something today, it’s about always finding a way to improve your code in the future.

In the past, specifically in my career, I’ve often gotten caught up in the day-to-day rush of just getting whatever piece of code done. I’ve rushed to get things coded, with the focus being on completing projects rather than developing deeper expertise or refining my approach to coding. After reading this chapter, I think I’m more likely to focus on the long-term value of writing quality code and the importance of standards. It’s not just about writing code that works; it’s about writing code that lasts, is readable, and contributes to a more sustainable project. I’m going to start to approach my coding tasks with a deeper goal, rather than rushing through them for the sake of completion.

As for the chapters that seem most relevant to me, I’d say Chapter 2 and Chapter 3 are particularly interesting. Chapter 2 addresses the Apprentice phase, which is where I find myself right now. It talks about how to build your foundational skills, seek out learning opportunities, and adopt the mindset of a lifelong learner – things I’m actively working on. Chapter 3, which discusses the Journeyman phase, is also relevant to me because it’s about expanding your skill set and finding ways to deepen your experience. I’m interested in how this phase encourages finding ways to take on more responsibility and become a more well-rounded developer, which is exactly where I want to focus next in my development journey.

From the blog Mr. Lancer 987&#039;s Blog by Mr. Lancer 987 and used with permission of the author. All other rights reserved by the author.

On Being a Software Apprentice

 In this post, I’ll be discussing my thoughts primarily on Chapter 1 of Apprenticeship Patterns. I really like the idea of being a “Software Apprentice,” as I think it is a good representation of the attitude I have adopted over the years. I’ve always viewed myself as someone who is constantly learning and evolving in this field, and the apprenticeship model seems to align perfectly with that. It’s not about arriving at some endpoint but about growing, making mistakes, and improving.

What stood out to me the most was the idea of viewing the development process as a journey, not a destination. Chapter 1 introduces the concept of “Software Craftsmanship” as more than just technical skill, but also as a mindset of continuous learning. This was particularly thought-provoking because I think there’s a tendency, especially early in our careers, to think of coding as a task to be completed or a skill to be mastered and then moved beyond while in pursuit of some computer science career. However, the idea that becoming a Software Craftsman is an ongoing process—a commitment to constant improvement and a mindset that thrives on learning—is something that I think is a better description. It shifted my perspective on what it means to be good at coding- it’s not enough to know how to do something today, it’s about always finding a way to improve your code in the future.

In the past, specifically in my career, I’ve often gotten caught up in the day-to-day rush of just getting whatever piece of code done. I’ve rushed to get things coded, with the focus being on completing projects rather than developing deeper expertise or refining my approach to coding. After reading this chapter, I think I’m more likely to focus on the long-term value of writing quality code and the importance of standards. It’s not just about writing code that works; it’s about writing code that lasts, is readable, and contributes to a more sustainable project. I’m going to start to approach my coding tasks with a deeper goal, rather than rushing through them for the sake of completion.

As for the chapters that seem most relevant to me, I’d say Chapter 2 and Chapter 3 are particularly interesting. Chapter 2 addresses the Apprentice phase, which is where I find myself right now. It talks about how to build your foundational skills, seek out learning opportunities, and adopt the mindset of a lifelong learner – things I’m actively working on. Chapter 3, which discusses the Journeyman phase, is also relevant to me because it’s about expanding your skill set and finding ways to deepen your experience. I’m interested in how this phase encourages finding ways to take on more responsibility and become a more well-rounded developer, which is exactly where I want to focus next in my development journey.

From the blog Mr. Lancer 987&#039;s Blog by Mr. Lancer 987 and used with permission of the author. All other rights reserved by the author.

Apprenticeship Patterns : Software Craftsmanship

The first chapter of the book Apprenticeship Patterns describes what the authors call The first chapter of the book Apprenticeship Patterns describes what the authors call “Software Craftsmanship”. While the general definition came from medieval times and relates to guilds and master/apprentice relationships, the definition used by the authors comes from a combination of these medieval principals and Peter McBreen. Peter’s definition starts from craftsmanship being opposite of the engineering and scientific approach to computer science. This view of craftsmanship being alternative to program, however the authors hold the opinion that both approaches are part of an ideal software dev landscape. I found this hard to wrap my head around the first go around but I hope that as I continue here readers may also get a better grasp of this “craftsmanship” mindset.

First is the manifesto. Each idea is taken from one of many other highly skilled people who were interviewed by the authors.

  1. Have a growth mindset – there is always a better solution and the only way to get there is to be prepared and work for it. Chiefly, this effort is what makes you smart and talented. I love this idea as it’s a brighter outlook on the world compared to the “talent is born and failure proves lack of talent” crowd.
  2. Accept that you will lack something and pursue the solution – this one is hard for me personally. I tend not to admit my lacking at first or second blush. However, once it is clear that I am ill-informed/confused/missing something I will search relentlessly until I find the proof I am wrong/the reason my thought process was flawed because going through life without a proper set of logic to solve problems will lead to a tough time. As such, I strongly agree with this tenet.
  3. Favor pragmatism over dogmatism – this is a doozy for me because I really love perfectionism. To be clear, this tenet is 100% true, no doubt in my mind. However, knowing that there exists a better way to do something is always hard to square with pragmatism. I know my code hits the necessary parameters, but it doesn’t do everything as efficiently as possible and that makes me want to tweak my code in so many ways.
  4. Favor sharing information over hoarding – I understand this idea and mostly agree to this ideal. I still think there is room for some level of hoarding in some cases, not everyone is a saint and some tools are definitely dangerous in the wrong scenarios.
  5. You must be willing to try and fail – I find this to be easy to follow, maybe because it ties in with a lack of knowledge on my part meaning most projects I work on are about techniques and knowledge I do not have.

I will continue posting more about this chapter later this week as there are a few more tenets to dissect and they are an interesting perusal. This first chapter is truly an interesting read and I hope these ideals don’t find themselves turned into corporate rule sets.

From the blog Coder&#039;s First Steps by amoulton2 and used with permission of the author. All other rights reserved by the author.