Skip to content

Commit

Permalink
migrate provider-pojo module to mokito (#3421)
Browse files Browse the repository at this point in the history
  • Loading branch information
fu-turer authored Oct 23, 2022
1 parent 31da265 commit c6de4d0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 94 deletions.
4 changes: 2 additions & 2 deletions providers/provider-pojo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import javax.inject.Inject;

import com.google.common.annotations.VisibleForTesting;
import org.apache.servicecomb.core.provider.producer.ProducerMeta;
import org.springframework.beans.factory.InitializingBean;

Expand All @@ -30,6 +31,11 @@ public class PojoProducerMeta extends ProducerMeta implements InitializingBean {

private String schemaInterfaceName;

@VisibleForTesting
void setPojoProducers(PojoProducers pojoProducers) {
this.pojoProducers = pojoProducers;
}

public String getImplementation() {
return implementation;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,41 @@

package org.apache.servicecomb.provider.pojo.instance;

import org.apache.servicecomb.provider.common.MockUtil;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.provider.pojo.PojoConst;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;

public class TestSpringInstanceFactory {

@Test
public void testInitException() {

SpringInstanceFactory lSpringInstanceFactory = new SpringInstanceFactory();
MockUtil.getInstance().mockBeanUtils();
try {
lSpringInstanceFactory.create("TestSpringInstanceFactory");
} catch (Error e) {
Assertions.assertEquals("Fail to find bean:TestSpringInstanceFactory", e.getMessage());
try (MockedStatic<BeanUtils> beanUtilsMockedStatic = Mockito.mockStatic(BeanUtils.class)) {
beanUtilsMockedStatic.when(BeanUtils::getContext).thenReturn(Mockito.mock(ApplicationContext.class));
beanUtilsMockedStatic.when(() -> BeanUtils.getBean(Mockito.anyString())).thenReturn(null);
try {
lSpringInstanceFactory.create("TestSpringInstanceFactory");
} catch (Error e) {
Assertions.assertEquals("Fail to find bean:TestSpringInstanceFactory", e.getMessage());
}
}
}

@Test
public void testInit() {

SpringInstanceFactory lSpringInstanceFactory = new SpringInstanceFactory();
MockUtil.getInstance().mockBeanUtils();
MockUtil.getInstance().mockBeanUtilsObject();
lSpringInstanceFactory.create("org.apache.servicecomb.provider.pojo.instance.TestPojoInstanceFactory");
Assertions.assertEquals(PojoConst.SPRING, lSpringInstanceFactory.getImplName());
try (MockedStatic<BeanUtils> beanUtilsMockedStatic = Mockito.mockStatic(BeanUtils.class)) {
beanUtilsMockedStatic.when(BeanUtils::getContext).thenReturn(Mockito.mock(ApplicationContext.class));
beanUtilsMockedStatic.when(() -> BeanUtils.getBean(Mockito.anyString())).thenReturn(new Object());

lSpringInstanceFactory.create("org.apache.servicecomb.provider.pojo.instance.TestPojoInstanceFactory");
Assertions.assertEquals(PojoConst.SPRING, lSpringInstanceFactory.getImplName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.springframework.context.ApplicationContext;

import mockit.Injectable;

public class TestRpcReferenceProcessor {
RpcReferenceProcessor consumers = new RpcReferenceProcessor();
Expand All @@ -52,7 +49,7 @@ public void postProcessAfterInitialization() {
}

@Test
public void testReference(@Injectable ApplicationContext applicationContext) {
public void testReference() {
SCBEngine scbEngine = SCBBootstrap.createSCBEngineForTest();

PersonReference bean = new PersonReference();
Expand All @@ -68,7 +65,7 @@ public void testReference(@Injectable ApplicationContext applicationContext) {
}

@Test
public void testNoReference(@Injectable ApplicationContext applicationContext) {
public void testNoReference() {
Person bean = new Person();

Assertions.assertNull(bean.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.servicecomb.provider.pojo.schema;

import mockit.Injectable;
import org.apache.servicecomb.foundation.test.scaffolding.spring.SpringUtils;
import org.apache.servicecomb.provider.pojo.IPerson;
import org.apache.servicecomb.provider.pojo.Person;
Expand All @@ -42,7 +41,7 @@ public void testPojoProducers() {
}

@Test
public void testPojoProducersSchemaNull(@Injectable RpcSchema schema) {
public void testPojoProducersSchemaNull() {
IPerson bean = new IPerson() {
};
Assertions.assertSame(bean, producer.postProcessAfterInitialization(bean, "test"));
Expand All @@ -55,7 +54,7 @@ static class PersonEmptySchema implements IPerson {
}

@Test
public void testPojoProducersSchemaIdNull(@Injectable RpcSchema schema) {
public void testPojoProducersSchemaIdNull() {
IPerson bean = new PersonEmptySchema();
Assertions.assertSame(bean, producer.postProcessAfterInitialization(bean, "test"));
Assertions.assertEquals(producer.getProducerMetas().size(), 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import org.junit.Before;
import org.junit.Test;

import mockit.Deencapsulation;
import mockit.Mocked;
import org.junit.jupiter.api.Assertions;
import org.mockito.Mockito;

public class TestPojoSchemaMeta {

Expand All @@ -42,10 +41,10 @@ public void tearDown()
}

@Test
public void testGetImplementation(@Mocked PojoProducers producers)
throws Exception {
public void testGetImplementation() {
PojoProducers producers = Mockito.mock(PojoProducers.class);
lPojoSchemaMeta.setImplementation("implementation");
Deencapsulation.setField(lPojoSchemaMeta, "pojoProducers", producers);
lPojoSchemaMeta.setPojoProducers(producers);
lPojoSchemaMeta.afterPropertiesSet();
Assertions.assertEquals("implementation", lPojoSchemaMeta.getImplementation());
}
Expand Down

0 comments on commit c6de4d0

Please sign in to comment.