wr21
wr21

Reputation: 23

How to properly write JUnit tests?

I'm a bit confused on how to properly write unit tests for my application. Actually i want to know if i have to rewrite an existed method and modify it for junit or just call my existed method and use assert. So far, i use the second option, but i've came across with a problem. For example, inside a Controller method i'm getting the currently logged in user and pass it to some services. If i call this method through JUnit it will show null exception because there is no logged in user. So, 1) do i have to rewrite these kind of methods for testing purposes? 2) Is it proper to call existed methods and use assertion anyway? Thanks

@RequestMapping(value="/like", method=RequestMethod.GET)
public String msgLike(@RequestParam("msgId") long messageId, @RequestParam("like") boolean like){

    User user = new User();
    user = this.userService.getUserByName(this.userService.getLoggedInUsername()); //NULL EXCEPTION HERE WHEN TESTING

    if(!messageService.checkIfLiked(user, messageId)){
        if(like){
            messageService.insertLike(messageId);
            messageService.insertMessageUserLike(user, messageId);
        }
        if(!like){
            messageService.insertDislike(messageId);
            messageService.insertMessageUserLike(user, messageId);
        }
    }

    return "redirect:/home";
}

Upvotes: 0

Views: 574

Answers (3)

KarlM
KarlM

Reputation: 1662

What you need to do is in testing, supply a fake userService that will give the function a User with properties relevant for that test.

Before the test, you need to set this.userService to your FakeUserService.

It's hard to be more specific without seeing more of your code.

This is a good question because this is a common problem when learning to write unit tests. Solving this problem will help you write better code.

Upvotes: 0

Filipe Teixeira
Filipe Teixeira

Reputation: 3565

In your case you are trying to test a rest controller which is slightly different when testing normal methods. For example the following method:

public MyClass{
    public static int sum(int a, int b){
        return a + b;
    }
}

Could simply be tested by:

@Test
public void testSum(){
    assertEquals(3, MyClass.sum(1, 2));
}

But in your case you require the controller api to be used properly. i.e. you require a logged in user. For this you would need to test your controllers by accessing them as users would. This provides some explanation on the process and this answer provides even more details.

Essentially what you are looking for is unit testing rest controllers.

Upvotes: 0

Tnadev
Tnadev

Reputation: 10072

Actually i want to know if i have to rewrite an existed method and modify it for junit or just call my existed method and use assert.

Just call your exiting method, In fact junit is written before writing logic for calling method.

Example:

If you want to test int square(int num) method, which find square of given num,
So write Junits like this ,

  @Test
    squareTest() {
    int square = objectName.square(3);
    assertThat(square , is(equalTo(9)));
    }

And when coding done like this,

int square(int a) {
result=a*a;
return result;
}

Run your Junit.

For your second question,


You will have to read Mocking.

Upvotes: 1

Related Questions