junit test fails with exception


Sebastian Dietrich

I am using junit to test my Metrics class. When the test runs, I don't get any exceptions, but the test doesn't succeed. I've tried to find all the different questions but haven't found any. Here is its code.

public class MetricsTest {
  private MetricsHelper metricHelper;
  private MeterRegistry registry;
  private Timer timer;
    
  @Before
  public void setup(){
    registry = spy(mock(MeterRegistry.class));
    when(registry.timer(any(), any(Tags.class))).thenReturn(timer);
    metricHelper = new MetricHelper(registry);
  }

  @Test
  public void testSubmitSuccessWithoutContext() throws Exception {
    metricHelper.submitTimer(metric, 1L, TimeUnit.SECONDS);
    verify(registry, times(1)).timer(
        KEY_PREFIX_TIMER_METRIC + metric.toLowerCase());
  }
}

I got exception java.lang.NullPointerException at com.company.metrics.MetricsHelper.submitTimer(MetricsHelper.java:39). Please help me out.

Deepachetan

It seems to me that you are not initializing the Timer object that could cause this NPE. Please try to initialize as follows:

  private MetricsHelper metricHelper;
  private MeterRegistry registry;
  private Timer timer;
    
  @Before
  public void setup(){
    registry = spy(mock(MeterRegistry.class));
    timer = mock(Timer.class);
    when(registry.timer(any(), any(Tags.class))).thenReturn(timer);
    metricHelper = new MetricHelper(registry);
  }

Related


JUnit exception test fails (unexpectedly)

Simon I'm trying to run a JUnit test to test a method that will throw an exception. However, the test fails and I don't know why. The method that throws the exception is: calcultor.setN();. I made two versions of this test and both failed, although they should

JUnit exception test fails (unexpectedly)

Simon I'm trying to run a JUnit test to test a method that will throw an exception. However, the test fails and I don't know why. The method that throws the exception is: calcultor.setN();. I made two versions of this test and both failed, although they should

JUnit exception test fails (unexpectedly)

Simon I'm trying to run a JUnit test to test a method that will throw an exception. However, the test fails and I don't know why. The method that throws the exception is: calcultor.setN();. I made two versions of this test and both failed, although they should

JUnit test fails despite throwing expected exception

JZahow: I can't seem to figure out why one of my tests is failing. Here is the test: @Test(expected = IllegalArgumentException.class) public void complainsIfFromLocIsDifferentObject() throws Throwable { board.set(make(), 1, 3); //Creates different rook fro

JUnit test fails despite throwing expected exception

JZahow: I can't seem to figure out why one of my tests is failing. Here is the test: @Test(expected = IllegalArgumentException.class) public void complainsIfFromLocIsDifferentObject() throws Throwable { board.set(make(), 1, 3); //Creates different rook fro

jUnit test on JAVA fails

k2wln I'm new here and sorry for the stupid question. I have some problems with my JUnit test: for some reason it shows me an error in index.html. I attach the link to my repository through tests and screens. Thanks! mistake: org.opentest4j.AssertionFailedErro

JUnit test uncaught exception

fumeng : Update: Here is the full test: @Test(expected = NullPointerException.class) public void testMissingData() throws Exception{ Resource<ObjectDataModel, Content, Status> resource = builder.build(content, argA1Response, argA2Response, objFil

Junit exception test

Prithis I have two tests to check for expected exception throws. I am using Junit 4 and have the following syntax. @Test(expected=IllegalArgumentException.class) public void testSomething(){ .......... } One of the tests fails even though IllegalArgumentExcep

Test if Junit catches an exception

walter van der schleren I'll start with a code example; I have to test a function that handles packets. In this function, the packet is opened, and when it does not contain all the expected data, an InvalidParameterExeption is raised and logged. public void ha

JUnit test exception

Paper cut: I am really new to Java. I am running some JUnit tests on a constructor. The constructor is like this: if one of its parameters is given null or empty string, it should throw an exception. When I test this constructor in JUnit with a null or empty s

Junit exception test

Prithis I have two tests to check for expected exception throws. I am using Junit 4 and have the following syntax. @Test(expected=IllegalArgumentException.class) public void testSomething(){ .......... } One of the tests fails even though IllegalArgumentExcep

JUnit test uncaught exception

fumeng : Update: Here is the full test: @Test(expected = NullPointerException.class) public void testMissingData() throws Exception{ Resource<ObjectDataModel, Content, Status> resource = builder.build(content, argA1Response, argA2Response, objFil

JUnit test uncaught exception

fumeng : Update: Here is the full test: @Test(expected = NullPointerException.class) public void testMissingData() throws Exception{ Resource<ObjectDataModel, Content, Status> resource = builder.build(content, argA1Response, argA2Response, objFil

JUnit test uncaught exception

fumeng : Update: Here is the full test: @Test(expected = NullPointerException.class) public void testMissingData() throws Exception{ Resource<ObjectDataModel, Content, Status> resource = builder.build(content, argA1Response, argA2Response, objFil

Test if Junit catches an exception

walter van der schleren I'll start with a code example; I have to test a function that handles packets. In this function, the packet is opened, and when it does not contain all the expected data, an InvalidParameterExeption is raised and logged. public void ha

Test if Junit catches an exception

walter van der schleren I'll start with a code example; I have to test a function that handles packets. In this function, the packet is opened, and when it does not contain all the expected data, an InvalidParameterExeption is raised and logged. public void ha

JUnit test custom exception

hash I'm using JUnit and not quite sure how to test custom exception classes. I created, public class CustomException extends Exception { //@param message is the exception message public CustomException(final String message) { super(message);

Junit Test 5 (Exception)

Pedro Martinez: I'm doing Junit tests and in one of the tests, I'm having issues and I don't understand why. You don't have to allow more objects to be added to the list at any given time, the size of the list is greater than 5, anyway if I add a sixth object

JUnit test exception

Paper cut: I am really new to Java. I am running some JUnit tests on a constructor. The constructor is like this: if one of its parameters is given null or empty string, it should throw an exception. When I test this constructor in JUnit with a null or empty s

JUnit test exception

Paper cut: I am really new to Java. I am running some JUnit tests on a constructor. The constructor is like this: if one of its parameters is given null or empty string, it should throw an exception. When I test this constructor in JUnit with a null or empty s

Junit exception test

Prithis I have two tests to check for expected exception throws. I am using Junit 4 and have the following syntax. @Test(expected=IllegalArgumentException.class) public void testSomething(){ .......... } One of the tests fails even though IllegalArgumentExcep

JUnit test uncaught exception

fumeng : Update: Here is the full test: @Test(expected = NullPointerException.class) public void testMissingData() throws Exception{ Resource<ObjectDataModel, Content, Status> resource = builder.build(content, argA1Response, argA2Response, objFil

JUnit test uncaught exception

fumeng : Update: Here is the full test: @Test(expected = NullPointerException.class) public void testMissingData() throws Exception{ Resource<ObjectDataModel, Content, Status> resource = builder.build(content, argA1Response, argA2Response, objFil