Mocking Toolkit using JMockit Expectations - unit-testing

I am trying to mock java.awt.Toolkit.beep() using JMockit Expectations. I have the following code in my test case:
new Expectations() {
Toolkit mock;
{
mock.beep();
}
}.endRecording();
When I run the test case (JUnit 4), I get the following exception at the "new Expectations" line:
java.lang.ClassFormatError: Code attribute in native or abstract methods in class file $Mock
Any ideas?

The default jmock can only mock interface. To mock class, you need to following these instructions

Related

spring boot application main method unit test with mockito

I'm very new to unit testing and mockito. How can write a unit test for the code below.
#SpringBootApplication
public class MyApplication {
public static void main(String[] argv) {
SpringApplication.run(MyApplication.class);
}
}
From my experience, you usually don't test the one-line main method of a #SpringBootApplication annotated class. The only logic you have there is a static call to run from SpringApplication class.
If you really want to unit test that, then the only possibility is to use PowerMock. Mockito itself doesn't offer the possibility to mock static methods, while PowerMock allows you to do that. You could refer to this PowerMock wiki page.
But seriously, as JB Nizet said: nothing to test here, really. Especially that the recommendation is to use Spring Initializr which auto-generates it for you and I believe that in most cases - you shouldn't really be touching that.

How do I declare that I "expect" an exception in a unit test with Groovy, JUnit and Maven?

I have the following Groovy unit test code:
class MyTest extends GroovyTestCase {
#Test(expected = IllegalArgumentException.class)
public void testReadFileMissing() {
// does something which causes an IllegalArgumentException
}
// more tests
}
This works perfectly with my Java unit tests, but with groovy tests, mvn clean test runs the test, but the test fails because an IllegalArgumentException was thrown. In other words, my "expected" annotation attribute seems to be completely ignored.
I could of course simply use a try/catch block to check the behaviour I'm interested in, but I'd like to use the JUnit API if possible because that's what it's for and I find the resulting code simpler to read and understand.
So, can anyone tell me what I'm doing wrong?
either don't use GroovyTestCase and the according JUnit class instead as base or go full groovy and use shouldFail. examples are here http://mrhaki.blogspot.de/2009/11/groovy-goodness-testing-for-expected.html
You can use shouldFail or shouldFailWithCause showcasing exactly which kind of exception is expected from the code under test.

Mock UserContext and FacesContext - jUnit

I'm trying to write some basic backingBean tests but I'm stuck with mocking the UserContext and facesContext.
This code is in the code that I'm trying to test:
UserContext uc = ContextProvider.getContext();
Locale locale = uc.getLocale();
ResourceBundle bundle = ResourceBundle.getBundle("AppMessages", locale);
String message = bundle.getString("this.is.the.message.key");
In another block of code I've got the following:
FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().redirect(handleRedirect("someString"));
How could I mock these in a standard jUnit test using only mockito? Or do I have to use something like PowerMock?
Mockito can't mock static methods. You have a few options though:
Extract the code under test to methods which takes the UserContext and ResourceBundle or FacesContext instances as arguments
Wrap the static method calls in a factory object, and pass the factory objact instance as an argument to the code under test
PowerMock is an option, but slows down test execution and in my opinion allows bad-practice solutions
Instead create mocks for yourself, you can use Apache MyFaces Test, which provided already prepared Mock Objects for JSF artifacts. It will work better in a more wide range of cases, with less effort.

GWTP unit testing using Mockito

I am trying to use Mockito to test my GWTP application.
I am trying to Mock my View,Proxy,Placemanager and eventbus.
I tried using
#Mock
AbcView abc;
and Abcview abc = Mockito.mock(AbcView.class);
However every time the mocked view is instantiated as null.
How shall i address the same?
Once the view is mocked i will be able go on with testing my presenter class, as the constructor of presenter has following code:
getView().setUiHandlers( this );
so until view is instantiated properly null pointer exception is thrown.
Did you run your test using the MockitoJUnitRunner runner?
#RunWith(MockitoJUnitRunner.class)
public class ExampleTest {
#Mock
private List list;
#Test
public void shouldDoSomething() {
list.add(100);
}
}
Besides #Sydney's response, you also need o make sure that AbcView.class is not final. I forget whether a final class results in a null or a runtime error, but that can be a cause for some sort of unexpected behavior -- one way or another, the mocking doesn't work. And if the class is not final, you need to make sure that any methods you stub on that mock are not final.

How do I use PowerMock / Mockito / EasyMock to use a mocked object for dependency injection?

I have an AuthenticationManager.authenticate(username,password) method that gets called in someMethod of a SomeService under test. The AuthenticationManager is injected into SomeService:
#Component
public class SomeService {
#Inject
private AuthenticationManager authenticationManager;
public void someMethod() {
authenticationManager.authenticate(username, password);
// do more stuff that I want to test
}
}
Now for the unit test I need the authenticate method to just pretend it worked correctly, in my case do nothing, so I can test if the method itself does the expected work (Authentication is tested elsewhere according to the unit testing principles, however authenticate needs to be called inside that method) So I am thinking, I need SomeService to use a mocked AuthenticationManager that will just return and do nothing else when authenticate() gets called by someMethod().
How do I do that with PowerMock (or EasyMock / Mockito, which are part of PowerMock)?
With Mockito you could just do that with this piece of code (using JUnit) :
#RunWith(MockitoJUnitRunner.class)
class SomeServiceTest {
#Mock AuthenitcationManager authenticationManager;
#InjectMocks SomeService testedService;
#Test public void the_expected_behavior() {
// given
// nothing, mock is already injected and won't do anything anyway
// or maybe set the username
// when
testService.someMethod
// then
verify(authenticationManager).authenticate(eq("user"), anyString())
}
}
And voila. If you want to have specific behavior, just use the stubbing syntax; see the documentation there.
Also please note that I used BDD keywords, which is a neat way to work / design your test and code while practicing Test Driven Development.
Hope that helps.