How can I ensure that a particular exception is thrown in JUnit5? - java

With JUnit4 you could e.g. just write:
#Test (expectedException = new UnsupportedOperationException()){...}
How is this possible in JUnit5? I tried this way, but I'm not sure if this is equal.
#Test
public void testExpectedException() {
Assertions.assertThrows(UnsupportedOperationException.class, () -> {
Integer.parseInt("One");});

Yes, those are equivalent.
public class DontCallAddClass {
public void add() {
throws UnsupportedOperationException("You are not supposed to call me!");
}
}
public class DontCallAddClassTest {
private DontCallAddClass dontCallAdd = new DontCallAddClass();
#Rule
public ExpectedException exception = ExpectedException.none();
#Test
public void add_throwsException() {
exception.expect(UnsupportedOperationException.class);
dontCallAdd.add();
}
#Test(expected = UnsupportedOperationException.class)
public void add_throwsException_differentWay() {
dontCallAdd.add();
}
#Test
public void add_throwsException() {
Assertions.assertThrows(UnsupportedOperationException.class, dontCallAdd.add());
}
}
The three test methods above are quivalent. In Junit 5 use the last one. It's the newer approach. It also allows you to take advantage of Java 8 lambdas. You can also checks for what the error message should be. See below:
public class DontCallAddClass {
public void add() {
throws UnsupportedOperationException("You are not supposed to call me!");
}
}
public class DontCallAddClassTest {
private DontCallAddClass dontCallAdd = new DontCallAddClass();
#Rule
public ExpectedException exception = ExpectedException.none();
#Test
public void add_throwsException() {
exception.expect(UnsupportedOperationException.class);
exception.expectMessage("You are not supposed to call me!");
dontCallAdd.add();
}
// this one doesn't check for error message :(
#Test(expected = UnsupportedOperationException.class)
public void add_throwsException_differentWay() {
dontCallAdd.add();
}
#Test
public void add_throwsException() {
Assertions.assertThrows(UnsupportedOperationException.class, dontCallAdd.add(), "You are not supposed to call me!");
}
}
check there for more information: JUnit 5: How to assert an exception is thrown?
Hope this clear it up

Related

How to unit test logic in an inner class using Mockito / Powermockito

I have the following class to test.
public class ClassToTest {
private View view;
public ClassToTest(View view) {
this.view = view;
}
public void init() {
view.add(new ParamClass.OnSomething() {
#Override
public void onSomethingElse() {
view.doSomeWork();
}
});
}
}
Where view is
public class View {
public void add(OnSomething onSomething) {
}
public void doSomeWork() {
}
}
I have mocked view object, but no clue how to test the logic marked "Some logic here to test"
ParamClass is final.
public final class ParamClass {
public interface onSomething {
public void onSomethingElse();
}
}
Without making change to source, is there a way to unit test this using Mockito / Powermockito?
I'm trying to verify the invocation of doSomeWork()
#RunWith(PowerMockRunner.class)
#PrepareForTest({ ClassToTest.class })
public class TestClass {
#Mock View view;
#Before
public void init() {
MockitoAnnotations.initMocks(this);
}
#Test
public void test() throws Exception {
ClassToTest classToTest = new ClassToTest(view);
PowerMockito.doCallRealMethod().when(view).add(Mockito.any(ParamClass.OnSomething.class));
PowerMockito.whenNew(ParamClass.OnSomething.class)
.withAnyArguments()
.thenReturn(new ParamClass.OnSomething() {
#Override
public void onSomethingElse() {
view.doSomeWork();
}
});
classToTest.init();
Mockito.verify(view, Mockito.times(1)).doSomeWork();
}
}
Its throwing exception
java.lang.ArrayIndexOutOfBoundsException: 0
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withAnyArguments(DefaultConstructorExpectationSetup.java:66)
at com.linkedin.android.lite.animations.TestClass.test(TestClass.java:29)
I figured out how to do it. Updating the answer.
public class TestClass {
#Mock View view;
#Before
public void init() {
MockitoAnnotations.initMocks(this);
}
#Test
public void test() throws Exception {
ClassToTest classToTest = new ClassToTest(view);
classToTest.init();
ArgumentCaptor<ParamClass.OnSomething> captor =
ArgumentCaptor.forClass(ParamClass.OnSomething.class);
verify(view).add(captor.capture());
ParamClass.OnSomething onSomething = captor.getValue();
onSomething.onSomethingElse();
Mockito.verify(view, Mockito.times(1)).doSomeWork();
}
}
Well what are you exactly trying to test? If there is code inside onSomethingElse that calls other objects/methods then you can simply mock all those other calls and then use Mockito.verify(mockObjectHere).methodCalled() on those methods that were called, if you just want to implement testing for behaviour.

writing a JUnit Test case for custom checked exception in Java?

I am writing a test case for my class that has methods which throw exceptions (both checked and runtime). I have tried different possible ways of testing as suggested in this link.. It appears they seem to work only for runtime exceptions. for Checked exceptions, I need to do a try/catch/assert as shown in the code below. Is there any alternatives to try/catch/assert/. you will notice that testmethod2() and testmethod2_1() shows compile error but testmethod2_2() does not show compile error which uses try/catch.
class MyException extends Exception {
public MyException(String message){
super(message);
}
}
public class UsualStuff {
public void method1(int i) throws IllegalArgumentException{
if (i<0)
throw new IllegalArgumentException("value cannot be negative");
System.out.println("The positive value is " + i );
}
public void method2(int i) throws MyException {
if (i<10)
throw new MyException("value is less than 10");
System.out.println("The value is "+ i);
}
}
Test class:
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class UsualStuffTest {
private UsualStuff u;
#Before
public void setUp() throws Exception {
u = new UsualStuff();
}
#Rule
public ExpectedException exception = ExpectedException.none();
#Test(expected = IllegalArgumentException.class)
public void testMethod1() {
u.method1(-1);
}
#Test(expected = MyException.class)
public void testMethod2() {
u.method2(9);
}
#Test
public void testMethod2_1(){
exception.expect(MyException.class);
u.method2(3);
}
public void testMethod2_3(){
try {
u.method2(5);
} catch (MyException e) {
assertEquals(e.getMessage(), "value is less than 10") ;
}
}
}
#Test(expected = MyException.class)
public void testMethod2() throws MyException {
u.method2(9);
}

Use old instance in JUnit test

I try to use different instances for my tests but the first one is always used.
During the second test, it's the content of the first instance that is displayed.
I don't know where to look for.
public class MyActivityTest extends
ActivityInstrumentationTestCase2<MyActivity> {
private Solo solo;
public MyActivityTest() {
super(MyActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
Authentication.setSessionId("mysessionid", this.getInstrumentation()
.getTargetContext().getApplicationContext());
solo = new Solo(getInstrumentation(), getActivity());
}
public void testFailFetching() {
CommunicationFactory.setInstance(MyActivityData.FALSE_QUIZCOMM_DEFAULT);
//some Solo tests
}
public void testSucceedFetching() {
CommunicationFactory.setInstance(MyActivity.CORRECT_QUIZCOMM_DEFAULT);
//some Solo tests
}
#Override
protected void tearDown() throws Exception {
CommunicationFactory.setInstance(null);
super.tearDown();
}
}
The setUp() method will be called before each test. Provided it completes without throwing an exception (which presumably would abort your tests anyway), your solo variable is being reconstructed for each test. The follow test code demonstrates this:
public class ExampleTest extends TestCase {
private static int num = 1;
private Foo foo;
#Override
protected void setUp() throws Exception {
super.setUp();
foo = new Foo(num++);
}
public void testA() {
foo.printNum();
}
public void testB() {
foo.printNum();
}
private static class Foo {
private final int num;
public Foo(int num) {
this.num = num;
}
public void printNum() {
System.out.println(num);
}
}
}
This prints:
1
2
It's possible the Solo objects equal each other in the separate tests. But they won't be the same object.

Setting up a test system with "real data"

So right now I'm using JUnit 4 and in the #BeforeClass methods I setup everything needed to reset the user schema or to prepare sample data.
Now, it's not that I don't like this approach but I found it quite frustrating for the following reason:
I'm using the Parameterized annotation to run the very same tests with different input data. Parameterized doesn't work on #BeforeClass because #BeforeClass works with a static method.
This means I have to replicate tests if I want to keep the #BeforeClass logic. I can't use #After and #Before because those will happen after every test and it would be an overhead.
I was thinking I could refactor this Unit Tests in the sense that I'll write an abstract class that handles the test and a subclass for every group parameters I want to try so that I can have the test code written only once.
I'm hoping you can suggest a cleaner option with the following starting point: the use of #Parameterized, the need to run the "database" method only once per parameter group.
EDIT:
this is an example of my class without the BeforeClass
RunWith(LabelledParameterized.class)
public class TestCreateCampaign extends AbstractTestSubscriberCampaign {
public TestCreateCampaign(String label, String apiKey, String userKey,
int customerId) {
super(label, apiKey, userKey, customerId);
}
#Before
public void setUp() throws Exception {
super.setUp();
}
#After
public void tearDown() throws Exception {
super.tearDown();
}
#Parameters
public static Collection<Object[]> generatedData() {
return DataProvider.generatedCorrectSubscriberData();
}
#Test
public void testCreateEmailCampaignBothTriggered() {
// TEST
}
#Test
public void testCreateTextCampaignTriggered() {
// TEST
}
#Test
public void testCreateTextCampaignTest() {
// TEST
}
// Other Tests
}
This depends on how you want to set up your classes, but you can use a ClassRule for this. This does the same job as a TestRule, but it runs once for each class, rather than each test. This can be combined with Parameterized and TestRule, such as:
#RunWith(Parameterized.class)
public class TestCreateCampaign {
#ClassRule
public static ExternalResource beforeAfterClass = new ExternalResource() {
#Override
protected void before() throws Throwable {
System.out.println("before each class");
}
#Override
protected void after() {
System.out.println("after each class");
}
};
#Rule
public ExternalResource beforeAfter = new ExternalResource() {
#Override
protected void before() throws Throwable {
System.out.println("before each test");
}
#Override
protected void after() {
System.out.println("after each test");
}
};
#Parameters(name = "{index}: fib({0})={1}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] { { 3, 0 }, { 4, 1 } });
}
private int fInput;
private int fExpected;
public TestCreateCampaign(int input, int expected) {
fInput = input;
fExpected = expected;
}
#Test
public void test1() {
System.out.println("test1 fInput=" + fInput);
}
}
This produces the following output:
before each class
before each test
test1 3
after each test
before each test
test1 4
after each test
after each class
This seems to be what you're looking for. To cut down on the amount of duplication, you can of course define beforeAfterClass and beforeAfter in a separate java class.
These are available in JUnit 4.9+.
What about calling your setup method from the constructor of your parameterized test class?
Edit:
OK, do I don't know of anything that does this automatically, but I think you could code up a Rule to do it. You could either implement a Rule from scratch of extend ExternalResource. Here is what I think it would do.
The constructor would take an instance of the test class and an ExternalResource instance.
In the constructor it would find the list of methods that contain the #Test annotation a get a count. It would set an iteration count to 0.
In the before method it would increment the iteration count and if it is 1 after increment (or 0 before) it would invoke the before method on the passed ExternalResource.
In the after method it would check to see if the iteration count was equal to the number of tests and if so call the after method on the passed ExternalResource.
You might need to use a different callback class / interface and ExternalResource since the before and after methods are protected. If you really wanted to be cool, you would define your own BeforeParameters and AfterParameter annotations in your rule and it would look for those methods in the passed instance.
If you develop this please post it or submit it to JUnit for inclusion.
Here is what I came up with, not as nice as I would like:
#RunWith(Parameterized.class)
public class TestExample {
private interface BeforeAfter {
void before();
void after();
}
public static class Resource extends ExternalResource {
private final int count;
private final BeforeAfter ba;
private int iteration = 0;
Resource(Object instance, BeforeAfter ba) {
int localCount = 0;
for (Method method : instance.getClass().getMethods()) {
if (method.getAnnotation(Test.class) != null) {
localCount++;
}
}
this.count = localCount;
this.ba = ba;
}
#Override
protected void before() throws Throwable {
if (iteration == 0) {
ba.before();
}
iteration++;
}
#Override
protected void after() {
if (iteration == count) {
ba.after();
iteration = 0;
}
}
}
#Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] { { 3, 0 }, { 4, 1 } });
}
#Rule
public static Resource resource = new Resource(new TestExample(0, 0), new BeforeAfter() {
#Override
public void before() {
System.out.println("setup");
}
#Override
public void after() {
System.out.println("cleanup");
}
});
private int fInput;
private int fExpected;
public TestExample(int input, int expected) {
// System.out.println("Constructor invoked" + fInput);
fInput = input;
fExpected = expected;
}
#Test
public void test1() {
System.out.println("test1 fInput=" + fInput);
}
#Test
public void test2() {
System.out.println("test2 fInput=" + fInput);
}
}
Resulted in:
setup
test1 fInput=3
test2 fInput=3
cleanup
setup
test1 fInput=4
test2 fInput=4
cleanup
See How to load DBUnit test data once per case with Spring Test for a way of initialising your test data only once per test run.

Log information inside a JUnit Suite

I'm currently trying to write inside a log file the total number of failed tests from a JUnite Suite.
My testsuite is defined as follows:
#RunWith(Suite.class)
#SuiteClasses({Class1.class, Class2.class etc.})
public class SimpleTestSuite {}
I tried to define a rule which would increase the total number of errors when a test fails, but apparently my rule is never called.
#Rule
public MethodRule logWatchRule = new TestWatchman() {
public void failed(Throwable e, FrameworkMethod method) {
errors += 1;
}
public void succeeded(FrameworkMethod method) {
}
};
Any ideas on what I should to do to achieve this behaviour?
The following code works for me. I suspect you are not declaring errors as static.
Consider that JUnit creates a new instance of the fixture before exercising each test method.
public class WatchmanExample {
private static int failedCount = 0;
#Rule
public MethodRule watchman = new TestWatchman() {
#Override
public void failed(Throwable e, FrameworkMethod method) {
failedCount++;
}
};
#Test
public void test1() {
fail();
}
#Test
public void test2() {
fail();
}
}

Categories

Resources