Home » Tag Archives: JUnit (page 8)

Tag Archives: JUnit

Spring MVC Controller JUnit Testing

spring-interview-questions-answers

JUnit testing Spring MVC controllers is not an easy task. But recently, a new project (to be included in Spring soon) offers new tools to facilitate this. This post illustrates how to test a simple controller via JUnit tests. This code is a variation of the code used in JUnit Testing Spring Service and DAO (with In-Memory Database). It is ...

Read More »

Enhancing Spring Test Framework with beforeClass and afterClass setup

spring-interview-questions-answers

How to allow instance methods to run as JUnit BeforeClass behavior JUnit allows you to setup methods on the class level once before and after all tests methods invocation. However, by design on purpose that they restrict this to only static methods using @BeforeClass and @AfterClass annotations. For example this simple demo shows the typical Junit setup: package deng.junitdemo; import ...

Read More »

Testing Custom Exceptions with JUnit’s ExpectedException and @Rule

junit-logo

Exception Testing Why test exception flows? Just like with all of your code, test coverage writes a contract between your code and the business functionality that the code is supposed to produce leaving you with a living documentation of the code along with the added ability to stress the functionality early and often. I won’t go into the many benefits ...

Read More »

JUnit Pass Test Case on Failures

junit-logo

Why create a mechanism to expect a test failure? There comes a time when one would want and expect a JUnit @Test case fail. Though this is pretty rare, it happens. I had the need to detect when a JUnit Test fails and then, if expected, to pass instead of fail. The specific case was that I was testing a ...

Read More »

Test Driven Traps, part 2

junit-logo

The Story of a Unit in Unit Tests In the previous part of this article, you could see some bad, though popular, test samples. But I’m not a professional critic (also known as a troll, or a hater), to grumble about without having anything constructive to say. Years of TDD have taught me more than just how bad the things ...

Read More »

Spring Testing Support and Context caching

spring-interview-questions-answers

Spring provides a comprehensive support for unit and integration testing – through annotations to load up a Spring application context, integrate with unit testing frameworks like JUnit and TestNG. Since loading up a large application context for every test takes time, Spring intelligently caches the application context for a test suite – typically when we execute tests for a project, ...

Read More »

Using PowerMock to Mock Constructors

powermock-logo

In my opinion, one of the main benefits of dependency injection is that you can inject mock and/or stub objects into your code in order to improve testability, increase test coverage and write better and more meaningful tests. There are those times, however, when you come across some legacy code that doesn’t use dependency injection and held together by composition ...

Read More »

5 Tips for Unit Testing Threaded Code

junit-logo

Here’s a few tips on how take make testing your code for logical correctness (as opposed to multi-threaded correctness). I find that there are essentially two stereotypical patterns with threaded code: Task orientated – many, short running, homogeneous tasks, often run within the Java 5 executor framework, Process orientated – few, long running, heterogeneous tasks, often event based (waiting on ...

Read More »

Tips for testing with Java

junit-logo

Introduction I love automated testing. In a rare diversion into op-ed1 I thought a put few thoughts (read – opinions) together. Before I start on how best to compose your tests, I briefly ask – What are the reasons for testing? Broadly, I think they are: Reduce total number of bugs / increase product stability Ensure software works as per ...

Read More »

Spring Integration – Session 2 – More Hello Worlds

spring-interview-questions-answers

This is a follow up to Spring Integration Session 1 The first session was a simple Hello World application using Spring Integration. I want to take it a little further by considering a few more scenarios around it. So the first change to the Hello World application is to add in a Gateway component. To quickly revisit the earlier test ...

Read More »
Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.