我正在尝试更新我的测试用例以使用@DataJpaTest.但是,我遇到了一些似乎与Spring Security相关的问题.以下是测试类的示例.
@RunWith(SpringRunner.class)
@DataJpaTest
public class ExampleRepositoryTest {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Inject
private ExampleRepository repository;
@Test
public void test() throws Exception {
...
}
Run Code Online (Sandbox Code Playgroud)
java.lang.IllegalStateException: Failed to load ApplicationContext由于缺少bean,我不断收到错误org.springframework.security.config.annotation.ObjectPostProcessor.
该项目是一个带有Spring安全性的RESTful应用程序.原始测试用例使用创建了完整的Spring Boot上下文@SpringBootTest.这@DataJpaTest应该可以帮助我测试JPA切片,这正是我想要的.
任何帮助将不胜感激.我错过了什么?
我正在尝试为服务编写测试.但我嘲笑repository依赖是不成功的.其他非存储库依赖项已成功模拟.存储库实例始终是实际实现,而不是模拟实例.
我正在使用Spring Boot和Spring Data JPA来构建应用程序.Mockito用于嘲笑.我设法将问题提炼成一个测试项目.完整的测试项目在GitHub上.以下是测试项目的代码片段; 以下是PersonServiceTest班级.
更新1: before()代码应该检查personRepository不personService
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(App.class)
@WebAppConfiguration
@TestExecutionListeners({ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class, SqlScriptsTestExecutionListener.class})
@Transactional
@ActiveProfiles({"mock-some-bean", "mock-person-repository"})
public class PersonServiceTest {
@Inject
private SomeBean someBean;
@Inject
private PersonRepository personRepository;
@Inject
private PersonService personService;
@Before
public void before() {
assertThat(mockingDetails(someBean).isMock(), is(true));
assertThat(mockingDetails(personRepository).isMock(), is(true));
}
@Test
public void doSomething() throws Exception { ... }
}
Run Code Online (Sandbox Code Playgroud)
测试类使用两个配置文件:mock-some-bean和mock-person-repository.基本上,我使用配置文件来确定应该嘲笑什么.在进行任何测试之前,我断言someBean和personService被模拟的实例.someBean被嘲笑但personService总是失败.以下代码是TestConfig …
我在尝试将枚举与Django和Graphene结合使用时遇到一些问题。即使枚举的值保留在SQLite中并且可以正确检索,它仍然导致错误。下面是错误的示例。
{
"message": "Cannot return null for non-nullable field DjangoObject.vanillaEnum.",
"locations": [
{
"line": 10,
"column": 5
}
]
},
Run Code Online (Sandbox Code Playgroud)
我正在将Django 2.0.3和Graphene 2.0.1与Anaconda3 5.0.0和Python 3.6.4一起使用。我设法使用一个简单的示例重现了该错误,该示例在我的GitHub帐户上可用。
在中models.py,我定义了Enum使用该枚举的Python 和Django模型。此处的模型按预期工作(AFAIK),不涉及任何石墨烯依赖项。默认的SQLite数据库似乎也具有正确的值。
models.py
import enum
from django.db import models
@enum.unique
class VanillaEnum(enum.Enum):
RED = enum.auto()
BLUE = enum.auto()
GREEN = enum.auto()
@classmethod
def choices(cls):
return tuple((x, x) for x in cls)
class DjangoModel(models.Model):
name = models.CharField(max_length=20)
vanilla_enum = models.CharField(choices=VanillaEnum.choices(), default=VanillaEnum.GREEN, max_length=20)
def __str__(self):
return f'name={self.name}, vanilla_enun={self.vanilla_enum}'
Run Code Online (Sandbox Code Playgroud)
接下来,在中 …