Junit - Null Pointer Exception with #Before - junit

I just started learning Junit and I just got Null Pointer Exception in my first test.
If I read correctly #Before annotation means it will be called before each test but looks like it doesn't or something else is wrong with this code. In this code below I get Null Pointer in myList.add() line.
import org.junit.Before;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class StudentTest {
private List<String> myList;
#Before
public void init(){
myList = new ArrayList<>();
}
#Test
public void size(){
myList.add("TEST");
assertEquals(1, myList.size());
}
}

The imports (jupiter) indicate you're using Junit5.
In JUnit5 you have to use the #BeforeEach annotation to indicate steps that have to be execute before each test method.
The #Before annotation was used in JUnit4.
I haven't tested this, just read the documentation https://junit.org/junit5/docs/current/user-guide/

Related

org.mockito.MockingDetails.getMockHandler()Lorg/mockito/invocation/MockHandler

I want to write the Unit test using PowerMockito/Mockito for my static method/void method.
But When I try to run , I got the following error:
/Users/<username>/Library/Java/JavaVirtualMachines/corretto-
---- IntelliJ IDEA coverage runner ----
sampling ...
include patterns:
exclude patterns:
ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider jdk.nashorn.api.scripting.NashornScriptEngineFactory not a subtype
ScriptEngineManager providers.next(): javax.script.ScriptEngineFactory: Provider jdk.nashorn.api.scripting.NashornScriptEngineFactory not a subtype
java.lang.NoSuchMethodError: org.mockito.MockingDetails.getMockHandler()Lorg/mockito/invocation/MockHandler;
at org.powermock.api.mockito.invocation.MockHandlerAdaptor.getMockHandler(MockHandlerAdaptor.java:56)
at org.powermock.api.mockito.invocation.MockHandlerAdaptor.createInvocation(MockHandlerAdaptor.java:81)
at org.powermock.api.mockito.invocation.MockHandlerAdaptor.performIntercept(MockHandlerAdaptor.java:61)
at org.powermock.api.mockito.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:93)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:186)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:168)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:138)
I am new to use powerMockito/Mockito, Can Anyone help to figure out the exact issue.
This is my CreateTaskBuilder Class method that I want to test:
Here JgitAccessor.clone() is a static void methid that I used donothing() for it.
public void Repository() throws DependencyFailureException, IOException, GitAPIException {
try {
ServiceAccessor.loadTmpSshTicket();
if (!Files.exists(Paths.get(LambdaEnv.GIT_SSH_SCRIPT.getValue()))) { // getValue will throw exception on null
throw new IllegalStateException(String.format("Environment variable GIT_SSH points to file %s but it doesn't exist.",
LambdaEnv.GIT_SSH_SCRIPT.getValue()));
}
JgitAccessor.clone(REPO_URI, CLONED_REPO_PATH);
} catch (IOException | DependencyFailureException e) {
log.info("Exception occurred while performing Service client integration. exception: [{}]", e.getMessage());
e.printStackTrace();
}
}
And this is the unit test class :
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import static org.mockito.Mockito.*;
#RunWith(PowerMockRunner.class)
#PowerMockIgnore({"javax.management.*"})
#PrepareForTest({CreateTaskBuilder.class, LambdaEnv.class, ServiceAccessor.class, JgitAccessor.class})
public class CreateTaskBuilderTest extends TestUtils {
#Mock
private ServiceAccessor serviceAccessor;
#Mock
private JgitAccessor jgitAccessor;
#InjectMocks
CreateTaskBuilder builder;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
#Test
public void loadServiceTicket_happyCase() throws Exception {
doNothing().when(serviceAccessor).loadTmpSshTicket();
PowerMockito.mockStatic(System.class);
Mockito.when(System.getenv("GIT_SSH")).thenReturn("/tmp/ssh.sh");
PowerMockito.mockStatic(Files.class);
Mockito.when(Files.exists(Paths.get("/tmp/ssh.sh"))).thenReturn(true);
PowerMockito.mockStatic(JgitAccessor.class);
PowerMockito.doNothing().when(JgitAccessor.class, "clone", Mockito.anyString(), Mockito.anyString());
builder.cloneRepository();
}
I am using Mockito = 2.28.x; PowerMockMockito = 2.x;
It looks like you have the wrong versions of libraries on your classpath.
The version of PowerMock you are using requires a Mockito with an org.mockito.MockingDetails.getMockHandler() which is not available in Mocktio 2.8.x. You can find it in a later version in 2.23.x.
Looking at minimum version dependencies for powermock-api-mockito2 version 2.0.0 you should be using mockito version 2.23.0 or later.
So looks like you need to update Mockito to a later version compatible with your PowerMock version, 2.23.x or later instead of 2.8.x.

How to create mock instance of Autowired component in Vert.x

I am trying to create mock instance of the class which is autowired inside Verticle but I am getting it as a null. For synchronous code the way which works is looking not useful for Vert.x.
Verticle is:
#Component
public class MyVerticle extends AbstractVerticle{
#Autowired
private ServiceExecutor serviceExecutor;
#Override
public void start() throws Exception {
super.start();
vertx.eventBus().<String>consumer("address.xyz").handler(handleRequest());
}
private Handler<Message<String>> handleRequest() {
return msg -> {
getSomeData(msg.body().toString())
.setHandler(ar -> {
if(ar.succeeded()){
msg.reply(ar.result());
}else{
msg.reply(ar.cause().getMessage());
}
});
};
}
private Future<String> getSomeData(String inputJson) {
Promise<String> promise = Promise.promise();
String data = serviceExecutor.executeSomeService(inputJson); // Getting NPE here. serviceExecutor is coming as null when trying to create mock of it using Mockito.when.
promise.complete(data);
return promise.future();
}
}
Dependent component is:
#Component
public class ServiceExecutor {
public String executeSomeService(String input){
return "Returning Data";
}
}
Test case is:
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import io.vertx.core.Vertx;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
#RunWith(VertxUnitRunner.class)
public class MyVerticleTest {
#Mock
private ServiceExecutor serviceExecutor;
private Vertx vertx;
#Before
public void setup(TestContext ctx){
MockitoAnnotations.initMocks(this);
Async async = ctx.async();
this.vertx = Vertx.vertx();
vertx.deployVerticle(MyVerticle.class.getName(), h -> {
if(h.succeeded()){
async.complete();
}else{
ctx.fail();
}
});
}
#Test
public void test_consumption(TestContext ctx) {
Async async = ctx.async();
when(serviceExecutor.executeSomeService(Mockito.anyString())).thenReturn("Returning Data");
vertx.eventBus().request("address.xyz","message", h ->{
if(h.succeeded()){
ctx.assertEquals("Returning Data",h.result().body().toString());
async.complete();
}else{
ctx.fail(h.cause());
}
});
}
}
Above Test Case works well if I don't use autowired instance to call a method to get the date. But if used it (which I must do to get the data), it is giving NPE at MyVerticle->getSomeData() method when trying to use serviceExecutor object as a mock. This approach works very well for Synchronous code flow but for Vert.x looks like it won't help. So need help here to mock the autowired instance serviceExecutor inside Verticle.
Add a constructor in your MyVerticle
public MyVerticle(ApplicationContext context) {
context.getAutowireCapableBeanFactory().autowireBean(this);
}
and deploy your verticle something like vertx.deployVerticle(new MyVerticle(context),...
I have application context while deploying the verticle and thats what I am passing in the constructor. Check if this works for you.

How do I mock a static method call from a nother static method in the same class?

I have a utilities class wherein one static method calls another. I want to mock the called method but not the target method. Does anyone have an example?
I've got:
#RunWith(PowerMockRunner.class)
#PrepareForTest({SubjectUnderTest.class})
public class SubjectUnderTestTest {
...
SubjectUnderTest testTarget = PowerMockito.mock(SubjectUnderTest.class, Mockito.CALLS_REAL_METHODS);
Starting from the docs, with some minor tweaks, you can either mock or spy the static method, depending on what you need (spying seems a bit less verbose but has different syntax). You can find below a sample for both, based on PowerMockito 1.7.3 & Mockito 1.10.19.
Given the following simple class with the required 2 static methods:
public class ClassWithStaticMethods {
public static String doSomething() {
throw new UnsupportedOperationException("Nope!");
}
public static String callDoSomething() {
return doSomething();
}
}
You can do something like:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.*;
// prep for the magic show
#RunWith(PowerMockRunner.class)
#PrepareForTest(ClassWithStaticMethods.class)
public class ClassWithStaticMethodsTest {
#Test
public void shouldMockStaticMethod() {
// enable static mocking
mockStatic(ClassWithStaticMethods.class);
// mock the desired method
when(ClassWithStaticMethods.doSomething()).thenReturn("Did something!");
// can't use Mockito.CALLS_REAL_METHODS, so work around it
when(ClassWithStaticMethods.callDoSomething()).thenCallRealMethod();
// validate results
assertThat(ClassWithStaticMethods.callDoSomething(), is("Did something!"));
}
#Test
public void shouldSpyStaticMethod() throws Exception {
// enable static spying
spy(ClassWithStaticMethods.class);
// mock the desired method - pay attention to the different syntax
doReturn("Did something!").when(ClassWithStaticMethods.class, "doSomething");
// validate
assertThat(ClassWithStaticMethods.callDoSomething(), is("Did something!"));
}
}

Calling real method though given different answer to return on a static method execution

I am using power mockito and I am mocking a class SomeUtil having all static methods.
import java.util.List;
class SomeUtil {
// other static methods
public static X createX(String name, List<String> addresses, boolean isEnabled) {
// implementation
return null;
}
// other static methods
}
And I have mocked it as follows.
PowerMockito.mockStatic(SomeUtil.class, Answers.CALLS_REAL_METHODS.get());
Answer<Row> createXAnswer = new Answer<Row>() {
#Override
public Row answer(InvocationOnMock invocation) throws Throwable {
return new X();
}
};
PowerMockito.when(SomeUtil.createX(Mockito.any(String.class), Mockito.any(List.class), Mockito.any(Boolean.class)).thenAnswer(createXAnswer);
But with that createX() method always invokes original method, which I am trying to avoid. I suspect that because of boolean primitive type. Am I missing something here? Please guide
I think you have omitted some important annotations on your test class (#RunWithand #PrepareForTest). This is a complete example:
import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(SomeUtil.class)
public class SomeUtilTest {
#Test
public void should_do_this() {
mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);
final X x = new X();
when(SomeUtil.createX(anyString(), anyList(), anyBoolean())).thenReturn(x);
assertSame(x, SomeUtil.createX(null, null, true));
}
}
This example works but could in some case. For example if the createX raise an exception. fail sometime because the **real method createX is invoked ** * when we stub it:
when(SomeUtil.createX(anyString(), anyList(), anyBoolean())).thenAnswer(createXAnswer);
this is due to the default answer CALLS_REAL_METHODS:
mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);
since the OP can't remove this default answer (see comment):
For only few methods I want to return our own Answer. For the rest of methods I want to call real methods.
I think you will have to use powermock api:
import static org.junit.Assert.assertSame;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.stub;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(SomeUtil.class)
public class SomeUtilTest {
#Test
public void should_do_this() {
mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);
final X x = new X();
stub(method(SomeUtil.class,
"createX",
String.class,
List.class,
boolean.class)).toReturn(x);
assertSame(x, SomeUtil.createX(null, null, true));
}
}

How to test #Valid

In my entities I have some hibernate annotations for validation, like #NotEmpty, #Pattern.. and others
In my controller, on save action, it has an #Valid parameter.
But if any entity has any required field, and there is no annotation I will have problems.
So I would like to test each entity, to ensure they have the necessary notes.
Something like:
#Test(expect=IllegalArgumentException.class)
public void testAllNull() {
Person p = new Persson(); // Person name has an #NotEmpty
validator.validate(p);
}
But how to validate it? Who is called to check #Valid?
Thanks.
I found out how to check:
#Autowired
private LocalValidatorFactoryBean validator;
...
validator.validateProperty(object, propertyName)
Here is a Spring v4.1.x based example of a test validating presence and processing of the #Valid annotation and building of custom JSON response in case of an error.
jUnit
import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import javax.inject.Inject;
import java.util.List;
import static org.abtechbit.miscboard.util.JsonUtils.toJson;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {
RegistrationValidationTest.MockDependencies.class,
})
public class RegistrationValidationTest {
#Inject
MockMvc mvc;
#Test
public void validatesRegistration() throws Exception {
Registration registration = ... //build an invalid Registration object
MvcResult result = mvc.perform(post(RegistrationController.CONTEXT_REGISTER).
contentType(MediaType.APPLICATION_JSON).
content(toJson(registration))).
andExpect(status().isBadRequest()).
andExpect(content().contentType(MediaType.APPLICATION_JSON)).
andReturn();
assertThat(result.getResolvedException(), is(notNullValue()));
String content = result.getResponse().getContentAsString();
assertThat(content, is(notNullValue()));
List<Message> messages = JsonUtils.fromJson(content, new TypeReference<List<Message>>() {
});
assertThat(messages.size(), is(1));
}
public static class MockDependencies {
#Bean
public MockMvc mvc() {
return MockMvcBuilders.standaloneSetup(new RegistrationController()).build();
}
}
}
Controller
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
#Controller
public class RegistrationController
{
public static final String CONTEXT_REGISTER = "/register";
#RequestMapping(value = CONTEXT_REGISTER, method = RequestMethod.POST)
#ResponseBody
public String register(#RequestBody #Valid Registration registration) {
//perform registration
}
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<List> handleValidationException(MethodArgumentNotValidException ex) {
//Build a list of custom Message{String message;} objects
List<Message> messages = ex.getBindingResult().getAllErrors().
stream().map(e->new Message(e.getDefaultMessage())).collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).body(messages);
}
}
Spring MVC Test Framework might be a good choice. By using this, you can be assured that validations in your tests runs codes as Spring #MVC actually works.
Actually, the #Valid annotation is detected by HandlerMethodInvoker, which processes annotations on the handler methods of Spring controllers. Internally, the actual validation logic is delegated to the Validator bean depending on your application context settings. (Hibernate Validator is widely used.)
By default configuration (e.g. <mvc:annotation-driven />), LocalValidatorFactoryBean is used internally to process #Valid annotation as #Falci noted, but it may differ time to time. Instead, Spring MVC Test Framework provides the same environment as the main application uses, hence a good choice.