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

  • Home
  • SEARCH
  • 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 8418223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:15:47+00:00 2026-06-10T02:15:47+00:00

I am trying to test the throwing of an exception in my unit test.

  • 0

I am trying to test the throwing of an exception in my unit test. I tried to metaclass the delete method, but it doesn’t want to stick. Can you tell from the code what I’m doing wrong?

Unit Test code:

@TestFor(ProductController)
@TestMixin(DomainClassUnitTestMixin)
class ProductControllerTests {     
  void testDeleteWithException() {
    mockDomain(Product, [[id: 1, name: "Test Product"]])
    Product.metaClass.delete = {-> throw new DataIntegrityViolationException("I'm an       exception")}
    controller.delete(1)
    assertEquals(view, '/show/edit')
}

Controller action code:

def delete(Long id) {
    def productInstance = Product.get(id)
    if (!productInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'product.label', default: 'Product'), id])
        redirect(action: "list")
        return
    }

    try {
        productInstance.delete(flush: true)
        flash.message = message(code: 'default.deleted.message', args: [message(code: 'product.label', default: 'Product'), id])
        redirect(action: "list")
    }
    catch (DataIntegrityViolationException e) {
        flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'product.label', default: 'Product'), id])
        redirect(action: "show", id: id)
    }
}    

When I run the test, productInstance.delete(flush: true) does not throw the exception I’m expecting. Instead it redirects to action: "list". Does anyone know how to override the Product.delete() method so I can force the exception?

  • 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-10T02:15:48+00:00Added an answer on June 10, 2026 at 2:15 am

    You’re mocking delete without any arguments, but your controller calls delete(flush: true). Try mocking out delete(Map) like this:

    Product.metaClass.delete = { Map params -> 
        throw new DataIntegrityViolationException("...")
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to test with JUnit4 a method throwing an exception. Here is the
I'm trying to test that I'm throwing an exception when appropriate. In my test
just trying to test for equality in this piece of code, but getting a
I trying to test an AccountController that uses DotNetOpenAuth but I am running into
I'm trying to test some mailers with rspec but deliveries are always empty. Here
I am trying to test a sinatra app using minitest and capybara but get
I just trying to pass some values but it's throwing an error all the
I am trying to figure out how to correctly and efficiently unit test my
The below script is trying to test the existence of lets say Utility.Method.PageInit(); the
I'm trying to set up unit tests for a card game application, but 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.