我们过去7-8个月一直在使用Firebase.
它是一个非常棒的工具,感谢您的努力.
在这里,我有一个问题,关于是否有一种方法来修改数据而不实际写入数据库.
最常见的是当我们调试我们总是写入我们的实时数据库的东西时,我们必须手动删除它们.你可以想象它是多么痛苦.
那么有没有像测试数据库那样我们可以写东西而不用担心修改数据库?
我可以在每次想要写东西时导出整个数据库,然后在我完成后将其导回.但这是一个相当繁琐的程序.如果我正在做auth的事情,那么目前无法导出用户数据.
我在Spring应用程序中进行了一个简单的测试,其默认时区设置为UTC:
@SpringBootApplication
public class MemberIntegrationApp {
@Autowired
private TimeZoneProperties timeZoneProperties;
@PostConstruct
void started() {
TimeZone.setDefault(TimeZone.getTimeZone(timeZoneProperties.getAppDefault())); // which is UTC
}
public static void main(String[] args) {
SpringApplication.run(MemberIntegrationApp.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
并且,这个简单的测试:( 测试类注释@SpringBootTest用于在主类中加载配置并且@SpringRunner也被应用)
/**
* Test the effect of setting timezone
*/
@Test
public void testTimezoneSettingOnSimpleDateFormat() throws ParseException {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = "2018-08-08 12:34:56";
log.info("Trying to parse the date string: {}", d);
Date result = f.parse(d);
log.info("The result …Run Code Online (Sandbox Code Playgroud) 我有一个返回自定义对象的方法
public MyObject getTheObject(){
...
return muObject;
}
Run Code Online (Sandbox Code Playgroud)
其单元测试检查getTheObject()方法返回的对象是否不为null
@Test
public void testGetTheObject(){
...
assertNotNull(actualObject);
}
Run Code Online (Sandbox Code Playgroud)
并通过测试。
当使用Pitest进行突变测试时,它显示一个SURVIVED突变,它表示如下内容:
mutated returned of Object for value for ..../getTheObject to ( if ( x!= null ) null else throw new RuntimeException )
Run Code Online (Sandbox Code Playgroud)
问题是我们的单元测试应该如何摆脱这个问题,并且KILL该突变
我正在使用Retrofit 2,在使用Firebase Auth之前我曾经将我的令牌存储SharedPreferences在我的HttpInterceptor
@Named("rest_api")
@Singleton
@Provides
Interceptor provideRESTInterceptor(final UserManager userManager) {
return new Interceptor() {
@Override
public Response intercept(final Chain chain) throws IOException {
final Request original = chain.request();
Request request = original.newBuilder()
.header("Accept", "application/json")
.header("Authorization", "XXXX " + sharedPreference.getToken())
.method(original.method(), original.body())
.build();
Response response = chain.proceed(request);
return response;
}
};
}
Run Code Online (Sandbox Code Playgroud)
我怎么能用这样的东西来实现
FirebaseUser.getToken(false/true)呢?我不知道如何等待侦听器回调,而不是使用firebase令牌处理请求.
我也想在这里检查令牌有效性以及它是否即将到期 getToken(true)
如果电子邮件地址已存在,则抛出一条异常消息(“消息:”具有“+tempEmailId+”的用户已存在”)。当我在邮递员中测试时,我没有收到异常消息。你能帮我吗?在哪里问题?
控制器类:
@RestController
public class RegistrationController {
@Autowired
private RegistrationService service;
@PostMapping("/registeruser")
public User registerUser(@RequestBody User user) throws Exception {
String tempEmailId = user.getEmailId();
if(tempEmailId !=null && !"".equals(tempEmailId)) {
User userObject = service.fetchUserByEmailId(tempEmailId);
if(userObject!=null) {
throw new Exception("User with "+tempEmailId+" is already exist");
}
}
User userObject = null;
userObject = service.saveUser(user);
return userObject;
}
}
Run Code Online (Sandbox Code Playgroud)
存储库:
public interface RegistrationRepository extends JpaRepository<User, Integer> {
public User findByEmailId(String emailId); // Here we declare
}
Run Code Online (Sandbox Code Playgroud)
服务:
@Service
public class RegistrationService {
@Autowired …Run Code Online (Sandbox Code Playgroud) java ×4
firebase ×2
spring-boot ×2
android ×1
datetime ×1
java-8 ×1
jpa ×1
pitest ×1
spring ×1
unit-testing ×1