Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for com.zheng.common.util.Paginator #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions zheng-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,30 @@
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.diffblue</groupId>
<artifactId>deeptestutils</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.zheng.common.util;

import static org.mockito.Matchers.anyDouble;
import com.diffblue.deeptestutils.Reflector;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
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.lang.reflect.InvocationTargetException;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.management.*"})
public class PaginatorTest {

/* testedClasses: Paginator */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment

@Test
public void initTotalPageOutputVoid() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test provides 100% coverage for the method :-)
I wonder if we could use some of the setters so that the expected value is non-zero?

final Paginator paginator = new Paginator();
paginator.initTotalPage();
Assert.assertEquals(0L, paginator.getTotalPage());
}

@PrepareForTest({Paginator.class, Math.class})
@Test
public void getHtmlOutputNotNull() throws Exception,
InvocationTargetException {
PowerMockito.mockStatic(Math.class);
final Paginator paginator = new Paginator();
Reflector.setField(paginator, "total", 0L);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to see reflection in our tests. We might be able to work around this using constructor/getters/setters.

paginator.setStep(0);
paginator.setUrl(null);
paginator.setQuery(null);
paginator.setParam(null);
paginator.setPage(0);
paginator.setTotalPage(0L);
Reflector.setField(paginator, "rows", 2);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reflection

PowerMockito.when(Math.ceil(anyDouble()))
.thenReturn(-0x1.ffffffffffffep+31 /* -4.29497e+09 */);
Assert.assertEquals("", paginator.getHtml());
}

@Test
public void getHtmlOutputNotNull2() throws Exception,
InvocationTargetException {
PowerMockito.mockStatic(Math.class);
final Paginator paginator = new Paginator();
Reflector.setField(paginator, "total", 0L);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reflection

paginator.setStep(0);
paginator.setUrl(null);
paginator.setQuery("foo");
paginator.setParam("foo");
paginator.setPage(0);
paginator.setTotalPage(0L);
Reflector.setField(paginator, "rows", -786_432);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reflection

PowerMockito.when(Math.ceil(anyDouble()))
.thenReturn(-0x1.ffffffffffffep+31 /* -4.29497e+09 */);
Assert.assertEquals("", paginator.getHtml());
}
}