-1

I am testing some service-layer methods on JUnit, but I got NPE error.

Here's my junit code

@Autowired
TeacherServiceImpl teacherServiceImpl;

@Test
public void test(){
    String teacherName = "Bernice";
    List<Teacher> teachers = new ArrayList<Teacher>();
    teachers = teacherServiceImpl.findTeacherByName(teacherName);**(error here)**
}

Here's my service layer code

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;

    public List<Teacher> findTeacherByName(String teacherName){
        List<Teacher> teachers = new ArrayList<Teacher>();
        teachers = teacherDao.findByNameCn(teacherName);
        return teachers;
    }
}

Any help will be appreciated!

4 Answers4

0

It seems that TeacherServiceImpl is null. Try to use

@Autowired
TeacherService teacherService;

What's more, you should run your Test Case within Spring, that means you should start Spring Framework by adding @Runwith and @SpringBootTest annotation to your test class if you were using Spring Boot.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public void TeacherServiceTest {
    ...
}
chaos
  • 1,359
  • 2
  • 13
  • 25
0

You should write your tests properly.

Integration testing for the web layer and Unit testing for the service layer.

You are trying to write integration test using @Autowired but I assume that you did not configure it as an integration test. NPE means that teacherServiceImpl reference has no object refer to. @Autowired works only when you have Spring Context with beans that could be injected.

You should not write an integration test for your service, an only Unit test is suitable here (using JUnit + Mockito).

Read please what is Unit testing in general: What is unit testing and how do you do it?

Unit testing example: https://howtodoinjava.com/spring-boot2/spring-boot-mockito-junit-example/

Integration testing example: https://dzone.com/articles/integration-testing-in-spring-boot-1

But I recommend reading more info before coding next time ;)

-1

Man, which class are you testing? if you want to test the TeacherServiceImpl, then you shouldnt mock this, but use constructor (you are testing the mock atm). Second, in your TeacherServiceImpl.class you have a dependency. Unless you mock this dependency and stub the method u use (findByNameCn), you ll be getting NPE

So, in your test, use the constructor for the class you re testing and mock the dependences using Mockito. Dont forget to use the proper annotation for Runner or ExtendWith

-2

I'm assuming that you did not annotate the test class with @SpringBootTest. For this reason you don't have the application context and therefore no injection.

Please read up on testing with Spring :-)

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
  • 5
    To the person voting down this answer: Please leave a comment what you did not like so I can improve my answer accordingly. Thank you :-) – Christoph Grimmer Jul 22 '19 at 07:19
  • 1
    I didn't downvote this, but just for your reference, `@SpringBootTest` is used for integration tests and not unit tests, which the question refers to. – Madhu Bhat Jul 22 '19 at 08:00
  • Since the OP uses `@Autowired` for the class under test I was assuming that he was trying to get the application context to load. Otherwise he would have to use @Mock and @InjectMocks AFAIK. – Christoph Grimmer Jul 22 '19 at 10:50