泽西测试在使用 UriInfo 时给出 NullPointerException



我正在创建一个应用程序,该应用程序应该能够使用REST api创建一个帐户。当我写AccountController.java课时,一切正常。我使用邮递员来验证我是否得到了正确的回复。

所以我的AccountController.java工作正常,但我用它编写的单元测试不起作用。执行单元测试时,它会返回一个NullPointerException,这会导致 REST API 中出现内部服务器错误。在调试时,我发现如果我删除AccountController.java中使用 UriInfo 并返回 Response.ok() 而不是创建,测试就会成功。

所以我的问题是。如何正确测试/模拟返回和 URI 的方法?

波纹管是所有相关的类和依赖项。Account.java类位于另一个包中。但是,作为包含Account.java的项目的主应用程序都是同一父pom的一部分。我不确定这是否与我的问题有关。

帐户控制器.java

@Stateless
@Path("/accounts")
public class AccountController {
@Inject
private AccountService accountService;
@Context
private UriInfo uriInfo;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response create(Account account) {
final Account newAccount = accountService.create(account);
System.out.println("Pre uriinfo message");
//Removing this line and returning Response.ok() removes the exception
final URI uri = uriInfo.getAbsolutePathBuilder().path(newAccount.getId() + "").build();
System.out.println("Post uriinfo message");
return Response.created(uri).entity(newAccount).build();
}
}

帐户控制器测试.java

@RunWith(MockitoJUnitRunner.class)
public class AccountControllerTest extends JerseyTest {
@Mock
private AccountService service;
@Override
protected Application configure() {
final ResourceConfig config = new ResourceConfig(AccountController.class);
config.register(new AbstractBinder() {
@Override
protected void configure() {
bind(service).to(AccountService.class);
}
});
return config;
}
@Test
public void createUserTest() throws Exception {
final Account testAccount = new Account("testName", "test@mail.nl");
when(service.create(testAccount))
.thenReturn(testAccount);
System.out.println("Create account test");
//Create a new Account
final Response correctResult = target("/accounts")
.request(MediaType.APPLICATION_JSON)
.post(Entity.json(testAccount));
Assert.assertEquals(HTTP_CREATED, correctResult.getStatus());
Assert.assertEquals(testAccount, correctResult.readEntity(Account.class));
}
}

帐户.java

@Entity
@XmlRootElement
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String fullName;
@Column(unique = true, nullable = false)
private String mail;
private boolean active;
public Account() { }
public Account(String fullName, String mail) {
this.fullName = fullName;
this.mail = mail;
}
//Contains getters&setters
}

测试依赖项

<!-- jersey version for "Jersey Media Json Jackson" & "Jersey Test Framework Provider Jetty" -->
<properties>
<jersey.version>2.26-b03</jersey.version>
</properties>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.test-framework.providers/jersey-test-framework-provider-jetty -->
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.4.Final</version>
<scope>test</scope>
</dependency>

所以我终于想通了。 在AccountController.java的这一行的某个地方,抛出了一条NullPointerException

final URI uri = uriInfo.getAbsolutePathBuilder().path(newAccount.getId() + "").build();

这只发生在使用我的 JerseyUnitTest 时,所以我想 Jersey 必须改变UriInfo的一些行为。

最终,我通过在Account.java中覆盖equals来解决问题。

相关内容

  • 没有找到相关文章

最新更新