Blog Archives

Testing an abstract base class – code example

In the “How do I test an abstract base class if I can’t instantiate it?” post, you found out that you can actually test an abstract base class even though you can’t instantiate one.

The “all you need to do is” end of the post, however,

Read more ›

Posted in Unit Testing
Tags: , , , , , ,

How does my mock know what value I want?

It is early afternoon. You just started your job at Glamorous Inc. Your buddy this morning talked about unit tests. How they want all code to have unit tests but also have code that was written before they started doing that. And they want you to start by getting some of that code under test.

Read more ›

Posted in Unit Testing
Tags: , , , , , , , , , , ,

How do I make a method use a different class in unit tests?

The code you work on has this wonderful simple method. All it does is use a couple of other classes, passing through the parameters it received and setting some properties.

As it is such a simple function nobody bothered to write any tests for it. After all, “It only passes through to another class.”

Read more ›

Posted in Unit Testing
Tags: , , , , , ,

Why can’t my test access a public constructor?

It’s late in the afternoon. Almost time to go home. Just one issue to finish. A simple class that needs a couple of simple tests. Should be a breeze.

Five minutes later you are ready to bang your head against your desk. The error messages keep flying. And you can’t see why.

Read more ›

Posted in Unit Testing
Tags: , , , , , ,

How do I test an abstract base class if I can’t instantiate it?

You have a set of classes to write. Classes that have details specific to each, but that also have a lot in common.

You don’t like to repeat yourself and like your code DRY.

So you have created a base class to contain all the common code.

Read more ›

Posted in Unit Testing
Tags: , , , ,

How do I test an interface? Should I even do that?

Every so often this comes up: I have an interface. It has methods. I should unit test everything. So I should test this interface. But how? It doesn’t have an implementation…?!

Right…

Well…

Hmm…

No. Of course you don’t test the interface.

Read more ›

Posted in Unit Testing
Tags: , , , , , , ,

Is unit testing literal values pointless?

Well, yes. And no.

You shouldn’t be using literal values anyway. You should be using constants. And when you use constants everywhere, there is absolutely no point in testing whether a constant actually has the literal value assigned to it. No sense in testing the compiler. After all,

Read more ›

Posted in Unit Testing
Tags: ,

Show Buttons
Hide Buttons