Saturday 15 February 2014

java - How to mock an exception when creating an instance of a new class using Mockito -


Within a method, I have been caught with an exception that I want to fake.

I know to fake something to mimic the object. Some (), but I need to throw a remote exception when a class creates a new example of itself.

  Transient Cycle Bike = Faucet; Public Bicycle Mill (Cycle) (if (bike == blank) {try {bike = new bicycle (this);} grip (pre-remoteextension) {System.out.println ("no bike was found");}} return Bike;}   

I want to be able to duplicate everything in the block, but I do not understand how you make fun of building a new class, following line specific Is: bike = new bicycle (this);

I have many different options Ta tests have been tried, such as: b bicycle = bakery (bicycle category); mokito.doThrow (new remote expansion). When (b = new bicycle ()); < / Code>

Although I understand that this will work and is not working, I want to do something like this.

I have read the Mokitio docs and nothing has been found to be useful You can use a mono extension, powermoke in cases like this: / P>

You can do this The characters get permission to joke (see).

In this case, you will write something like the following test:

  @RunWith (PowerMockRunner.class) @ PrepareForTest ({ClassUnderTest.class, Bicycle.class}) Public Square Constructor Mockingstst {@Test Public Zero Receipt () {Classwise Test = New Classwise (); When new (cycle.class). Electrostatic (test) .thenThrow (new remote exception ()); Bicycle Cycle = Tested. Beta bicycle (); AssertNull (bicycle); }}   

More examples can be found here:

No comments:

Post a Comment